fix: support `VersionedMessage` in `getFeeForMessage` (#28996)

This commit is contained in:
Jon Cinque 2022-11-30 19:06:43 +01:00 committed by GitHub
parent 16e2c5c0a2
commit 337621d031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 3 deletions

View File

@ -4154,10 +4154,10 @@ export class Connection {
* Fetch the fee for a message from the cluster, return with context
*/
async getFeeForMessage(
message: Message,
message: VersionedMessage,
commitment?: Commitment,
): Promise<RpcResponseAndContext<number>> {
const wireMessage = message.serialize().toString('base64');
const wireMessage = toBuffer(message.serialize()).toString('base64');
const args = this._buildArgs([wireMessage], commitment);
const unsafeRes = await this._rpcRequest('getFeeForMessage', args);

View File

@ -24,6 +24,7 @@ import {
NONCE_ACCOUNT_LENGTH,
} from '../src';
import invariant from '../src/utils/assert';
import {toBuffer} from '../src/utils/to-buffer';
import {MOCK_PORT, url} from './url';
import {
AccountInfo,
@ -60,6 +61,7 @@ import {
TransactionExpiredBlockheightExceededError,
TransactionExpiredNonceInvalidError,
TransactionExpiredTimeoutError,
TransactionMessage,
} from '../src/transaction';
import type {
SignatureStatus,
@ -3673,7 +3675,7 @@ describe('Connection', function () {
expect(feeCalculator.lamportsPerSignature).to.eq(5000);
});
it('get fee for message', async () => {
it('get fee for message (legacy)', async () => {
const accountFrom = Keypair.generate();
const accountTo = Keypair.generate();
@ -3705,6 +3707,41 @@ describe('Connection', function () {
expect(fee).to.eq(5000);
});
it('get fee for message (v0)', async () => {
const accountFrom = Keypair.generate();
const accountTo = Keypair.generate();
const recentBlockhash = (await helpers.latestBlockhash({connection}))
.blockhash;
const instructions = [
SystemProgram.transfer({
fromPubkey: accountFrom.publicKey,
toPubkey: accountTo.publicKey,
lamports: 10,
}),
];
const messageV0 = new TransactionMessage({
payerKey: accountFrom.publicKey,
recentBlockhash,
instructions,
}).compileToV0Message();
await mockRpcResponse({
method: 'getFeeForMessage',
params: [
toBuffer(messageV0.serialize()).toString('base64'),
{commitment: 'confirmed'},
],
value: 5000,
withContext: true,
});
const fee = (await connection.getFeeForMessage(messageV0, 'confirmed'))
.value;
expect(fee).to.eq(5000);
});
it('get block time', async () => {
await mockRpcResponse({
method: 'getBlockTime',