Skip to content

Commit

Permalink
add getFees with test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Jul 22, 2021
1 parent 5cabb5b commit 7ffe40a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
40 changes: 40 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,17 @@ export type PerfSample = {
samplePeriodSecs: number;
};

export type Fees = {
/** Recent blockhash */
blockhash: Blockhash;
/** Calculator for transaction fees */
feeCalculator: FeeCalculator;
/** @deprecated this value is inaccurate and should not be relied upon */
lastValidSlot: number;
/** last block height at which a blockhash will be valid */
lastValidBlockHeight: number;
}

function createRpcClient(
url: string,
useHttps: boolean,
Expand Down Expand Up @@ -1559,6 +1570,20 @@ const GetFeeCalculatorRpcResult = jsonRpcResultAndContext(
),
);

/**
* Expected JSON RPC response for the "getFees" message
*/
const GetFeesRpcResult = jsonRpcResultAndContext(
pick({
blockhash: string(),
feeCalculator: pick({
lamportsPerSignature: number(),
}),
lastValidSlot: number(),
lastValidBlockHeight: number(),
}),
);

/**
* Expected JSON RPC response for the "requestAirdrop" message
*/
Expand Down Expand Up @@ -2935,6 +2960,21 @@ export class Connection {
};
}

/**
* Fetch a recent block hash from the ledger, a fee schedule and the last valid block height the block hash is valid for
*/
async getFees(
commitment?: Commitment,
): Promise<RpcResponseAndContext<Fees>> {
const args = this._buildArgs([], commitment, undefined);
const unsafeRes = await this._rpcRequest('getFees', args);
const res = create(unsafeRes, GetFeesRpcResult);
if ('error' in res) {
throw new Error('failed to get fees: ' + res.error.message);
}
return res.result;
}

/**
* Fetch a recent blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}
Expand Down
21 changes: 21 additions & 0 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,27 @@ describe('Connection', () => {
expect(feeCalculator.lamportsPerSignature).to.eq(5000);
});

it('get fees', async () => {
await mockRpcResponse({
method: 'getFees',
params: [{commitment: 'confirmed'}],
value: {
blockhash: '57zQNBZBEiHsCZFqsaY6h176ioXy5MsSLmcvHkEyaLGy',
feeCalculator: {
lamportsPerSignature: 5000,
},
lastValidSlot: 5678,
lastValidBlockHeight: 1234,
},
withContext: true,
});

const fees = (await connection.getFees('confirmed')).value;
expect(fees.feeCalculator).not.to.be.null;
expect(fees.lastValidSlot).to.be.greaterThan(0);
expect(fees.lastValidBlockHeight).to.be.greaterThan(0);
})

it('get block time', async () => {
await mockRpcResponse({
method: 'getBlockTime',
Expand Down

0 comments on commit 7ffe40a

Please sign in to comment.