Skip to content

Commit

Permalink
feat: add getGasPrice rpc provider method (starknet-io#1056)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
princeibs authored Apr 8, 2024
1 parent dd7dc10 commit d396275
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ describeIfRpc('RPCProvider', () => {
expect(typeof blockNumber).toBe('number');
});

test('getL1GasPrice', async () => {
const gasPrice = await rpcProvider.getL1GasPrice('latest');
expect(typeof gasPrice).toBe('string');
});

test('getStateUpdate', async () => {
const stateUpdate = await rpcProvider.getBlockStateUpdate('latest');
expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse');
Expand Down
8 changes: 8 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export abstract class ProviderInterface {
blockIdentifier?: BlockIdentifier
): Promise<ContractClassResponse>;

/**
* Gets the price of l1 gas in the block
*
* @param blockIdentifier block identifier
* @returns gas price of the block
*/
public abstract getL1GasPrice(blockIdentifier: BlockIdentifier): Promise<string>;

/**
* Returns the contract class hash in the given block for the contract deployed at the given address
*
Expand Down
6 changes: 6 additions & 0 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export class RpcProvider implements ProviderInterface {
return this.channel.getBlockWithTxs(blockIdentifier);
}

public async getL1GasPrice(blockIdentifier?: BlockIdentifier) {
return this.channel
.getBlockWithTxHashes(blockIdentifier)
.then(this.responseParser.parseL1GasPriceResponse);
}

public async getBlockWithReceipts(blockIdentifier?: BlockIdentifier) {
if (this.channel instanceof RPC06.RpcChannel)
throw new LibraryError('Unsupported method for RPC version');
Expand Down
6 changes: 6 additions & 0 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { toBigInt } from '../num';
import { isString } from '../shortString';
import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark';
import { ResponseParser } from '.';
import { isString } from '../shortString';


export class RPCResponseParser
implements
Expand Down Expand Up @@ -121,4 +123,8 @@ export class RPCResponseParser
abi: isString(res.abi) ? JSON.parse(res.abi) : res.abi,
};
}

public parseL1GasPriceResponse(res: BlockWithTxHashes): string {
return res.l1_gas_price.price_in_wei;
}
}

0 comments on commit d396275

Please sign in to comment.