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: fee history #86

Merged
merged 3 commits into from
Sep 11, 2023
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.0.42",
"version": "0.0.43",
"stream": "true",
"command": {
"version": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "root",
"private": true,
"version": "0.0.42",
"version": "0.0.43",
"engines": {
"node": ">=18.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "api",
"version": "0.0.42",
"version": "0.0.43",
"description": "The API module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -35,12 +35,12 @@
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"ethers": "5.7.2",
"executor": "^0.0.42",
"executor": "^0.0.43",
"fastify": "4.14.1",
"pino": "8.11.0",
"pino-pretty": "10.0.0",
"reflect-metadata": "0.1.13",
"types": "^0.0.42"
"types": "^0.0.43"
},
"devDependencies": {
"@types/connect": "3.4.35"
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ export class ApiApp {
case CustomRPCMethods.skandha_getGasPrice:
result = await skandhaApi.getGasPrice();
break;
case CustomRPCMethods.skandha_feeHistory:
result = await skandhaApi.getFeeHistory({
entryPoint: params[0],
blockCount: params[1],
newestBlock: params[2],
});
break;
default:
throw new RpcError(
`Method ${method} is not supported`,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const CustomRPCMethods = {
skandha_validateUserOperation: "skandha_validateUserOperation",
skandha_getGasPrice: "skandha_getGasPrice",
skandha_feeHistory: "skandha_feeHistory",
};

export const BundlerRPCMethods = {
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/dto/FeeHistory.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IsEthereumAddress, ValidateIf } from "class-validator";
import { BigNumberish } from "ethers";
import { IsBigNumber } from "../utils/is-bignumber";

export class FeeHistoryArgs {
@IsEthereumAddress()
entryPoint!: string;

@IsBigNumber()
blockCount!: BigNumberish;

@ValidateIf((o) => o.newestBlock != "latest")
@IsBigNumber()
newestBlock!: BigNumberish | string;
}
26 changes: 25 additions & 1 deletion packages/api/src/modules/skandha.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Eth } from "executor/lib/modules/eth";
import { GetGasPriceResponse } from "types/lib/api/interfaces";
import {
GetFeeHistoryResponse,
GetGasPriceResponse,
} from "types/lib/api/interfaces";
import { Skandha } from "executor/lib/modules";
import RpcError from "types/lib/api/errors/rpc-error";
import * as RpcErrorCodes from "types/lib/api/errors/rpc-error-codes";
import { RpcMethodValidator } from "../utils/RpcMethodValidator";
import { SendUserOperationGasArgs } from "../dto/SendUserOperation.dto";
import { FeeHistoryArgs } from "../dto/FeeHistory.dto";

export class SkandhaAPI {
constructor(private ethModule: Eth, private skandhaModule: Skandha) {}
Expand All @@ -19,6 +25,24 @@ export class SkandhaAPI {
return await this.ethModule.validateUserOp(args);
}

/**
* @param entryPoint Entry Point
* @param useropCount Number of blocks in the requested range
* @param newestBlock Highest number block of the requested range, or "latest"
* @returns
*/
@RpcMethodValidator(FeeHistoryArgs)
async getFeeHistory(args: FeeHistoryArgs): Promise<GetFeeHistoryResponse> {
if (!this.ethModule.validateEntryPoint(args.entryPoint)) {
throw new RpcError("Invalid Entrypoint", RpcErrorCodes.INVALID_REQUEST);
}
return await this.skandhaModule.getFeeHistory(
args.entryPoint,
args.blockCount,
args.newestBlock
);
}

async getGasPrice(): Promise<GetGasPriceResponse> {
return await this.skandhaModule.getGasPrice();
}
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli",
"version": "0.0.42",
"version": "0.0.43",
"description": "> TODO: description",
"author": "zincoshine <[email protected]>",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -31,13 +31,13 @@
"url": "https://https://github.com/etherspot/skandha/issues"
},
"dependencies": {
"api": "^0.0.42",
"db": "^0.0.42",
"executor": "^0.0.42",
"api": "^0.0.43",
"db": "^0.0.43",
"executor": "^0.0.43",
"find-up": "5.0.0",
"got": "12.5.3",
"js-yaml": "4.1.0",
"types": "^0.0.42",
"types": "^0.0.43",
"yargs": "17.6.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "db",
"version": "0.0.42",
"version": "0.0.43",
"description": "The DB module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://github.com/etherspot/etherspot-bundler#readme",
Expand Down Expand Up @@ -37,6 +37,6 @@
"devDependencies": {
"@types/rocksdb": "3.0.1",
"prettier": "^2.8.4",
"types": "^0.0.42"
"types": "^0.0.43"
}
}
6 changes: 3 additions & 3 deletions packages/executor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "executor",
"version": "0.0.42",
"version": "0.0.43",
"description": "The Relayer module of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"async-mutex": "0.4.0",
"ethers": "5.7.2",
"params": "^0.0.42",
"types": "^0.0.42"
"params": "^0.0.43",
"types": "^0.0.43"
}
}
2 changes: 1 addition & 1 deletion packages/executor/src/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export class Eth {
);
}

