Skip to content

Commit

Permalink
add function for get rewards params (#24)
Browse files Browse the repository at this point in the history
* add function for get rewards params

* add x/staking queries

* bump version

* run webpack
  • Loading branch information
rosepuppy authored Sep 13, 2023
1 parent 2eae4dc commit 401e6ef
Show file tree
Hide file tree
Showing 7 changed files with 3,956 additions and 76 deletions.
3,890 changes: 3,816 additions & 74 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions v4-client-js/__native__/__ios__/v4-native-client.js.map

Large diffs are not rendered by default.

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

try {
const rewardsParams = await client.get.getRewardsParams();
console.log('Rewards Params');
console.log(JSON.stringify(rewardsParams));
} 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
65 changes: 65 additions & 0 deletions v4-client-js/src/clients/modules/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
FeeTierModule,
PerpetualsModule,
PricesModule,
RewardsModule,
SubaccountsModule,
StakingModule,
StatsModule,
} from './proto-includes';
import { TendermintClient } from './tendermintClient';
Expand Down Expand Up @@ -194,6 +196,23 @@ 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(),
);

const data: Uint8Array = await this.sendQuery(
'/dydxprotocol.rewards.Query/Params',
requestData,
);
return RewardsModule.QueryParamsResponse.decode(data);
}

/**
* @description Get all Clob Pairs.
*
Expand Down Expand Up @@ -341,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
2 changes: 2 additions & 0 deletions v4-client-js/src/clients/modules/proto-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export * as SubaccountsModule
from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/subaccounts/query';
export * as FeeTierModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/feetiers/query';
export * as StatsModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/stats/query';
export * as RewardsModule from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/rewards/query';
export * as StakingModule from '@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/query';

export * from '@dydxprotocol/v4-proto/src/codegen/cosmos/base/abci/v1beta1/abci';
export * from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order';
Expand Down
45 changes: 45 additions & 0 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,3 +886,48 @@ export async function getOptimalIndexer(endpointUrlsAsJson: string): Promise<str
return wrappedError(error);
}
}

export async function getRewardsParams(): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectClient() first');
}
const rewardsParams = await globalThis.client?.validatorClient.get.getRewardsParams();
return encodeJson(rewardsParams);
} catch (e) {
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 401e6ef

Please sign in to comment.