From d9bfd5dec371dce8e0b949c3baf924c2cee3b105 Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 1 Nov 2023 15:02:00 -0500 Subject: [PATCH 1/2] feat: snip20 token info async wrapper, docs --- docs/.vitepress/config.ts | 1 + docs/snip20.md | 54 +++++++++++++++++++++++++++ src/contracts/services/snip20.test.ts | 20 ++++++++-- src/contracts/services/snip20.ts | 24 ++++++++++++ src/index.ts | 6 ++- 5 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 docs/snip20.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 7f5e011..5d5ba09 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -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' }, ] } ], diff --git a/docs/snip20.md b/docs/snip20.md new file mode 100644 index 0000000..e7849a0 --- /dev/null +++ b/docs/snip20.md @@ -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 +``` + +**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', +}; + +``` \ No newline at end of file diff --git a/src/contracts/services/snip20.test.ts b/src/contracts/services/snip20.test.ts index 078e590..fad8c04 100644 --- a/src/contracts/services/snip20.test.ts +++ b/src/contracts/services/snip20.test.ts @@ -1,6 +1,7 @@ import { parseTokenInfo, querySnip20TokenInfo$, + querySnip20TokenInfo, } from '~/contracts/services/snip20'; import { test, @@ -38,9 +39,7 @@ 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', @@ -48,6 +47,8 @@ test('it can call the snip20 token info query', () => { chainId: 'CHAIN_ID', }; + // observable function + sendSecretClientContractQuery$.mockReturnValueOnce(of(tokenInfoResponse)); let output; querySnip20TokenInfo$(input).subscribe({ next: (response) => { @@ -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); }); diff --git a/src/contracts/services/snip20.ts b/src/contracts/services/snip20.ts index 5b0b423..25f6a6f 100644 --- a/src/contracts/services/snip20.ts +++ b/src/contracts/services/snip20.ts @@ -3,6 +3,7 @@ import { switchMap, map, first, + lastValueFrom, } from 'rxjs'; import { sendSecretClientContractQuery$ } from '~/client/services/clientServices'; import { snip20 } from '~/contracts/definitions/snip20'; @@ -40,7 +41,30 @@ const querySnip20TokenInfo$ = ({ first(), ); +/** + * query the factory config + */ +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, }; diff --git a/src/index.ts b/src/index.ts index 5b6d6bc..983eec6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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$, @@ -44,4 +47,5 @@ export { batchQueryPairsInfo, batchQueryStakingInfo, querySnip20TokenInfo$, + querySnip20TokenInfo, }; From 9ea2b57a2ad96e685829dd2a13c747cd7b29ea61 Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 1 Nov 2023 15:04:00 -0500 Subject: [PATCH 2/2] docs: fix copy paste issue --- src/contracts/services/snip20.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/contracts/services/snip20.ts b/src/contracts/services/snip20.ts index 25f6a6f..56d447e 100644 --- a/src/contracts/services/snip20.ts +++ b/src/contracts/services/snip20.ts @@ -18,7 +18,7 @@ const parseTokenInfo = (response: TokenInfoResponse): TokenInfo => ({ }); /** - * query the factory config + * query the snip20 token info */ const querySnip20TokenInfo$ = ({ snip20ContractAddress, @@ -42,7 +42,7 @@ const querySnip20TokenInfo$ = ({ ); /** - * query the factory config + * query the snip20 token info */ async function querySnip20TokenInfo({ snip20ContractAddress,