Skip to content

Commit

Permalink
fix: support VersionedMessage in getFeeForMessage (#28996)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Nov 30, 2022
1 parent 16e2c5c commit 337621d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions web3.js/src/connection.ts
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
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

0 comments on commit 337621d

Please sign in to comment.