From ce337e009d7e206ef4e7cb76c7a62f5a2bd5a978 Mon Sep 17 00:00:00 2001 From: Athar Mohammad <56029409+atharmohammad@users.noreply.github.com> Date: Fri, 22 Jul 2022 13:32:49 +0530 Subject: [PATCH] feat(web3.js): add support for get stake minimum delegation (#26682) --- web3.js/src/connection.ts | 27 +++++++++++++++++++++++++++ web3.js/test/connection.test.ts | 4 ++++ web3.js/test/stake-program.test.ts | 16 +++++++++------- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 18a8b90cb8..f27a4b0a31 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -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 */ @@ -4317,6 +4325,25 @@ export class Connection { } } + /** + * get the stake minimum delegation + */ + async getStakeMinimumDelegation( + config?: GetStakeMinimumDelegationConfig, + ): Promise> { + 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 */ diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index c6e85a557e..7cc7c987ed 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -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'; diff --git a/web3.js/test/stake-program.test.ts b/web3.js/test/stake-program.test.ts index f7bfee3271..381ac20ff4 100644 --- a/web3.js/test/stake-program.test.ts +++ b/web3.js/test/stake-program.test.ts @@ -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(