Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

fix: support VersionedMessage in getFeeForMessage #28996

Merged
merged 1 commit into from
Nov 30, 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
4 changes: 2 additions & 2 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
39 changes: 38 additions & 1 deletion web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,6 +61,7 @@ import {
TransactionExpiredBlockheightExceededError,
TransactionExpiredNonceInvalidError,
TransactionExpiredTimeoutError,
TransactionMessage,
} from '../src/transaction';
import type {
SignatureStatus,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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',
Expand Down