Skip to content

Commit

Permalink
Gas price method added to provider (#521)
Browse files Browse the repository at this point in the history
gas price method added to the provider
  • Loading branch information
volovyks authored Mar 2, 2021
1 parent f9cc761 commit 7896384
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/providers/json-rpc-provider.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/providers/json-rpc-provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/providers/provider.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import depd from 'depd';
import {
Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, Finality,
BlockResult, ChunkId, ChunkResult, EpochValidatorInfo,
GenesisConfig, LightClientProof, LightClientProofRequest
GenesisConfig, LightClientProof, LightClientProofRequest, GasPrice
} from './provider';
import { Network } from '../utils/network';
import { ConnectionInfo, fetchJson } from '../utils/web';
Expand Down Expand Up @@ -209,4 +209,13 @@ export class JsonRpcProvider extends Provider {
}
return result;
}

/**
* Returns gas price for a specific block_height or block_hash.
* See [docs for more info](https://docs.near.org/docs/develop/front-end/rpc#gas-price)
* @param blockId Block hash or height, or null for latest.
*/
async gasPrice(blockId: BlockId | null): Promise<GasPrice> {
return await this.sendJsonRpc('gas_price', [blockId]);
}
}
5 changes: 5 additions & 0 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ export interface LightClientProofRequest {
receiver_id?: string;
}

export interface GasPrice {
gas_price: string;
}

export abstract class Provider {
abstract getNetwork(): Promise<Network>;
abstract status(): Promise<NodeStatusResult>;
Expand All @@ -250,6 +254,7 @@ export abstract class Provider {
abstract validators(blockId: BlockId): Promise<EpochValidatorInfo>;
abstract experimental_genesisConfig(): Promise<GenesisConfig>;
abstract lightClientProof(request: LightClientProofRequest): Promise<LightClientProof>;
abstract gasPrice(blockId: BlockId): Promise<GasPrice>
}

export function getTransactionLastResult(txResult: FinalExecutionOutcome): any {
Expand Down
14 changes: 14 additions & 0 deletions test/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ test('json rpc light client proof', async() => {

await expect(provider.lightClientProof(lightClientRequest)).rejects.toThrow(/.+ block .+ is ahead of head block .+/);
});

test('json rpc gas price', withProvider(async (provider) => {
let status = await provider.status();
let positiveIntegerRegex = /^[+]?\d+([.]\d+)?$/;

let response1 = await provider.gasPrice(status.sync_info.latest_block_height);
expect(response1.gas_price).toMatch(positiveIntegerRegex);

let response2 = await provider.gasPrice(status.sync_info.latest_block_hash);
expect(response2.gas_price).toMatch(positiveIntegerRegex);

let response3 = await provider.gasPrice();
expect(response3.gas_price).toMatch(positiveIntegerRegex);
}));

0 comments on commit 7896384

Please sign in to comment.