Skip to content

Commit

Permalink
feat: adding proposals query
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Feb 27, 2024
1 parent 628e399 commit a18b87f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 35 deletions.
10 changes: 9 additions & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ export * from "sdk";

// Export types
export type { Address, ShieldedKeys, TransparentKeys } from "keys";
export type { Bonds, Delegation, Unbonds } from "rpc";
export type {
Balance,
Bonds,
DelegationTotals,
DelegatorsVotes,
StakingPositions,
StakingTotals,
Unbonds,
} from "rpc";
export type { EncodedTx, SignedTx } from "tx";
129 changes: 100 additions & 29 deletions packages/sdk/src/rpc/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Query as QueryWasm, Sdk as SdkWasm } from "@namada/shared";
import {
Query as QueryWasm,
Sdk as SdkWasm,
TransferToEthereum,
} from "@namada/shared";
import { SignedTx } from "tx/types";

import { deserialize } from "@dao-xyz/borsh";
import { Proposal, Proposals } from "@namada/shared/src/borsh-schemas";
import {
Balance,
BondsResponse,
Delegation,
DelegationResponse,
DelegationTotals,
DelegatorsVotes,
GasCosts,
StakingPositions,
StakingTotals,
StakingTotalsResponse,
UnbondsResponse,
} from "./types";
} from "rpc/types";

/**
* API for executing RPC requests with Namada
Expand All @@ -24,66 +35,108 @@ export class Rpc {
/**
* Query balances from chain
* @async
* @param {string} owner
* @param {string[]} tokens
* @returns {string[][]} [address][amount]
* @param {string} owner - Owner address
* @param {string[]} tokens - Array of token addresses
* @returns {Balance} [[tokenAddress, amount]]
*/
async queryBalance(owner: string, tokens: string[]): Promise<string[][]> {
async queryBalance(owner: string, tokens: string[]): Promise<Balance> {
return await this.query.query_balance(owner, tokens);
}

/**
* Query native token from chain
* @async
* @returns {string}
* @returns {string} Address of native token
*/
async queryNativeToken(): Promise<string> {
return await this.query.query_native_token();
}

/**
* Query public key
* Return string of public key if it has been revealed on chain, otherwise, return null
* @async
* @returns {string}
* @param {string} address
* @returns {string|null} String of public key if found
*/
async queryPublicKey(owner: string): Promise<string> {
return await this.query.query_public_key(owner);
async queryPublicKey(address: string): Promise<string | null> {
const pk = await this.query.query_public_key(address);
return pk || null;
}

/**
* Query all validator addresses
* @async
* @returns {string[]} All validator addresses
* @returns {string[]} Array of all validator addresses
*/
async queryAllValidators(): Promise<string[]> {
return await this.query.query_all_validator_addresses();
}

/**
* Query delegations by owner addresses
* Query Proposals
* @async
* @returns {Proposal[]}
*/
async queryProposals(): Promise<Proposal[]> {
const serializedProposals = await this.query.query_proposals();
const { proposals } = deserialize(serializedProposals, Proposals);
return proposals;
}

/**
* Query total delegations
* @async
* @param {string[]} owners - Array of owner addresses
* @param {bigint} [epoch]
*/
async queryTotalDelegations(
owners: string[],
epoch?: bigint
): Promise<DelegationTotals> {
return await this.query.get_total_delegations(owners, epoch);
}

/**
* Query delegators votes
* @async
* @param {bigint} proposalId
*/
async queryDelegatorsVotes(proposalId: bigint): Promise<DelegatorsVotes> {
return await this.query.delegators_votes(proposalId);
}

/**
* Query staking totals by owner addresses
* @async
* @param {string[]} owners
* @returns {Delegation[]}
* @param {string[]} owners - Array of owner addresses
* @returns {StakingTotals[]}
*/
async queryDelegations(owners: string[]): Promise<Delegation[]> {
const delegations = await this.query.query_my_validators(owners);
return delegations.map((delegation: DelegationResponse) => {
const [owner, validator, totalBonds, totalUnbonds, withdrawable] =
delegation;
return {
async queryStakingTotals(owners: string[]): Promise<StakingTotals[]> {
const stakingAmounts = await this.query.query_my_validators(owners);
return stakingAmounts.map(
([
owner,
validator,
totalBonds,
totalUnbonds,
bonds,
unbonds,
withdrawable,
};
});
]: StakingTotalsResponse) => {
return {
owner,
validator,
bonds,
unbonds,
withdrawable,
};
}
);
}

/**
* Query staking positions
* Query bond and unbond details by owner addresses
* @async
* @param {string[]} owners
* @param {string[]} owners - Array of owner addresses
* @returns {StakingPositions}
*/
async queryStakingPositions(owners: string[]): Promise<StakingPositions> {
Expand Down Expand Up @@ -117,13 +170,31 @@ export class Rpc {

/**
* Query total bonds by owner address
* @param {string} owner
* @param {string} owner - Owner address
* @returns {number}
*/
async queryTotalBonds(owner: string): Promise<number> {
return await this.query.query_total_bonds(owner);
}

/**
* Query pending transactions in the signed bridge pool
* @async
* @param {string[]} owners - Array of owner addresses
* @returns {TransferToEthereum[]}
*/
async querySignedBridgePool(owners: string[]): Promise<TransferToEthereum[]> {
return await this.query.query_signed_bridge_pool(owners);
}

/**
* Query gas costs
* @returns {GasCosts[]}
*/
async queryGasCosts(): Promise<GasCosts> {
return await this.query.query_gas_costs();
}

/**
* Broadcast a Tx to the ledger
* @async
Expand Down
46 changes: 42 additions & 4 deletions packages/sdk/src/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
export type DelegationResponse = [string, string, string, string, string];
/**
* StakingTotalsResponse
* [owner, validator, bonds, unbonds, withdrawable]
*/
export type StakingTotalsResponse = [string, string, string, string, string];

/**
* BondsResponse
* [owner, validator, amount, startEpoch]
*/
export type BondsResponse = [string, string, string, string];

/**
* UnbondsResponse:
* [owner, validator, amount, startEpoch, withdrawableEpoch]
*/
export type UnbondsResponse = [string, string, string, string, string];

export type Delegation = {
export type StakingTotals = {
owner: string;
validator: string;
totalBonds: string;
totalUnbonds: string;
bonds: string;
unbonds: string;
withdrawable: string;
};

Expand All @@ -29,3 +43,27 @@ export type StakingPositions = {
bonds: Bonds;
unbonds: Unbonds;
};

/**
* DelegationTotals
* Record<address, totalDelegations>
*/
export type DelegationTotals = Record<string, number>;

/**
* Delegator Votes
* Record<address, boolean>
*/
export type DelegatorsVotes = Record<string, boolean>;

/**
* GasCosts
* [[tokenAddress, gasCost]]
*/
export type GasCosts = [[string, string]];

/**
* Balance
* [[tokenAddress, amount]]
*/
export type Balance = [[string, number]];
3 changes: 2 additions & 1 deletion packages/shared/lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ impl Query {
}

/// Gets all delegations for every provided address.
/// Returns a tuple of (owner_address, validator_address, total_bonds)
/// Returns a tuple of:
/// (owner_address, validator_address, total_bonds, total_unbonds, withdrawable)
///
/// # Arguments
///
Expand Down

0 comments on commit a18b87f

Please sign in to comment.