From dd7aa8f8603c5a73ec3e0b682ce9b2a639b8b200 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Tue, 2 Apr 2024 19:32:28 +0100 Subject: [PATCH 1/4] feat: add getGasPrice rpc provider method --- __tests__/rpcProvider.test.ts | 5 +++++ src/provider/interface.ts | 8 ++++++++ src/provider/rpc.ts | 6 ++++++ src/utils/responseParser/rpc.ts | 5 +++++ 4 files changed, 24 insertions(+) diff --git a/__tests__/rpcProvider.test.ts b/__tests__/rpcProvider.test.ts index f5a5cf289..eca87a2a3 100644 --- a/__tests__/rpcProvider.test.ts +++ b/__tests__/rpcProvider.test.ts @@ -69,6 +69,11 @@ describeIfRpc('RPCProvider', () => { expect(typeof blockNumber).toBe('number'); }); + test('getGasprice', async () => { + const gasPrice = await rpcProvider.getGasPrice(967555); + expect(gasPrice).toBe(0x4a817c800); + }); + test('getStateUpdate', async () => { const stateUpdate = await rpcProvider.getBlockStateUpdate('latest'); expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse'); diff --git a/src/provider/interface.ts b/src/provider/interface.ts index b9026ab91..d4c9fdd41 100644 --- a/src/provider/interface.ts +++ b/src/provider/interface.ts @@ -78,6 +78,14 @@ export abstract class ProviderInterface { blockIdentifier?: BlockIdentifier ): Promise; + /** + * Gets the price of l1 gas in the block + * + * @param blockIdentifier block identifier + * @returns gas price of the block + */ + public abstract getGasPrice(blockIdentifier: BlockIdentifier): Promise; + /** * Returns the contract class hash in the given block for the contract deployed at the given address * diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index ee2846992..f117cd11c 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -96,6 +96,12 @@ export class RpcProvider implements ProviderInterface { return this.channel.getBlockWithTxs(blockIdentifier); } + public async getGasPrice(blockIdentifier?: BlockIdentifier) { + return this.channel + .getBlockWithTxs(blockIdentifier) + .then(this.responseParser.parseGasPriceResponse); + } + public async getBlockWithReceipts(blockIdentifier?: BlockIdentifier) { if (this.channel instanceof RPC06.RpcChannel) throw new LibraryError('Unsupported method for RPC version'); diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 64badba28..f2e4d4d1f 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -18,6 +18,7 @@ import { import { toBigInt } from '../num'; import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark'; import { ResponseParser } from '.'; +import { BlockWithTxs } from '../../types/api/rpcspec_0_6/nonspec'; export class RPCResponseParser implements @@ -97,4 +98,8 @@ export class RPCResponseParser abi: typeof res.abi === 'string' ? JSON.parse(res.abi) : res.abi, }; } + + public parseGasPriceResponse(res: BlockWithTxs): string { + return res.l1_gas_price.price_in_wei; + } } From 4188cc64e5808ada7ad8eba161ed20ea907acaa0 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Fri, 5 Apr 2024 10:25:08 +0100 Subject: [PATCH 2/4] fix: rpc method name and test function --- __tests__/rpcProvider.test.ts | 4 ++-- src/provider/interface.ts | 2 +- src/provider/rpc.ts | 4 ++-- src/utils/responseParser/rpc.ts | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/__tests__/rpcProvider.test.ts b/__tests__/rpcProvider.test.ts index 20157f61d..fefb2d937 100644 --- a/__tests__/rpcProvider.test.ts +++ b/__tests__/rpcProvider.test.ts @@ -72,8 +72,8 @@ describeIfRpc('RPCProvider', () => { }); test('getGasprice', async () => { - const gasPrice = await rpcProvider.getGasPrice(967555); - expect(gasPrice).toBe(0x4a817c800); + const gasPrice = await rpcProvider.getL1GasPrice('latest'); + expect(typeof gasPrice).toBe('string'); }); test('getStateUpdate', async () => { diff --git a/src/provider/interface.ts b/src/provider/interface.ts index fc4e1c712..8903bbd98 100644 --- a/src/provider/interface.ts +++ b/src/provider/interface.ts @@ -84,7 +84,7 @@ export abstract class ProviderInterface { * @param blockIdentifier block identifier * @returns gas price of the block */ - public abstract getGasPrice(blockIdentifier: BlockIdentifier): Promise; + public abstract getL1GasPrice(blockIdentifier: BlockIdentifier): Promise; /** * Returns the contract class hash in the given block for the contract deployed at the given address diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index f4ddb9b5e..2c5d2b9a9 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -100,10 +100,10 @@ export class RpcProvider implements ProviderInterface { return this.channel.getBlockWithTxs(blockIdentifier); } - public async getGasPrice(blockIdentifier?: BlockIdentifier) { + public async getL1GasPrice(blockIdentifier?: BlockIdentifier) { return this.channel .getBlockWithTxs(blockIdentifier) - .then(this.responseParser.parseGasPriceResponse); + .then(this.responseParser.parseL1GasPriceResponse); } public async getBlockWithReceipts(blockIdentifier?: BlockIdentifier) { diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 4f7e2792a..2739c1ee0 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -119,7 +119,7 @@ export class RPCResponseParser }; } - public parseGasPriceResponse(res: BlockWithTxs): string { + public parseL1GasPriceResponse(res: BlockWithTxs): string { return res.l1_gas_price.price_in_wei; } } From 9c9be86e1b91a706d35b3ca20d68d9862390963a Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Fri, 5 Apr 2024 16:41:32 +0100 Subject: [PATCH 3/4] fix: change rpc provide method helper - change the helper method of `getL1GasPrice()` from `getBlockWithTxs()` to `getBlockWithTxHashes()` --- src/provider/rpc.ts | 2 +- src/utils/responseParser/rpc.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 2c5d2b9a9..68b2f5396 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -102,7 +102,7 @@ export class RpcProvider implements ProviderInterface { public async getL1GasPrice(blockIdentifier?: BlockIdentifier) { return this.channel - .getBlockWithTxs(blockIdentifier) + .getBlockWithTxHashes(blockIdentifier) .then(this.responseParser.parseL1GasPriceResponse); } diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index c5595c5e3..acce51203 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -20,7 +20,6 @@ import { toBigInt } from '../num'; import { isString } from '../shortString'; import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark'; import { ResponseParser } from '.'; -import { BlockWithTxs } from '../../types/api/rpcspec_0_6/nonspec'; import { isString } from '../shortString'; @@ -125,7 +124,7 @@ export class RPCResponseParser }; } - public parseL1GasPriceResponse(res: BlockWithTxs): string { + public parseL1GasPriceResponse(res: BlockWithTxHashes): string { return res.l1_gas_price.price_in_wei; } } From 370c771082159458a8b33cbbbb464a2796bb1aa1 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Fri, 5 Apr 2024 16:57:50 +0100 Subject: [PATCH 4/4] fix: change test name to getL1GasPrice --- __tests__/rpcProvider.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/rpcProvider.test.ts b/__tests__/rpcProvider.test.ts index 90efa42a4..6c2fe6835 100644 --- a/__tests__/rpcProvider.test.ts +++ b/__tests__/rpcProvider.test.ts @@ -73,7 +73,7 @@ describeIfRpc('RPCProvider', () => { expect(typeof blockNumber).toBe('number'); }); - test('getGasprice', async () => { + test('getL1GasPrice', async () => { const gasPrice = await rpcProvider.getL1GasPrice('latest'); expect(typeof gasPrice).toBe('string'); });