Skip to content

Commit

Permalink
fix: update setup prefixService for query
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhnv2303 committed Oct 14, 2022
1 parent fa8da0f commit 5711d92
Show file tree
Hide file tree
Showing 28 changed files with 123 additions and 93 deletions.
63 changes: 38 additions & 25 deletions src/lib/cosm/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DirectSecp256k1HdWallet, Registry } from '@cosmjs/proto-signing';
import { AccountData } from '@cosmjs/proto-signing/build/signer';
import { SigningStargateClient } from '@cosmjs/stargate';
import { PrivateSigningStargateClient } from '@cosmjs/stargate/build/signingstargateclient';
import {
PrivateSigningStargateClient
} from '@cosmjs/stargate/build/signingstargateclient';
import { expect } from 'chai';
import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx';
import { BondStatus } from 'cosmjs-types/cosmos/staking/v1beta1/staking';
Expand All @@ -10,20 +12,22 @@ import 'mocha';

import { BaseProvider } from '../providers';

import { defaultSigningClientOptions, faucet } from './testutils.spec';

import Cosm from './index';

import { defaultSigningClientOptions, faucet } from './testutils.spec';

// const rpcUrl = 'https://testnet.rpc.orai.io';
// const rpcUrl = 'https://cosmos-testnet-rpc.allthatnode.com:26657';
const rpcUrl = 'https://osmosis-testnet-rpc.allthatnode.com:26657';
// const rpcUrl = "https://public-rpc1.stafihub.io";
const rpcUrl = "https://rpc-cosmoshub.keplr.app";
// const rpcUrl = "https://rpc-osmosis.keplr.app";
// const rpcUrl = 'https://osmosis-testnet-rpc.allthatnode.com:26657';
let provider;
let cosm;
let wallet: DirectSecp256k1HdWallet;
let client;
let account: AccountData;
// const prefix = 'cosmos';
const prefix = 'osmo';
const prefix = 'cosmos';
// const prefix = 'osmo';
let currentBlock;

