Skip to content

Commit

Permalink
Merge pull request #17 from securesecrets/snip20-token-info-docs
Browse files Browse the repository at this point in the history
feat: snip20 token info async wrapper, docs
  • Loading branch information
AustinWoetzel authored Nov 1, 2023
2 parents 84fed63 + 9ea2b57 commit b57e806
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
{ text: 'Swap', link: '/swap' },
{ text: 'Oracle', link: '/oracle' },
{ text: 'Batch Query', link: '/batch-query' },
{ text: 'Snip20', link: '/snip20' },
]
}
],
Expand Down
54 changes: 54 additions & 0 deletions docs/snip20.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Snip20 Token Examples

This page demonstrates how to query Snip20 token contracts


## Token Info

**input**

```js
async function querySnip20TokenInfo({
snip20ContractAddress,
snip20CodeHash,
lcdEndpoint,
chainId,
}:{
snip20ContractAddress: string,
snip20CodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
}): Promise<TokenInfo>
```

**output**

```js
type TokenInfo = {
name: string,
symbol: string,
decimals: number,
totalSupply: string,
}

```

**example use**

```js
const output = await querySnip20TokenInfo = ({
contractAddress: 'secret1fl449muk5yq8dlad7a22nje4p5d2pnsgymhjfd',
codeHash: '638a3e1d50175fbcb8373cf801565283e3eb23d88a9b7b7f99fcc5eb1e6b561e'
})
console.log(output)
```
***console***
```md
{
name: 'Silk',
symbol: 'SILK',
decimals: 6,
totalSupply: '2247680264140',
};

```
20 changes: 17 additions & 3 deletions src/contracts/services/snip20.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
parseTokenInfo,
querySnip20TokenInfo$,
querySnip20TokenInfo,
} from '~/contracts/services/snip20';
import {
test,
Expand Down Expand Up @@ -38,16 +39,16 @@ test('it can parse the response snip20 token info query', () => {
)).toStrictEqual(tokenInfoParsed);
});

test('it can call the snip20 token info query', () => {
sendSecretClientContractQuery$.mockReturnValue(of(tokenInfoResponse));

test('it can call the snip20 token info query', async () => {
const input = {
snip20ContractAddress: 'CONTRACT_ADDRESS',
snip20CodeHash: 'CODE_HASH',
lcdEndpoint: 'LCD_ENDPOINT',
chainId: 'CHAIN_ID',
};

// observable function
sendSecretClientContractQuery$.mockReturnValueOnce(of(tokenInfoResponse));
let output;
querySnip20TokenInfo$(input).subscribe({
next: (response) => {
Expand All @@ -63,4 +64,17 @@ test('it can call the snip20 token info query', () => {
});

expect(output).toStrictEqual(tokenInfoParsed);

// async/await function
sendSecretClientContractQuery$.mockReturnValueOnce(of(tokenInfoResponse));
const response = await querySnip20TokenInfo(input);

expect(sendSecretClientContractQuery$).toHaveBeenCalledWith({
queryMsg: 'TOKEN_INFO_MSG',
client: 'CLIENT',
contractAddress: input.snip20ContractAddress,
codeHash: input.snip20CodeHash,
});

expect(response).toStrictEqual(tokenInfoParsed);
});
26 changes: 25 additions & 1 deletion src/contracts/services/snip20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
switchMap,
map,
first,
lastValueFrom,
} from 'rxjs';
import { sendSecretClientContractQuery$ } from '~/client/services/clientServices';
import { snip20 } from '~/contracts/definitions/snip20';
Expand All @@ -17,7 +18,7 @@ const parseTokenInfo = (response: TokenInfoResponse): TokenInfo => ({
});

/**
* query the factory config
* query the snip20 token info
*/
const querySnip20TokenInfo$ = ({
snip20ContractAddress,
Expand All @@ -40,7 +41,30 @@ const querySnip20TokenInfo$ = ({
first(),
);

/**
* query the snip20 token info
*/
async function querySnip20TokenInfo({
snip20ContractAddress,
snip20CodeHash,
lcdEndpoint,
chainId,
}:{
snip20ContractAddress: string,
snip20CodeHash?: string,
lcdEndpoint?: string,
chainId?: string,
}) {
return lastValueFrom(querySnip20TokenInfo$({
snip20ContractAddress,
snip20CodeHash,
lcdEndpoint,
chainId,
}));
}

export {
querySnip20TokenInfo$,
parseTokenInfo,
querySnip20TokenInfo,
};
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
batchQueryPairsInfo,
batchQueryStakingInfo,
} from '~/contracts/services/swap';
import { querySnip20TokenInfo$ } from './contracts/services/snip20';
import {
querySnip20TokenInfo$,
querySnip20TokenInfo,
} from './contracts/services/snip20';

export {
getSecretNetworkClient$,
Expand All @@ -44,4 +47,5 @@ export {
batchQueryPairsInfo,
batchQueryStakingInfo,
querySnip20TokenInfo$,
querySnip20TokenInfo,
};

0 comments on commit b57e806

Please sign in to comment.