Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web3.js): add support for get stake minimum delegation #26682

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -4304,6 +4312,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