Skip to content

Commit

Permalink
feat: agave v2 rpc: replace getRecentBlockhash with `getLatestBlock…
Browse files Browse the repository at this point in the history
…hash`
  • Loading branch information
buffalojoec committed Nov 14, 2024
1 parent f96e4d2 commit c250727
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
50 changes: 29 additions & 21 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ import type {FeeCalculator} from './fee-calculator';
import type {TransactionSignature} from './transaction';
import type {CompiledInstruction} from './message';

const FEES_SYSVAR_PUBKEY = new PublicKey(
'SysvarFees111111111111111111111111111111111',
);

const PublicKeyFromString = coerce(
instance(PublicKey),
string(),
Expand Down Expand Up @@ -2597,20 +2601,6 @@ const GetParsedTransactionRpcResult = jsonRpcResult(
),
);

/**
* Expected JSON RPC response for the "getRecentBlockhash" message
*
* @deprecated Deprecated since RPC v1.8.0. Please use {@link GetLatestBlockhashRpcResult} instead.
*/
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
pick({
blockhash: string(),
feeCalculator: pick({
lamportsPerSignature: number(),
}),
}),
);

/**
* Expected JSON RPC response for the "getLatestBlockhash" message
*/
Expand Down Expand Up @@ -4565,13 +4555,31 @@ export class Connection {
feeCalculator: FeeCalculator;
}>
> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
const res = create(unsafeRes, GetRecentBlockhashAndContextRpcResult);
if ('error' in res) {
throw new SolanaJSONRPCError(res.error, 'failed to get recent blockhash');
}
return res.result;
// In order to serve the same response (with blockhash and fee calculator)
// as the now-removed `getRecentBlockhash` method, we'll have to make two
// parallel calls.

return await Promise.all([
this.getLatestBlockhashAndContext(commitment),
this.getAccountInfo(FEES_SYSVAR_PUBKEY, {commitment}),
]).then(([latestBlockhashAndContext, feesSysvar]) => {
const {
context,
value: {blockhash},
} = latestBlockhashAndContext;
const lamportsPerSignature = feesSysvar
? Number(feesSysvar.data.readBigUInt64LE())
: 5000;
return {
context,
value: {
blockhash,
feeCalculator: {
lamportsPerSignature,
},
},
};
});
}

/**
Expand Down
27 changes: 23 additions & 4 deletions test/mocks/rpc-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import {Connection, PublicKey, Transaction, Signer} from '../../src';
import invariant from '../../src/utils/assert';
import type {Commitment, HttpHeaders, RpcParams} from '../../src/connection';

const FEES_SYSVAR_PUBKEY = new PublicKey(
'SysvarFees111111111111111111111111111111111',
);

const SYSVAR_PUBKEY = new PublicKey(
'Sysvar1111111111111111111111111111111111111',
);

export const mockServer: mockttp.Mockttp | undefined =
process.env.TEST_LIVE === undefined ? mockttp.getLocal() : undefined;

Expand Down Expand Up @@ -160,13 +168,24 @@ const recentBlockhash = async ({
}

await mockRpcResponse({
method: 'getRecentBlockhash',
method: 'getLatestBlockhash',
params,
value: {
blockhash,
feeCalculator: {
lamportsPerSignature: 42,
},
lastValidBlockHeight: 200,
},
withContext: true,
});

await mockRpcResponse({
method: 'getAccountInfo',
params: [FEES_SYSVAR_PUBKEY.toBase58(), ...params],
value: {
executable: false,
owner: SYSVAR_PUBKEY,
lamports: 100_000,
data: ['ECcAAAAAAAA=', 'base64'], // 10_000
rentEpoch: 0,
},
withContext: true,
});
Expand Down

0 comments on commit c250727

Please sign in to comment.