Skip to content

Commit

Permalink
add x/staking queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rosepuppy committed Sep 7, 2023
1 parent 4efe64d commit c5f340e
Show file tree
Hide file tree
Showing 7 changed files with 3,591 additions and 11 deletions.
3,494 changes: 3,486 additions & 8 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion v4-client-js/__native__/__ios__/v4-native-client.js.map

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions v4-client-js/examples/validator_get_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ async function test(): Promise<void> {
} catch (error) {
console.log(JSON.stringify(error.message));
}

try {
const delegationsParams = await client.get.getDelegatorDelegations(DYDX_TEST_ADDRESS);
console.log('Rewards Params');
console.log(JSON.stringify(delegationsParams));
} catch (error) {
console.log(JSON.stringify(error.message));
}

try {
const unbondingDelegationsParams = await client
.get.getDelegatorUnbondingDelegations(DYDX_TEST_ADDRESS);
console.log('Rewards Params');
console.log(JSON.stringify(unbondingDelegationsParams));
} catch (error) {
console.log(JSON.stringify(error.message));
}
}

test().then(() => {
Expand Down
4 changes: 2 additions & 2 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ export const MAX_MEMO_CHARACTERS: number = 256;
// Querying
export const PAGE_REQUEST: PageRequest = {
key: new Uint8Array(),
offset: new Long(0),
limit: new Long(-1),
offset: Long.UZERO,
limit: Long.MAX_UNSIGNED_VALUE,
countTotal: true,
reverse: false,
};
Expand Down
52 changes: 52 additions & 0 deletions v4-client-js/src/clients/modules/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PricesModule,
RewardsModule,
SubaccountsModule,
StakingModule,
StatsModule,
} from './proto-includes';
import { TendermintClient } from './tendermintClient';
Expand Down Expand Up @@ -195,6 +196,11 @@ export class Get {
return SubaccountsModule.QuerySubaccountResponse.decode(data);
}

/**
* @description Get the params for the rewards module.
*
* @returns Params for the rewards module.
*/
async getRewardsParams(): Promise<RewardsModule.QueryParamsResponse> {
const requestData = Uint8Array.from(
RewardsModule.QueryParamsRequest.encode({}).finish(),
Expand Down Expand Up @@ -354,6 +360,52 @@ export class Get {
return ClobModule.QueryEquityTierLimitConfigurationResponse.decode(data);
}

/**
*
* @description Get all delegations from a delegator.
*
* @returns All delegations from a delegator.
*/
async getDelegatorDelegations(
delegatorAddr: string,
): Promise<StakingModule.QueryDelegatorDelegationsResponse> {
const requestData = Uint8Array.from(
StakingModule.QueryDelegatorDelegationsRequest.encode({
delegatorAddr,
pagination: PAGE_REQUEST,
}).finish(),
);

const data: Uint8Array = await this.sendQuery(
'/cosmos.staking.v1beta1.Query/DelegatorDelegations',
requestData,
);
return StakingModule.QueryDelegatorDelegationsResponse.decode(data);
}

/**
*
* @description Get all unbonding delegations from a delegator.
*
* @returns All unbonding delegations from a delegator.
*/
async getDelegatorUnbondingDelegations(
delegatorAddr: string,
): Promise<StakingModule.QueryDelegatorUnbondingDelegationsResponse> {
const requestData = Uint8Array.from(
StakingModule.QueryDelegatorUnbondingDelegationsRequest.encode({
delegatorAddr,
pagination: PAGE_REQUEST,
}).finish(),
);

const data: Uint8Array = await this.sendQuery(
'/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations',
requestData,
);
return StakingModule.QueryDelegatorUnbondingDelegationsResponse.decode(data);
}

private async sendQuery(requestUrl: string, requestData: Uint8Array): Promise<Uint8Array> {
return this.stargateQueryClient.queryUnverified(requestUrl, requestData);
}
Expand Down
1 change: 1 addition & 0 deletions v4-client-js/src/clients/modules/proto-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from '@dydxprotocol/dydxjs/src/codegen/dydxprotocol/subaccounts/query';
export * as FeeTierModule from '@dydxprotocol/dydxjs/src/codegen/dydxprotocol/feetiers/query';
export * as StatsModule from '@dydxprotocol/dydxjs/src/codegen/dydxprotocol/stats/query';
export * as RewardsModule from '@dydxprotocol/dydxjs/src/codegen/dydxprotocol/rewards/query';
export * as StakingModule from '@dydxprotocol/dydxjs/src/codegen/cosmos/staking/v1beta1/query';

export * from '@dydxprotocol/dydxjs/src/codegen/cosmos/base/abci/v1beta1/abci';
export * from '@dydxprotocol/dydxjs/src/codegen/dydxprotocol/clob/order';
Expand Down
32 changes: 32 additions & 0 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,35 @@ export async function getRewardsParams(): Promise<string> {
return wrappedError(e);
}
}

export async function getDelegatorDelegations(
delegatorAddress: string,
): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectClient() first');
}
const delegations = await globalThis
.client?.validatorClient.get.getDelegatorDelegations(delegatorAddress);
return encodeJson(delegations);
} catch (e) {
return wrappedError(e);
}
}

export async function getDelegatorUnbondingDelegations(
delegatorAddress: string,
): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectClient() first');
}
const delegations = await globalThis
.client?.validatorClient.get.getDelegatorUnbondingDelegations(delegatorAddress);
return encodeJson(delegations);
} catch (e) {
return wrappedError(e);
}
}

0 comments on commit c5f340e

Please sign in to comment.