Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #83 from Flouse/eth_gasPrice
Browse files Browse the repository at this point in the history
feat: implement eth_gasPrice RPC
  • Loading branch information
classicalliu authored Dec 3, 2021
2 parents 8071a19 + 0483ccc commit 40edda3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
32 changes: 29 additions & 3 deletions packages/api-server/src/methods/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
import { middleware, validators } from "../validator";
import { FilterFlag, FilterObject } from "../../cache/types";
import { utils, HexNumber, Hash, Address, HexString } from "@ckb-lumos/base";
import { RawL2Transaction, RunResult } from "@godwoken-web3/godwoken";
import {
FeeConfig,
RawL2Transaction,
RunResult,
} from "@godwoken-web3/godwoken";
import { Script } from "@ckb-lumos/base";
import {
CKB_SUDT_ID,
Expand Down Expand Up @@ -90,6 +94,7 @@ export class Eth {
validators.address,
validators.blockParameter,
]);
// this.gasPrice = middleware(this.gasPrice.bind(this), 0);
this.getStorageAt = middleware(this.getStorageAt.bind(this), 3, [
validators.address,
validators.hexNumber,
Expand Down Expand Up @@ -254,8 +259,29 @@ export class Eth {
return "0x0";
}

async gasPrice(args: []): Promise<HexNumber> {
return "0x1";
/**
* Get the current price per gas in shannon.
* 1 CKB = 100,000,000 Shannons
*
* Note: Polyjuice use CKB to pay fee by default.
* @returns gasPrice (unit: shannon)
*/
async gasPrice(_args: []): Promise<HexNumber> {
try {
let GodwokenFeeConfig: FeeConfig = await this.rpc.getFeeConfig();
if (
!GodwokenFeeConfig?.fee_rates ||
Object.keys(GodwokenFeeConfig.fee_rates).length === 0
) {
// default gasPrice = 0, if GodwokenFeeConfig.fee_rates is empty
return "0x0";
}
// Note: Polyjuice use CKB to pay fee by default.
const CKB_SUDT_ID = "0x1";
return GodwokenFeeConfig.fee_rates[CKB_SUDT_ID];
} catch (error: any) {
throw new Web3Error(error.message);
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/godwoken/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RunResult,
U128,
U32,
FeeConfig,
} from "./types";
import { SerializeL2Transaction, SerializeRawL2Transaction } from "../schemas";
import {
Expand Down Expand Up @@ -91,6 +92,14 @@ export class GodwokenClient {
return +nonce;
}

/**
* getFeeConfig from Godwoken RPC
*/
public async getFeeConfig(): Promise<FeeConfig> {
// TODO: cache FeeConfig
return this.rpcCall("get_fee_config");
}

public async getData(
dataHash: Hash,
blockParameter?: BlockParameter
Expand Down
14 changes: 14 additions & 0 deletions packages/godwoken/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ export type HexU128 = HexNumber;
// null means `pending`
export type BlockParameter = U64 | null;

/**
* HashMap<fee_sudt_id, fee_rate>
* fee_rate is known as gasPrice in Ethereum.
*/
interface FeeRates {
readonly [sudtId: string]: HexU64;
}
/**
* e.g. {"fee_rates":{"0x1":"0x1f4"},...}
*/
export interface FeeConfig {
fee_rates: FeeRates;
}

export interface LogItem {
account_id: HexU32;
// The actual type is `u8`
Expand Down

0 comments on commit 40edda3

Please sign in to comment.