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 2 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 @@ -71,6 +71,11 @@ describeIfRpc('RPCProvider', () => {
expect(typeof blockNumber).toBe('number');
});

test('getGasprice', async () => {
const gasPrice = await rpcProvider.getGasPrice(967555);
princeibs marked this conversation as resolved.
Show resolved Hide resolved
expect(gasPrice).toBe(0x4a817c800);
princeibs marked this conversation as resolved.
Show resolved Hide resolved
});

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 getGasPrice(blockIdentifier: BlockIdentifier): Promise<string>;
princeibs marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 getGasPrice(blockIdentifier?: BlockIdentifier) {
return this.channel
.getBlockWithTxs(blockIdentifier)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getBlockWithTxHashes should generate less amount of data to receive, and will nevertheless return the L1 gas price

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the response from BlastAPI but I couldn't find details of l1 gas price:
image

Copy link
Collaborator

@PhilippeR26 PhilippeR26 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my side, with

const bl=await provider.getBlockWithTxHashes();

https://starknet-sepolia.public.blastapi.io/rpc/v0_7

{
  l1_da_mode: 'BLOB',
  l1_data_gas_price: { price_in_fri: '0x91acb74c302d3', price_in_wei: '0x14f44f92900' },
  l1_gas_price: { price_in_fri: '0x2d7f150f9892', price_in_wei: '0x68b5b1857' },
  parent_hash: '0x16cc570bf6841d72a650ac06eefe965f8325c01d80df0ed5de2d3b03d62c6ec',
  sequencer_address: '0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8',
  starknet_version: '0.13.1',
  status: 'PENDING',
  timestamp: 1712319031,
  transactions: [

https://starknet-sepolia.public.blastapi.io/rpc/v0_6

{
  l1_gas_price: { price_in_fri: '0x2782d833b2c3', price_in_wei: '0x5b07580b4' },
  parent_hash: '0x2ef3f69a56a90ca2864d4679aa64e857b72b7c9702fb3e3caaf07d863da79d1',
  sequencer_address: '0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8',
  starknet_version: '0.13.1',
  status: 'PENDING',
  timestamp: 1712319274,
  transactions: [

Same with mainnet, same with Nethermind.
So, it should work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your problem is probably related to a connection to rpc v0.4 or v0.5 (as it's not described in your url)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅. What caused the problem earlier was because I was using older RPC version. @PhilippeR26

.then(this.responseParser.parseGasPriceResponse);
}

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

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

public parseGasPriceResponse(res: BlockWithTxs): string {
princeibs marked this conversation as resolved.
Show resolved Hide resolved
return res.l1_gas_price.price_in_wei;
}
}
Loading