Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 61 additions & 10 deletions src/cosmos/CosmosClient.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -17,29 +11,44 @@ export class CosmosClient {
}
}

async balance(address: string, denom: string): Promise<QueryBalanceResponse> {
async balance(address: string, denom: string) {
const client = await this.getCosmosRpcClient();
return client.cosmos.bank.v1beta1.balance({
address,
denom,
});
}

async supply(denom: string): Promise<QuerySupplyOfResponse> {
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<QueryValidatorResponse> {
async validator(validatorAddr: string) {
const client = await this.getCosmosRpcClient();
return client.cosmos.staking.v1beta1.validator({
validatorAddr,
});
}

async contractInfo(address: string): Promise<QueryContractInfoResponse> {
async contractInfo(address: string) {
const client = await this.getCosmwasmRpcClient();
return client.cosmwasm.wasm.v1.contractInfo({
address,
Expand All @@ -56,6 +65,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;
Expand Down