Skip to content

Commit

Permalink
Merge pull request #154 from securesecrets/develop
Browse files Browse the repository at this point in the history
snip20 transaction history queries
  • Loading branch information
AustinWoetzel authored Jun 5, 2024
2 parents 6786d1d + 1cc4633 commit 3f68375
Show file tree
Hide file tree
Showing 20 changed files with 1,015 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-forks-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": patch
---

snip20 transaction history using a batch query
5 changes: 5 additions & 0 deletions .changeset/red-seahorses-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shadeprotocol/shadejs": patch
---

snip20 transfer history query
94 changes: 94 additions & 0 deletions docs/queries/snip20.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,98 @@ console.log(output)
```md
'123'
```

## Get Transaction History

**input**

```ts
async function querySnip20TransactionHistory({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
snip20ContractAddress,
snip20CodeHash,
ownerAddress,
viewingKey,
page,
pageSize,
shouldFilterDecoys,
minBlockHeightValidationOptions,
}:{
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
snip20ContractAddress: string,
snip20CodeHash: string,
ownerAddress: string,
viewingKey: string,
page: number,
pageSize: number,
shouldFilterDecoys?: boolean,
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
}): Promise<TransactionHistory>
```

**output**

```ts
type Snip20Tx = {
id: number;
action: TxAction; // see secretJS types
denom: string,
amount: string,
memo?: string;
blockTime?: number;
blockHeight?: number;
}
type TransactionHistory = {
txs: Snip20Tx[],
tokenAddress: string,
totalTransactions?: number,
blockHeight: number,
}
```

## Get Transfer History
This query is used for legacy snip20s that do not support the newer transaction history query.

**input**

```ts
async function querySnip20TransferHistory({
queryRouterContractAddress,
queryRouterCodeHash,
lcdEndpoint,
chainId,
snip20ContractAddress,
snip20CodeHash,
ownerAddress,
viewingKey,
page,
pageSize,
minBlockHeightValidationOptions,
}:{
queryRouterContractAddress: string,
queryRouterCodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
snip20ContractAddress: string,
snip20CodeHash: string,
ownerAddress: string,
viewingKey: string,
page: number,
pageSize: number,
minBlockHeightValidationOptions?: MinBlockHeightValidationOptions,
}): Promise<TransactionHistory>
```

**output**

```ts
see querySnip20TransactionHistory for output type
```
64 changes: 57 additions & 7 deletions src/contracts/definitions/snip20.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
import { test, expect } from 'vitest';
import { snip20 } from '~/contracts/definitions/snip20';

// QUERIES
test('it checks the shape of the snip20 balance query', () => {
const output = {
balance: {
address: 'MOCK_ADDRESS',
key: 'MOCK_KEY',
},
};
expect(snip20.queries.getBalance('MOCK_ADDRESS', 'MOCK_KEY')).toStrictEqual(output);
});

test('it checks the shape of the snip20 token info query', () => {
const output = {
token_info: {},
};
expect(snip20.queries.tokenInfo()).toStrictEqual(output);
});

test('it checks the shape of the snip20 transaction history query', () => {
const input = {
ownerAddress: 'MOCK_OWNER_ADDRESS',
viewingKey: 'MOCK_VIEWING_KEY',
page: 1,
pageSize: 2,
shouldFilterDecoys: true,
};
const output = {
transaction_history: {
address: input.ownerAddress,
page_size: input.pageSize,
page: input.page,
key: input.viewingKey,
should_filter_decoys: input.shouldFilterDecoys,
},
};
expect(snip20.queries.getTransactionHistory(input)).toStrictEqual(output);
});

test('it checks the shape of the snip20 transfer history query', () => {
const input = {
ownerAddress: 'MOCK_OWNER_ADDRESS',
viewingKey: 'MOCK_VIEWING_KEY',
page: 1,
pageSize: 2,
};
const output = {
transfer_history: {
address: input.ownerAddress,
page_size: input.pageSize,
page: input.page,
key: input.viewingKey,
},
};
expect(snip20.queries.getTransferHistory(input)).toStrictEqual(output);
});

// EXECUTIONS
test('it checks the shape of the snip20 send', () => {
const params = {
recipient: 'MOCK_RECIPIENT',
Expand Down Expand Up @@ -103,13 +160,6 @@ test('it checks the shape of the snip20 redeem', () => {
})).toStrictEqual(output);
});

test('it checks the shape of the snip20 token info query', () => {
const output = {
token_info: {},
};
expect(snip20.queries.tokenInfo()).toStrictEqual(output);
});

test('it checks the shape of the snip20 increase allowance', () => {
const inputSpender = 'MOCK_SPENDER';
const inputAmount = 'MOCK_AMOUNT';
Expand Down
45 changes: 45 additions & 0 deletions src/contracts/definitions/snip20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,51 @@ const snip20 = {
token_info: {},
};
},
getTransactionHistory({
ownerAddress,
viewingKey,
page,
pageSize,
shouldFilterDecoys,
}:{
ownerAddress: string,
viewingKey: string,
page: number,
pageSize: number,
shouldFilterDecoys: boolean,
}) {
return {
transaction_history: {
address: ownerAddress,
page_size: pageSize,
page,
key: viewingKey,
should_filter_decoys: shouldFilterDecoys,
},
};
},
// transfer history is a query message used for older snip20s that do
// not support the newer transaction_history query
getTransferHistory({
ownerAddress,
viewingKey,
page,
pageSize,
}:{
ownerAddress: string,
viewingKey: string,
page: number,
pageSize: number,
}) {
return {
transfer_history: {
address: ownerAddress,
page_size: pageSize,
page,
key: viewingKey,
},
};
},
},
messages: {
send({
Expand Down
Loading

0 comments on commit 3f68375

Please sign in to comment.