Skip to content

Commit

Permalink
feat(web3.js): add support for get stake minimum delegation (#26682)
Browse files Browse the repository at this point in the history
  • Loading branch information
atharmohammad authored Jul 22, 2022
1 parent b9001a9 commit ce337e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
27 changes: 27 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ export type GetBlockConfig = {
maxSupportedTransactionVersion?: number;
};

/**
* Configuration object for changing `getStakeMinimumDelegation` query behavior
*/
export type GetStakeMinimumDelegationConfig = {
/** The level of commitment desired */
commitment?: Commitment;
};

/**
* Configuration object for changing `getBlockHeight` query behavior
*/
Expand Down Expand Up @@ -4317,6 +4325,25 @@ export class Connection {
}
}

/**
* get the stake minimum delegation
*/
async getStakeMinimumDelegation(
config?: GetStakeMinimumDelegationConfig,
): Promise<RpcResponseAndContext<number>> {
const {commitment, config: configArg} = extractCommitmentFromConfig(config);
const args = this._buildArgs([], commitment, 'base64', configArg);
const unsafeRes = await this._rpcRequest('getStakeMinimumDelegation', args);
const res = create(unsafeRes, jsonRpcResultAndContext(number()));
if ('error' in res) {
throw new SolanaJSONRPCError(
res.error,
`failed to get stake minimum delegation`,
);
}
return res.result;
}

/**
* Simulate a transaction
*/
Expand Down
4 changes: 4 additions & 0 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3804,6 +3804,10 @@ describe('Connection', function () {
}

if (process.env.TEST_LIVE) {
it('getStakeMinimumDelegation', async () => {
const {value} = await connection.getStakeMinimumDelegation();
expect(value).to.be.a('number');
});
it('simulate transaction with message', async () => {
connection._commitment = 'confirmed';

Expand Down
16 changes: 9 additions & 7 deletions web3.js/test/stake-program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,15 @@ describe('StakeProgram', () => {
if (process.env.TEST_LIVE) {
it('live staking actions', async () => {
const connection = new Connection(url, 'confirmed');
const SYSTEM_ACCOUNT_MIN_BALANCE =
await connection.getMinimumBalanceForRentExemption(0);
const STAKE_ACCOUNT_MIN_BALANCE =
await connection.getMinimumBalanceForRentExemption(StakeProgram.space);

// todo: use `Connection.getMinimumStakeDelegation` when implemented
const MIN_STAKE_DELEGATION = LAMPORTS_PER_SOL;
const [
SYSTEM_ACCOUNT_MIN_BALANCE,
STAKE_ACCOUNT_MIN_BALANCE,
{value: MIN_STAKE_DELEGATION},
] = await Promise.all([
connection.getMinimumBalanceForRentExemption(0),
connection.getMinimumBalanceForRentExemption(StakeProgram.space),
connection.getStakeMinimumDelegation(),
]);

const voteAccounts = await connection.getVoteAccounts();
const voteAccount = voteAccounts.current.concat(
Expand Down

0 comments on commit ce337e0

Please sign in to comment.