private validateEntryPoint(entryPoint: string): boolean {
validateEntryPoint(entryPoint: string): boolean {
return (
this.config.entryPoints.findIndex(
(ep) => ep.toLowerCase() === entryPoint.toLowerCase()
Expand Down
64 changes: 62 additions & 2 deletions packages/executor/src/modules/skandha.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BigNumber, ethers } from "ethers";
import { BigNumber, BigNumberish, ethers } from "ethers";
import { NetworkName } from "types/lib";
import { GetGasPriceResponse } from "types/lib/api/interfaces";
import {
GetFeeHistoryResponse,
GetGasPriceResponse,
} from "types/lib/api/interfaces";
import RpcError from "types/lib/api/errors/rpc-error";
import * as RpcErrorCodes from "types/lib/api/errors/rpc-error-codes";
import { GasPriceMarkupOne } from "params/lib";
import { getGasFee } from "params/lib";
import { IEntryPoint__factory } from "types/lib/executor/contracts";
import { UserOperationStruct } from "types/lib/executor/contracts/EntryPoint";
import { Logger, NetworkConfig } from "../interfaces";

// custom features of Skandha
Expand Down Expand Up @@ -51,4 +56,59 @@ export class Skandha {
maxFeePerGas,
};
}

/**
* see eth_feeHistory
* @param entryPoint Entry Point contract
* @param blockCount Number of blocks in the requested range
* @param newestBlock Highest number block of the requested range, or "latest"
*/
async getFeeHistory(
entryPoint: string,
blockCount: BigNumberish,
newestBlock: BigNumberish | string
): Promise<GetFeeHistoryResponse> {
const toBlockInfo = await this.provider.getBlock(newestBlock.toString());
const fromBlockNumber = BigNumber.from(toBlockInfo.number)
.sub(blockCount)
.toNumber();
const contract = IEntryPoint__factory.connect(entryPoint, this.provider);
const events = await contract.queryFilter(
contract.filters.UserOperationEvent(),
fromBlockNumber,
toBlockInfo.number
);
const txReceipts = await Promise.all(
events.map((event) => event.getTransaction())
);
const txDecoded = txReceipts
.map((receipt) => {
try {
return contract.interface.decodeFunctionData(
"handleOps",
receipt.data
);
} catch (err) {
this.logger.error(err);
return null;
}
})
.filter((el) => el !== null);

const actualGasPrice = events.map((event) =>
BigNumber.from(event.args.actualGasCost).div(event.args.actualGasUsed)
);
const userops = txDecoded
.map((handleOps) => handleOps!.ops as UserOperationStruct[])
.reduce((p, c) => {
return p.concat(c);
}, []);
return {
actualGasPrice,
maxFeePerGas: userops.map((userop) => userop.maxFeePerGas),
maxPriorityFeePerGas: userops.map(
(userop) => userop.maxPriorityFeePerGas
),
};
}
}
10 changes: 10 additions & 0 deletions packages/executor/src/services/BundlingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ export class BundlingService {
let txHash: string;
// geth-dev doesn't support signTransaction
if (!this.config.testingMode) {
// check for execution revert
try {
await wallet.estimateGas(tx);
} catch (err) {
this.logger.error(err);
for (const entry of entries) {
await this.mempoolService.remove(entry);
}
return null;
}
const signedRawTx = await wallet.signTransaction(tx);

const method = !this.networkConfig.conditionalTransactions
Expand Down
4 changes: 2 additions & 2 deletions packages/params/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "params",
"version": "0.0.42",
"version": "0.0.43",
"description": "Various bundler parameters",
"author": "Etherspot",
"homepage": "https://github.com/etherspot/skandha#readme",
Expand All @@ -25,7 +25,7 @@
"@eth-optimism/sdk": "3.0.0",
"@mantleio/sdk": "0.2.1",
"ethers": "5.7.2",
"types": "^0.0.42"
"types": "^0.0.43"
},
"scripts": {
"clean": "rm -rf lib && rm -f *.tsbuildinfo",
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "types",
"version": "0.0.42",
"version": "0.0.43",
"description": "The types of Etherspot bundler client",
"author": "Etherspot",
"homepage": "https://https://github.com/etherspot/skandha#readme",
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/api/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export type GetGasPriceResponse = {
maxPriorityFeePerGas: BigNumberish;
};

export type GetFeeHistoryResponse = {
actualGasPrice: BigNumberish[];
maxFeePerGas: BigNumberish[];
maxPriorityFeePerGas: BigNumberish[];
};

export type UserOperationReceipt = {
userOpHash: string;
sender: string;
Expand Down