Skip to content

Commit

Permalink
fix pvg calculation on arbitrum
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSulpiride committed Jun 19, 2023
1 parent 05b79b6 commit 183f753
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fastify, { FastifyInstance } from "fastify";
import cors from "@fastify/cors";
import RpcError from "types/lib/api/errors/rpc-error";
import { ServerConfig } from "types/src/api/interfaces";
import { ServerConfig } from "types/lib/api/interfaces";
import logger from "./logger";

export class Server {
Expand Down
1 change: 1 addition & 0 deletions packages/executor/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class Executor {
this.reputationService
);
this.eth = new Eth(
this.network,
this.provider,
this.userOpValidationService,
this.mempoolService,
Expand Down
27 changes: 23 additions & 4 deletions packages/executor/src/modules/eth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, ethers } from "ethers";
import { BigNumber, BigNumberish, ethers } from "ethers";
import { arrayify, hexlify } from "ethers/lib/utils";
import RpcError from "types/lib/api/errors/rpc-error";
import * as RpcErrorCodes from "types/lib/api/errors/rpc-error-codes";
Expand All @@ -13,6 +13,9 @@ import {
UserOperationReceipt,
} from "types/lib/api/interfaces";
import { EntryPoint__factory } from "types/lib/executor/contracts/factories";
import { NetworkName } from "types/lib";
import { estimateArbitrumPVG } from "params/lib/gas-estimation/arbitrum";
import { IPVGEstimator } from "params/lib/types/IPVGEstimator";
import { NetworkConfig } from "../config";
import { deepHexlify, packUserOp } from "../utils";
import { UserOpValidationService, MempoolService } from "../services";
Expand All @@ -23,13 +26,22 @@ import {
} from "./interfaces";

export class Eth {
private pvgEstimator: IPVGEstimator | null = null;

constructor(
private networkName: NetworkName,
private provider: ethers.providers.JsonRpcProvider,
private userOpValidationService: UserOpValidationService,
private mempoolService: MempoolService,
private config: NetworkConfig,
private logger: Logger
) {}
) {
if (
["arbitrum", "arbitrumNitro", "arbitrumNova"].includes(this.networkName)
) {
this.pvgEstimator = estimateArbitrumPVG(this.provider);
}
}

/**
*
Expand Down Expand Up @@ -86,14 +98,21 @@ export class Eth {
preVerificationGas: 0,
...userOp,
};
const preVerificationGas = this.calcPreVerificationGas(userOp);
let preVerificationGas: BigNumberish = this.calcPreVerificationGas(userOp);
userOpComplemented.preVerificationGas = preVerificationGas;

const { returnInfo } =
await this.userOpValidationService.validateForEstimation(
userOpComplemented,
entryPoint
);
if (this.pvgEstimator) {
preVerificationGas = await this.pvgEstimator(
entryPoint,
userOpComplemented,
preVerificationGas
);
}

// eslint-disable-next-line prefer-const
let { preOpGas, validAfter, validUntil } = returnInfo;
Expand All @@ -110,7 +129,7 @@ export class Eth {
err.message.match(/reason="(.*?)"/)?.at(1) ?? "execution reverted";
throw new RpcError(message, RpcErrorCodes.EXECUTION_REVERTED);
});
// const preVerificationGas = this.calcPreVerificationGas(userOp);

const verificationGas = BigNumber.from(preOpGas).toNumber();
validAfter = BigNumber.from(validAfter);
validUntil = BigNumber.from(validUntil);
Expand Down
4 changes: 3 additions & 1 deletion packages/params/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"url": "https://github.com/etherspot/skandha/issues"
},
"dependencies": {
"types": "^0.0.9"
"types": "^0.0.9",
"ethers": "5.7.2",
"@arbitrum/sdk": "3.1.4"
},
"scripts": {
"clean": "rm -rf lib && rm -f *.tsbuildinfo",
Expand Down
45 changes: 45 additions & 0 deletions packages/params/src/gas-estimation/arbitrum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { NodeInterface__factory } from "@arbitrum/sdk/dist/lib/abi/factories/NodeInterface__factory";
import { NODE_INTERFACE_ADDRESS } from "@arbitrum/sdk/dist/lib/dataEntities/constants";
import { UserOperationStruct } from "types/lib/executor/contracts/EntryPoint";
import { BigNumber, BigNumberish, ethers } from "ethers";
import { EntryPoint__factory } from "types/lib/executor/contracts";
import { IPVGEstimator, IPVGEstimatorWrapper } from "../types/IPVGEstimator";

export const estimateArbitrumPVG: IPVGEstimatorWrapper = (
provider
): IPVGEstimator => {
const nodeInterface = NodeInterface__factory.connect(
NODE_INTERFACE_ADDRESS,
provider
);
const dummyWallet = ethers.Wallet.createRandom();
return async (
entryPointAddr: string,
userOp: UserOperationStruct,
initial: BigNumberish
): Promise<BigNumber> => {
const entryPoint = EntryPoint__factory.connect(entryPointAddr, provider);
const handleOpsData = entryPoint.interface.encodeFunctionData("handleOps", [
[userOp],
dummyWallet.address,
]);

const contractCreation = BigNumber.from(userOp.nonce).eq(0);
try {
const gasEstimateComponents =
await nodeInterface.callStatic.gasEstimateL1Component(
entryPoint.address,
contractCreation,
handleOpsData
);
const l1GasEstimated = gasEstimateComponents.gasEstimateForL1;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const baseFee = gasEstimateComponents.baseFee;
return l1GasEstimated.add(initial);
} catch (err) {
// eslint-disable-next-line no-console
console.error("Error while estimating arbitrum PVG", err);
return BigNumber.from(initial);
}
};
};
12 changes: 12 additions & 0 deletions packages/params/src/types/IPVGEstimator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BigNumber, BigNumberish, providers } from "ethers";
import { UserOperationStruct } from "types/lib/executor/contracts/EntryPoint";

export type IPVGEstimatorWrapper = (
provider: providers.StaticJsonRpcProvider
) => IPVGEstimator;

export type IPVGEstimator = (
entryPointAddr: string,
userOp: UserOperationStruct,
initial: BigNumberish // initial amount of gas. It will be added to the estimated gas
) => Promise<BigNumber>;
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
"@jridgewell/gen-mapping" "^0.1.0"
"@jridgewell/trace-mapping" "^0.3.9"

"@arbitrum/[email protected]":
version "3.1.4"
resolved "https://registry.yarnpkg.com/@arbitrum/sdk/-/sdk-3.1.4.tgz#2088e09be4feaaf1ba3701290416e26f0407765e"
integrity sha512-G0hWl+rST4/vLIXJrNbw03q1YNwyzrhmLCfEckNOcLxCZzzqUv2iLkwabP3bKbfzFKP71ObepTDIREtmfEGjbA==
dependencies:
"@ethersproject/address" "^5.0.8"
"@ethersproject/bignumber" "^5.1.1"
"@ethersproject/bytes" "^5.0.8"
ethers "^5.1.0"

"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.12.13", "@babel/code-frame@^7.18.6":
version "7.18.6"
resolved "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.18.6.tgz"
Expand Down Expand Up @@ -379,7 +389,7 @@
"@ethersproject/logger" "^5.7.0"
"@ethersproject/properties" "^5.7.0"

"@ethersproject/[email protected]", "@ethersproject/address@^5.7.0":
"@ethersproject/[email protected]", "@ethersproject/address@^5.0.8", "@ethersproject/address@^5.7.0":
version "5.7.0"
resolved "https://registry.npmjs.org/@ethersproject/address/-/address-5.7.0.tgz"
integrity sha512-9wYhYt7aghVGo758POM5nqcOMaE168Q6aRLJZwUmiqSrAungkG74gSSeKEIR7ukixesdRZGPgVqme6vmxs1fkA==
Expand All @@ -405,7 +415,7 @@
"@ethersproject/bytes" "^5.7.0"
"@ethersproject/properties" "^5.7.0"

"@ethersproject/[email protected]", "@ethersproject/bignumber@^5.7.0":
"@ethersproject/[email protected]", "@ethersproject/bignumber@^5.1.1", "@ethersproject/bignumber@^5.7.0":
version "5.7.0"
resolved "https://registry.npmjs.org/@ethersproject/bignumber/-/bignumber-5.7.0.tgz"
integrity sha512-n1CAdIHRWjSucQO3MC1zPSVgV/6dy/fjL9pMrPP9peL+QxEg9wOsVqwD4+818B6LUEtaXzVHQiuivzRoxPxUGw==
Expand All @@ -414,7 +424,7 @@
"@ethersproject/logger" "^5.7.0"
bn.js "^5.2.1"

"@ethersproject/[email protected]", "@ethersproject/bytes@^5.7.0":
"@ethersproject/[email protected]", "@ethersproject/bytes@^5.0.8", "@ethersproject/bytes@^5.7.0":
version "5.7.0"
resolved "https://registry.npmjs.org/@ethersproject/bytes/-/bytes-5.7.0.tgz"
integrity sha512-nsbxwgFXWh9NyYWo+U8atvmMsSdKJprTcICAkvbBffT75qDocbuggBU0SJiVK2MuTrp0q+xvLkTnGMPK1+uA9A==
Expand Down Expand Up @@ -3913,7 +3923,7 @@ esutils@^2.0.2:
resolved "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

[email protected]:
[email protected], ethers@^5.1.0:
version "5.7.2"
resolved "https://registry.npmjs.org/ethers/-/ethers-5.7.2.tgz"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down

0 comments on commit 183f753

Please sign in to comment.