let validatorAddr;
Expand All @@ -36,7 +40,7 @@ describe('Cosm test', async () => {
cosm = new Cosm(provider);

wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, {
prefix: prefix,
prefix: prefix
});

account = (await wallet.getAccounts())[0];
Expand Down Expand Up @@ -67,15 +71,15 @@ describe('Cosm test', async () => {
delegation = validatorDelegationsResponse.delegationResponses[0].delegation;
});
describe('test client', async () => {
it('should lookup MsgCustom success', function () {
it('should lookup MsgCustom success', function() {
const openedClient = client as unknown as PrivateSigningStargateClient;
expect(openedClient.registry.lookupType('/custom.MsgCustom')).is.equal(
MsgSend
);
});
});
describe('Get blockchain information', async () => {
it('should get blockchain info', async function () {
it('should get blockchain info', async function() {
const chainId = await provider.batchQueryClient.getChainId();
expect(chainId, `Chain id: ${chainId}`).is.not.empty;

Expand All @@ -93,7 +97,7 @@ describe('Cosm test', async () => {
describe('Test auth query', async () => {
it('should get bank accounts', async () => {
const _account = await cosm.cosmos.auth.query.Account({
address: delegation.delegatorAddress,
address: delegation.delegatorAddress
});
// console.log(accounts);
expect(_account).is.not.eq(undefined);
Expand Down Expand Up @@ -135,12 +139,12 @@ describe('Cosm test', async () => {
});

describe('Test staking query', async () => {
it('should get staking info', async function () {
it('should get staking info', async function() {
const staking = cosm.cosmos.staking;
const queryValidatorsResponse = await staking
.block(currentBlock - 100)
.query.Validators({
status: BondStatus[BondStatus.BOND_STATUS_BONDED],
status: BondStatus[BondStatus.BOND_STATUS_BONDED]
});
// console.log(queryValidatorsResponse);
expect(queryValidatorsResponse.validators).is.not.eq(undefined);
Expand All @@ -162,7 +166,7 @@ describe('Cosm test', async () => {
expect(queryParamsResponse).is.not.empty;

let queryHistoricalInfoResponse = await staking.query.HistoricalInfo({
height: Long.fromNumber(currentBlock),
height: Long.fromNumber(currentBlock)
});
// console.log(queryHistoricalInfoResponse);
expect(queryHistoricalInfoResponse).is.not.empty;
Expand All @@ -173,21 +177,21 @@ describe('Cosm test', async () => {

let queryDelegatorDelegationsResponse =
await staking.query.DelegatorDelegations({
delegatorAddr: account.address,
delegatorAddr: account.address
});
// console.log(queryDelegatorDelegationsResponse);
expect(queryDelegatorDelegationsResponse).is.not.empty;

let queryDelegatorUnbondingDelegationsResponse =
await staking.query.DelegatorUnbondingDelegations({
delegatorAddr: account.address,
delegatorAddr: account.address
});
// console.log(queryDelegatorUnbondingDelegationsResponse);
expect(queryDelegatorUnbondingDelegationsResponse).is.not.empty;

let queryDelegatorValidatorsResponse =
await staking.query.DelegatorValidators({
delegatorAddr: account.address,
delegatorAddr: account.address
});
// console.log(queryDelegatorValidatorsResponse);
expect(queryDelegatorValidatorsResponse).is.not.empty;
Expand All @@ -201,7 +205,7 @@ describe('Cosm test', async () => {

let queryDelegationResponse = await staking.query.Delegation({
delegatorAddr: delegation.delegatorAddress,
validatorAddr: delegation.validatorAddress,
validatorAddr: delegation.validatorAddress
});
// console.log(queryDelegationResponse);
expect(queryDelegationResponse).is.not.empty;
Expand All @@ -216,15 +220,15 @@ describe('Cosm test', async () => {
let queryDelegatorValidatorResponse =
await staking.query.DelegatorValidator({
delegatorAddr: delegation.delegatorAddress,
validatorAddr: delegation.validatorAddress,
validatorAddr: delegation.validatorAddress
});
// console.log(queryDelegatorValidatorResponse);
expect(queryDelegatorValidatorResponse).is.not.empty;
});
});

describe('Test distribution query', async () => {
it('should get distribution info', async function () {
it('should get distribution info', async function() {
const distribution = cosm.cosmos.distribution;

const validatorDelegationsResponse = await cosm.cosmos.staking
Expand All @@ -240,14 +244,14 @@ describe('Cosm test', async () => {

let queryValidatorOutstandingRewardsResponse =
await distribution.query.ValidatorOutstandingRewards({
validatorAddress: validatorAddr,
validatorAddress: validatorAddr
});
// console.log("queryValidatorOutstandingRewardsResponse");
// console.log(queryValidatorOutstandingRewardsResponse);

let queryValidatorCommissionResponse =
await distribution.query.ValidatorCommission({
validatorAddress: validatorAddr,
validatorAddress: validatorAddr
});
// console.log("queryValidatorCommissionResponse");
// console.log(queryValidatorCommissionResponse);
Expand All @@ -259,21 +263,21 @@ describe('Cosm test', async () => {

let queryDelegationTotalRewardsResponse =
await distribution.query.DelegationTotalRewards({
delegatorAddress: delegation.delegatorAddress,
delegatorAddress: delegation.delegatorAddress
});
// console.log("queryDelegationTotalRewardsResponse");
// console.log(queryDelegationTotalRewardsResponse);

let queryDelegatorValidatorsResponse =
await distribution.query.DelegatorValidators({
delegatorAddress: delegation.delegatorAddress,
delegatorAddress: delegation.delegatorAddress
});
// console.log("queryDelegatorValidatorsResponse");
// console.log(queryDelegatorValidatorsResponse);

let queryDelegatorWithdrawAddressResponse =
await distribution.query.DelegatorWithdrawAddress({
delegatorAddress: delegation.delegatorAddress,
delegatorAddress: delegation.delegatorAddress
});
// console.log("queryDelegatorWithdrawAddressResponse");
// console.log(queryDelegatorWithdrawAddressResponse);
Expand All @@ -285,4 +289,13 @@ describe('Cosm test', async () => {
// console.log(queryCommunityPoolResponse);
});
});

describe('Get mint denom', async () => {
it('should get mint', async function() {
let mint = cosm.cosmos.mint;
let mintDenomParams = await mint.query.Params({});
console.log(mintDenomParams.params);
});

});
});
19 changes: 4 additions & 15 deletions src/lib/cosm/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OfflineSigner } from '@cosmjs/proto-signing';
import { SigningStargateClientOptions } from '@cosmjs/stargate/build/signingstargateclient';
import Cosmos from '../cosmos';
import { provider } from '../providers';
import { Provider } from '../providers';
import { Utils } from '../utils';
import { Wallet } from '../wallet';
import { Wasm } from '../wasm';
Expand All @@ -19,33 +19,22 @@ export default class Cosm {
private offlineSinger: OfflineSigner;
private _wallet: Wallet;

get provider(): provider {
get provider(): Provider {
return this._provider;
}

private _provider: provider;
private _provider: Provider;
utils: Utils;
cosmos: Cosmos;
wasm: Wasm;

constructor(provider: provider) {
constructor(provider: Provider) {
this._provider = provider;
this.cosmos = new Cosmos(provider);
this.wasm = new Wasm(provider);
this.utils = new Utils();
}

async setSigner(
offlineSigner: OfflineSigner,
options?: SigningStargateClientOptions
) {
this.offlineSinger = offlineSigner;
this._wallet = await Wallet.connectWithSigner(
this._provider.rpcUrl,
offlineSigner,
options
);
}

async setWallet(wallet: Wallet) {
this._wallet = wallet;
Expand Down
15 changes: 15 additions & 0 deletions src/lib/cosm/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BaseProvider } from '../providers';
import Cosm from './index';

let rpcUrl = 'https://osmosis-rpc.polkachu.com';

async function test2() {
let provider = new BaseProvider();
await provider.connect(rpcUrl);
let cosm = new Cosm(provider);

let denom = await cosm.cosmos.mint.prefixService('osmosis').query.Params({});
console.log(denom);
}

test2();
11 changes: 7 additions & 4 deletions src/lib/cosmos/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { provider } from '../providers';
import { Provider } from '../providers';
import {
createProtobufRpcStateClient,
ProtobufRpcStateClient
} from '../queryclient/utils';
import { Wallet } from '../wallet';

export class App {
public prefixService: (prefix?: string) => this;

set wallet(value: Wallet) {
this._wallet = value;
}
Expand All @@ -23,16 +25,17 @@ export class App {

public block: (height?: number) => this;

private provider: provider;
private provider: Provider;
private readonly rpc: ProtobufRpcStateClient;
private _wallet: Wallet;

constructor(provider: provider) {
constructor(provider: Provider) {
this.provider = provider;
this.rpc = createProtobufRpcStateClient(
this.provider.batchQueryClient.getQueryClient()
);
this.block = this.rpc.block;
this.prefixService = this.rpc.prefixService;
this.message = {};
}

Expand All @@ -45,7 +48,7 @@ export class App {
}

setMessage(Message: any) {
this.message = Message(this.rpc);
this.message = new Message(this.rpc);
}

setWallet(wallet: Wallet) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cosmos/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as Protobuf from 'cosmjs-types/cosmos/auth/v1beta1/auth';
import { QueryClientImpl } from 'cosmjs-types/cosmos/auth/v1beta1/query';

import { provider } from '../providers';
import { Provider } from '../providers';

import { App } from './app';

export class Auth extends App {
public declare query: QueryClientImpl;
public protobuf = Protobuf;

constructor(provider: provider) {
constructor(provider: Provider) {
super(provider);
this.setQueryClient(QueryClientImpl);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cosmos/authz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Protobuf from 'cosmjs-types/cosmos/authz/v1beta1/authz';
import { QueryClientImpl } from 'cosmjs-types/cosmos/authz/v1beta1/query';
import { MsgClientImpl } from 'cosmjs-types/cosmos/authz/v1beta1/tx';

import { provider } from '../providers';
import { Provider } from '../providers';

import { App } from './app';

Expand All @@ -11,7 +11,7 @@ export class Authz extends App {
public declare message: MsgClientImpl;
public protobuf = Protobuf;

constructor(provider: provider) {
constructor(provider: Provider) {
super(provider);
this.setQueryClient(QueryClientImpl);
this.setMessage(MsgClientImpl)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cosmos/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Protobuf from 'cosmjs-types/cosmos/bank/v1beta1/bank';
import { QueryClientImpl } from 'cosmjs-types/cosmos/bank/v1beta1/query';
import { MsgClientImpl } from 'cosmjs-types/cosmos/bank/v1beta1/tx';

import { provider } from '../providers';
import { Provider } from '../providers';

import { App } from './app';

Expand All @@ -11,7 +11,7 @@ export class Bank extends App {
public declare message: MsgClientImpl;
public protobuf = Protobuf;

constructor(provider: provider) {
constructor(provider: Provider) {
super(provider);
this.setQueryClient(QueryClientImpl);
this.setMessage(MsgClientImpl);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cosmos/base.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as Protobuf from 'cosmjs-types/cosmos/base/v1beta1/coin';

import { provider } from '../providers';
import { Provider } from '../providers';

import { App } from './app';

export class Base extends App {
public protobuf = Protobuf;

constructor(provider: provider) {
constructor(provider: Provider) {
super(provider);
}
}
4 changes: 2 additions & 2 deletions src/lib/cosmos/capability.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as Protobuf from 'cosmjs-types/cosmos/capability/v1beta1/capability';

import { provider } from '../providers';
import { Provider } from '../providers';

import { App } from './app';

export class Capability extends App {
public protobuf = Protobuf;

constructor(provider: provider) {
constructor(provider: Provider) {
super(provider);
}
}
Loading

0 comments on commit 5711d92

Please sign in to comment.