feat: add getFeeCalculatorForBlockhash method

This commit is contained in:
Justin Starry
2020-06-04 18:12:59 +08:00
committed by Michael Vines
parent e876081d52
commit e622bb12b3
4 changed files with 119 additions and 2 deletions

4
web3.js/module.d.ts vendored
View File

@ -271,6 +271,10 @@ declare module '@solana/web3.js' {
getRecentBlockhashAndContext( getRecentBlockhashAndContext(
commitment?: Commitment, commitment?: Commitment,
): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>; ): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>;
getFeeCalculatorForBlockhash(
blockhash: Blockhash,
commitment?: Commitment,
): Promise<RpcResponseAndContext<FeeCalculator | null>>;
getRecentBlockhash( getRecentBlockhash(
commitment?: Commitment, commitment?: Commitment,
): Promise<BlockhashAndFeeCalculator>; ): Promise<BlockhashAndFeeCalculator>;

View File

@ -284,6 +284,10 @@ declare module '@solana/web3.js' {
getRecentBlockhashAndContext( getRecentBlockhashAndContext(
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>; ): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>;
getFeeCalculatorForBlockhash(
blockhash: Blockhash,
commitment: ?Commitment,
): Promise<RpcResponseAndContext<FeeCalculator | null>>;
getRecentBlockhash( getRecentBlockhash(
commitment: ?Commitment, commitment: ?Commitment,
): Promise<BlockhashAndFeeCalculator>; ): Promise<BlockhashAndFeeCalculator>;

View File

@ -783,6 +783,20 @@ const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
}), }),
); );
/**
* Expected JSON RPC response for the "getFeeCalculatorForBlockhash" message
*/
const GetFeeCalculatorRpcResult = jsonRpcResultAndContext(
struct.union([
'null',
struct({
feeCalculator: struct({
lamportsPerSignature: 'number',
}),
}),
]),
);
/** /**
* Expected JSON RPC response for the "requestAirdrop" message * Expected JSON RPC response for the "requestAirdrop" message
*/ */
@ -1506,6 +1520,31 @@ export class Connection {
return res.result; return res.result;
} }
/**
* Fetch the fee calculator for a recent blockhash from the cluster, return with context
*/
async getFeeCalculatorForBlockhash(
blockhash: Blockhash,
commitment: ?Commitment,
): Promise<RpcResponseAndContext<FeeCalculator | null>> {
const args = this._argsWithCommitment([blockhash], commitment);
const unsafeRes = await this._rpcRequest(
'getFeeCalculatorForBlockhash',
args,
);
const res = GetFeeCalculatorRpcResult(unsafeRes);
if (res.error) {
throw new Error('failed to get fee calculator: ' + res.error.message);
}
assert(typeof res.result !== 'undefined');
const {context, value} = res.result;
return {
context,
value: value && value.feeCalculator,
};
}
/** /**
* Fetch a recent blockhash from the cluster * Fetch a recent blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>} * @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}

View File

@ -203,6 +203,7 @@ test('get program accounts', async () => {
}, },
}, },
]); ]);
transaction = SystemProgram.assign({ transaction = SystemProgram.assign({
accountPubkey: account1.publicKey, accountPubkey: account1.publicKey,
programId: programId.publicKey, programId: programId.publicKey,
@ -213,8 +214,40 @@ test('get program accounts', async () => {
skipPreflight: true, skipPreflight: true,
}); });
mockGetRecentBlockhash('recent'); mockRpc.push([
const {feeCalculator} = await connection.getRecentBlockhash(); url,
{
method: 'getFeeCalculatorForBlockhash',
params: [transaction.recentBlockhash, {commitment: 'recent'}],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: {
feeCalculator: {
lamportsPerSignature: 42,
},
},
},
},
]);
if (transaction.recentBlockhash === null) {
expect(transaction.recentBlockhash).not.toBeNull();
return;
}
const feeCalculator = (
await connection.getFeeCalculatorForBlockhash(transaction.recentBlockhash)
).value;
if (feeCalculator === null) {
expect(feeCalculator).not.toBeNull();
return;
}
mockRpc.push([ mockRpc.push([
url, url,
@ -1077,6 +1110,43 @@ test('get recent blockhash', async () => {
} }
}); });
test('get fee calculator', async () => {
const connection = new Connection(url);
mockGetRecentBlockhash('recent');
const {blockhash} = await connection.getRecentBlockhash('recent');
mockRpc.push([
url,
{
method: 'getFeeCalculatorForBlockhash',
params: [blockhash, {commitment: 'recent'}],
},
{
error: null,
result: {
context: {
slot: 11,
},
value: {
feeCalculator: {
lamportsPerSignature: 5000,
},
},
},
},
]);
const feeCalculator = (
await connection.getFeeCalculatorForBlockhash(blockhash, 'recent')
).value;
if (feeCalculator === null) {
expect(feeCalculator).not.toBeNull();
return;
}
expect(feeCalculator.lamportsPerSignature).toEqual(5000);
});
test('get block time', async () => { test('get block time', async () => {
const connection = new Connection(url); const connection = new Connection(url);