fix: support `VersionedMessage` in `getFeeForMessage` (#28996)
This commit is contained in:
parent
16e2c5c0a2
commit
337621d031
|
@ -4154,10 +4154,10 @@ export class Connection {
|
||||||
* Fetch the fee for a message from the cluster, return with context
|
* Fetch the fee for a message from the cluster, return with context
|
||||||
*/
|
*/
|
||||||
async getFeeForMessage(
|
async getFeeForMessage(
|
||||||
message: Message,
|
message: VersionedMessage,
|
||||||
commitment?: Commitment,
|
commitment?: Commitment,
|
||||||
): Promise<RpcResponseAndContext<number>> {
|
): Promise<RpcResponseAndContext<number>> {
|
||||||
const wireMessage = message.serialize().toString('base64');
|
const wireMessage = toBuffer(message.serialize()).toString('base64');
|
||||||
const args = this._buildArgs([wireMessage], commitment);
|
const args = this._buildArgs([wireMessage], commitment);
|
||||||
const unsafeRes = await this._rpcRequest('getFeeForMessage', args);
|
const unsafeRes = await this._rpcRequest('getFeeForMessage', args);
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import {
|
||||||
NONCE_ACCOUNT_LENGTH,
|
NONCE_ACCOUNT_LENGTH,
|
||||||
} from '../src';
|
} from '../src';
|
||||||
import invariant from '../src/utils/assert';
|
import invariant from '../src/utils/assert';
|
||||||
|
import {toBuffer} from '../src/utils/to-buffer';
|
||||||
import {MOCK_PORT, url} from './url';
|
import {MOCK_PORT, url} from './url';
|
||||||
import {
|
import {
|
||||||
AccountInfo,
|
AccountInfo,
|
||||||
|
@ -60,6 +61,7 @@ import {
|
||||||
TransactionExpiredBlockheightExceededError,
|
TransactionExpiredBlockheightExceededError,
|
||||||
TransactionExpiredNonceInvalidError,
|
TransactionExpiredNonceInvalidError,
|
||||||
TransactionExpiredTimeoutError,
|
TransactionExpiredTimeoutError,
|
||||||
|
TransactionMessage,
|
||||||
} from '../src/transaction';
|
} from '../src/transaction';
|
||||||
import type {
|
import type {
|
||||||
SignatureStatus,
|
SignatureStatus,
|
||||||
|
@ -3673,7 +3675,7 @@ describe('Connection', function () {
|
||||||
expect(feeCalculator.lamportsPerSignature).to.eq(5000);
|
expect(feeCalculator.lamportsPerSignature).to.eq(5000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('get fee for message', async () => {
|
it('get fee for message (legacy)', async () => {
|
||||||
const accountFrom = Keypair.generate();
|
const accountFrom = Keypair.generate();
|
||||||
const accountTo = Keypair.generate();
|
const accountTo = Keypair.generate();
|
||||||
|
|
||||||
|
@ -3705,6 +3707,41 @@ describe('Connection', function () {
|
||||||
expect(fee).to.eq(5000);
|
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 () => {
|
it('get block time', async () => {
|
||||||
await mockRpcResponse({
|
await mockRpcResponse({
|
||||||
method: 'getBlockTime',
|
method: 'getBlockTime',
|
||||||
|
|
Loading…
Reference in New Issue