From 21bf491dc072ad623651637695d792aba8841d6b Mon Sep 17 00:00:00 2001 From: ak <56300182+akcgjc007@users.noreply.github.com> Date: Fri, 27 Sep 2024 10:30:23 +0530 Subject: [PATCH 1/2] feat: add contract key-valut pair helper --- src/cosmos/CosmosClient.ts | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/cosmos/CosmosClient.ts b/src/cosmos/CosmosClient.ts index 95111d95..1795024a 100644 --- a/src/cosmos/CosmosClient.ts +++ b/src/cosmos/CosmosClient.ts @@ -56,6 +56,48 @@ export class CosmosClient { return JSON.parse(Buffer.from(res.data).toString('utf8')); } + async fetchAllContractStates(address: string) { + let key: any = []; + + const client = await this.getCosmwasmRpcClient(); + + let models: any = []; + let parsedModels: any = []; + + let iterationCount = 0; + while (iterationCount < 20) { + iterationCount++; + try { + const res = await client.cosmwasm.wasm.v1.allContractState({ + address, + pagination: { + countTotal: false, + key, + limit: 100n, + offset: 0n, + reverse: false, + }, + }); + models = [...models, ...res.models]; + parsedModels = [ + ...parsedModels, + ...res.models.map(({ key, value }) => { + return { + key: Buffer.from(key).toString('utf8'), + value: JSON.parse(Buffer.from(value).toString('utf8')), + }; + }), + ]; + + key = res.pagination?.nextKey; + } catch (error) { + break; + } + } + + return parsedModels; + } + async fetchLatestHeight() { const res = await axios.get(`${this.rpcEndpoint}/status`); return res.data?.result.sync_info.latest_block_height; From cb7eba23582bf2763a949671f4fb96d0342d06cd Mon Sep 17 00:00:00 2001 From: ak <56300182+akcgjc007@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:25:37 +0530 Subject: [PATCH 2/2] feat: add all balances helper --- src/cosmos/CosmosClient.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/cosmos/CosmosClient.ts b/src/cosmos/CosmosClient.ts index 1795024a..48d28c60 100644 --- a/src/cosmos/CosmosClient.ts +++ b/src/cosmos/CosmosClient.ts @@ -1,11 +1,5 @@ import { cosmos, cosmwasm, ibc, osmosis } from 'osmojs'; import axios from 'axios'; -import { - QueryBalanceResponse, - QuerySupplyOfResponse, -} from 'osmojs/cosmos/bank/v1beta1/query'; -import { QueryValidatorResponse } from 'osmojs/cosmos/staking/v1beta1/query'; -import { QueryContractInfoResponse } from 'osmojs/cosmwasm/wasm/v1/query'; export class CosmosClient { constructor( @@ -17,7 +11,7 @@ export class CosmosClient { } } - async balance(address: string, denom: string): Promise { + async balance(address: string, denom: string) { const client = await this.getCosmosRpcClient(); return client.cosmos.bank.v1beta1.balance({ address, @@ -25,21 +19,36 @@ export class CosmosClient { }); } - async supply(denom: string): Promise { + async allBalances(address: string) { + const client = await this.getCosmosRpcClient(); + const res = await client.cosmos.bank.v1beta1.allBalances({ + address, + pagination: { + countTotal: false, + key: [] as any, + limit: 1000n, + offset: 0n, + reverse: false, + }, + }); + return res.balances; + } + + async supply(denom: string) { const client = await this.getCosmosRpcClient(); return client.cosmos.bank.v1beta1.supplyOf({ denom, }); } - async validator(validatorAddr: string): Promise { + async validator(validatorAddr: string) { const client = await this.getCosmosRpcClient(); return client.cosmos.staking.v1beta1.validator({ validatorAddr, }); } - async contractInfo(address: string): Promise { + async contractInfo(address: string) { const client = await this.getCosmwasmRpcClient(); return client.cosmwasm.wasm.v1.contractInfo({ address,