From a6ad79abf187f7f936c317b072d361ebb9f04a25 Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Mon, 18 Nov 2024 20:49:11 +0900 Subject: [PATCH] feat: agave v2 rpc: replace `getStakeActivation` with rpc-client-extension --- package.json | 5 +++-- pnpm-lock.yaml | 40 ++++++++++++++++++++++++++++++++++++++- src/connection.ts | 48 +++++++++++++++-------------------------------- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 0f41acfce7a9..052dc215b51c 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "test:unit": "cross-env NODE_ENV=test NODE_OPTIONS='--import tsx' mocha './test/**/*.test.ts'" }, "dependencies": { + "@anza-xyz/solana-rpc-get-stake-activation": "^1.0.1", "@babel/runtime": "^7.25.0", "@noble/curves": "^1.4.2", "@noble/hashes": "^1.4.0", @@ -122,9 +123,9 @@ "sinon": "^19.0.2", "sinon-chai": "^4.0.0", "start-server-and-test": "^2.0.4", - "typescript": "^5.5.4", "tslib": "^2.6.3", "tsx": "^4.16.2", - "typedoc": "^0.26.5" + "typedoc": "^0.26.5", + "typescript": "^5.5.4" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a33ce338aac3..4bc02f908c0c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@anza-xyz/solana-rpc-get-stake-activation': + specifier: ^1.0.1 + version: 1.0.1(bufferutil@4.0.8)(utf-8-validate@5.0.10) '@babel/runtime': specifier: ^7.25.0 version: 7.26.0 @@ -220,6 +223,9 @@ packages: resolution: {integrity: sha512-lFMjJTrFL3j7L9yBxwYfCq2k6qqwHyzuUl/XBnif78PWTJYyL/dfowQHWE3sp6U6ZzqWiiIZnpTMO96zhkjwtg==} engines: {node: '>=6.0.0'} + '@anza-xyz/solana-rpc-get-stake-activation@1.0.1': + resolution: {integrity: sha512-g5JiDnk6pb/8kzMiPJti2aM2DZFO6SjYaDpuOUgvPUcTWPR8oKiHvFOfG6Ixbu74g0nqAJP0IrxKUS5UI8q4OQ==} + '@babel/code-frame@7.24.7': resolution: {integrity: sha512-BcYH1CVJBO9tvyIZ2jVeXgSIMvGZ2FDRvDdOIVQyuklNKSsx+eppDEBq/g47Ayw+RqNFE+URvOShmf+f/qwAlA==} engines: {node: '>=6.9.0'} @@ -1401,6 +1407,9 @@ packages: resolution: {integrity: sha512-E1ImOIAD1tBZFRdjeM4/pzTiTApC0AOBGwyAMS4fwIodCWArzJ3DWdoh8cKxeFM2fElkxBh2Aqts1BPC373rHA==} engines: {node: '>=5.10'} + '@solana/web3.js@1.95.4': + resolution: {integrity: sha512-sdewnNEA42ZSMxqkzdwEWi6fDgzwtJHaQa5ndUGEJYtoOnM6X5cvPmjoTUp7/k7bRrVAxfBgDnvQQHD6yhlLYw==} + '@swc/helpers@0.5.11': resolution: {integrity: sha512-YNlnKRWF2sVojTpIyzwou9XoTNbzbzONwRhOoniEioF1AtaitTvVZblaQRrAzChWQ1bLYyYSWzM18y4WwgzJ+A==} @@ -2366,7 +2375,6 @@ packages: eslint@8.57.0: resolution: {integrity: sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} - deprecated: This version is no longer supported. Please see https://eslint.org/version-support for other options. hasBin: true esm@3.2.25: @@ -4569,6 +4577,14 @@ snapshots: '@jridgewell/gen-mapping': 0.3.5 '@jridgewell/trace-mapping': 0.3.25 + '@anza-xyz/solana-rpc-get-stake-activation@1.0.1(bufferutil@4.0.8)(utf-8-validate@5.0.10)': + dependencies: + '@solana/web3.js': 1.95.4(bufferutil@4.0.8)(utf-8-validate@5.0.10) + transitivePeerDependencies: + - bufferutil + - encoding + - utf-8-validate + '@babel/code-frame@7.24.7': dependencies: '@babel/highlight': 7.24.7 @@ -5933,6 +5949,28 @@ snapshots: dependencies: buffer: 6.0.3 + '@solana/web3.js@1.95.4(bufferutil@4.0.8)(utf-8-validate@5.0.10)': + dependencies: + '@babel/runtime': 7.25.7 + '@noble/curves': 1.6.0 + '@noble/hashes': 1.5.0 + '@solana/buffer-layout': 4.0.1 + agentkeepalive: 4.5.0 + bigint-buffer: 1.1.5 + bn.js: 5.2.1 + borsh: 0.7.0 + bs58: 4.0.1 + buffer: 6.0.3 + fast-stable-stringify: 1.0.0 + jayson: 4.1.2(bufferutil@4.0.8)(utf-8-validate@5.0.10) + node-fetch: 2.7.0 + rpc-websockets: 9.0.4 + superstruct: 2.0.2 + transitivePeerDependencies: + - bufferutil + - encoding + - utf-8-validate + '@swc/helpers@0.5.11': dependencies: tslib: 2.8.1 diff --git a/src/connection.ts b/src/connection.ts index 40de1efbc8e2..b43803a86e10 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -28,6 +28,7 @@ import { import type {Struct} from 'superstruct'; import RpcClient from 'jayson/lib/client/browser'; import {JSONRPCError} from 'jayson'; +import {getStakeActivation} from '@anza-xyz/solana-rpc-get-stake-activation'; import {EpochSchedule} from './epoch-schedule'; import {SendTransactionError, SolanaJSONRPCError} from './errors'; @@ -1968,20 +1969,6 @@ const KeyedParsedAccountInfoResult = pick({ account: ParsedAccountInfoResult, }); -/** - * @internal - */ -const StakeActivationResult = pick({ - state: union([ - literal('active'), - literal('inactive'), - literal('activating'), - literal('deactivating'), - ]), - active: number(), - inactive: number(), -}); - /** * Expected JSON RPC response for the "getConfirmedSignaturesForAddress2" message */ @@ -3692,27 +3679,22 @@ export class Connection { commitmentOrConfig?: Commitment | GetStakeActivationConfig, epoch?: number, ): Promise { - const {commitment, config} = - extractCommitmentFromConfig(commitmentOrConfig); - const args = this._buildArgs( - [publicKey.toBase58()], - commitment, - undefined /* encoding */, - { - ...config, - epoch: epoch != null ? epoch : config?.epoch, - }, - ); - - const unsafeRes = await this._rpcRequest('getStakeActivation', args); - const res = create(unsafeRes, jsonRpcResult(StakeActivationResult)); - if ('error' in res) { - throw new SolanaJSONRPCError( - res.error, - `failed to get Stake Activation ${publicKey.toBase58()}`, + if (epoch) { + throw new Error( + 'The capability to fetch stake activation for a specific `epoch` is not longer offered by the network.', ); } - return res.result; + + const {status, active, inactive} = await getStakeActivation( + this, + publicKey, + ); + + return { + state: status as StakeActivationData['state'], + active: Number(active), + inactive: Number(inactive), + }; } /**