Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getGasPrice rpc provider method #1056

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}