Skip to content

Commit d396275

Browse files
authored
feat: add getGasPrice rpc provider method (#1056)
* feat: add getGasPrice rpc provider method * fix: rpc method name and test function * fix: change rpc provide method helper - change the helper method of `getL1GasPrice()` from `getBlockWithTxs()` to `getBlockWithTxHashes()` * fix: change test name to getL1GasPrice
1 parent dd7dc10 commit d396275

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

__tests__/rpcProvider.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ describeIfRpc('RPCProvider', () => {
7373
expect(typeof blockNumber).toBe('number');
7474
});
7575

76+
test('getL1GasPrice', async () => {
77+
const gasPrice = await rpcProvider.getL1GasPrice('latest');
78+
expect(typeof gasPrice).toBe('string');
79+
});
80+
7681
test('getStateUpdate', async () => {
7782
const stateUpdate = await rpcProvider.getBlockStateUpdate('latest');
7883
expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse');

src/provider/interface.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ export abstract class ProviderInterface {
7878
blockIdentifier?: BlockIdentifier
7979
): Promise<ContractClassResponse>;
8080

81+
/**
82+
* Gets the price of l1 gas in the block
83+
*
84+
* @param blockIdentifier block identifier
85+
* @returns gas price of the block
86+
*/
87+
public abstract getL1GasPrice(blockIdentifier: BlockIdentifier): Promise<string>;
88+
8189
/**
8290
* Returns the contract class hash in the given block for the contract deployed at the given address
8391
*

src/provider/rpc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ export class RpcProvider implements ProviderInterface {
100100
return this.channel.getBlockWithTxs(blockIdentifier);
101101
}
102102

103+
public async getL1GasPrice(blockIdentifier?: BlockIdentifier) {
104+
return this.channel
105+
.getBlockWithTxHashes(blockIdentifier)
106+
.then(this.responseParser.parseL1GasPriceResponse);
107+
}
108+
103109
public async getBlockWithReceipts(blockIdentifier?: BlockIdentifier) {
104110
if (this.channel instanceof RPC06.RpcChannel)
105111
throw new LibraryError('Unsupported method for RPC version');

src/utils/responseParser/rpc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { toBigInt } from '../num';
2020
import { isString } from '../shortString';
2121
import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark';
2222
import { ResponseParser } from '.';
23+
import { isString } from '../shortString';
24+
2325

2426
export class RPCResponseParser
2527
implements
@@ -121,4 +123,8 @@ export class RPCResponseParser
121123
abi: isString(res.abi) ? JSON.parse(res.abi) : res.abi,
122124
};
123125
}
126+
127+
public parseL1GasPriceResponse(res: BlockWithTxHashes): string {
128+
return res.l1_gas_price.price_in_wei;
129+
}
124130
}

0 commit comments

Comments
 (0)