From 7e4bf746d8d0fb7fd5209e68d1bfab4f539164bf Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Fri, 15 Nov 2024 21:09:47 +0000 Subject: [PATCH 1/2] init --- README.md | 4 ++++ example.env | 3 +++ src/cli.ts | 21 +++++++++++++++++++++ src/error.ts | 2 ++ src/index.ts | 1 + src/processOrders.ts | 2 +- src/types.ts | 2 ++ test/cli.test.js | 6 ++++++ test/data.js | 1 + test/e2e/e2e.test.js | 3 +++ 10 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4340358f..974d6cb5 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Other optional arguments are: - `--route`, Specifies the routing mode 'multi' or 'single' or 'full', default is 'single'. Will override the 'ROUTE' in env variables - `-w` or `--wallet-count`, Number of wallet to submit transactions with, requirs `--mnemonic`. Will override the 'WALLET_COUNT' in env variables - `-t` or `--topup-amount`, The initial topup amount of excess wallets, requirs `--mnemonic`. Will override the 'TOPUP_AMOUNT' in env variables +- `--gas-price-multiplier`, Option to multiply the gas price fetched from the rpc as percentage, default is 107, ie +7%. Will override the 'GAS_PRICE_MULTIPLIER' in env variables - `-V` or `--version`, output the version number - `-h` or `--help`, output usage information @@ -252,6 +253,9 @@ SELF_FUND_ORDERS= # Specifies the routing mode 'multi' or 'single' or 'full', default is 'single' ROUTE="single" + +# Option to multiply the gas price fetched from the rpc as percentage, default is 107, ie +7% +GAS_PRICE_MULTIPLIER= ``` If both env variables and CLI argument are set, the CLI arguments will be prioritized and override the env variables. diff --git a/example.env b/example.env index 2a4eac6a..f4f0a84c 100644 --- a/example.env +++ b/example.env @@ -82,6 +82,9 @@ SELF_FUND_ORDERS= # Specifies the routing mode 'multi' or 'single' or 'full', default is 'single' ROUTE="single" +# Option to multiply the gas price fetched from the rpc as percentage, default is 107, ie +7% +GAS_PRICE_MULTIPLIER= + # test rpcs vars TEST_POLYGON_RPC= diff --git a/src/cli.ts b/src/cli.ts index 63a92535..6bd1810b 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -56,6 +56,7 @@ const ENV_OPTIONS = { topupAmount: process?.env?.TOPUP_AMOUNT, botMinBalance: process?.env?.BOT_MIN_BALANCE, selfFundOrders: process?.env?.SELF_FUND_ORDERS, + gasPriceMultiplier: process?.env?.GAS_PRICE_MULTIPLIER, route: process?.env?.ROUTE, rpc: process?.env?.RPC_URL ? Array.from(process?.env?.RPC_URL.matchAll(/[^,\s]+/g)).map((v) => v[0]) @@ -163,6 +164,10 @@ const getOptions = async (argv: any, version?: string) => { "--route ", "Specifies the routing mode 'multi' or 'single' or 'full', default is 'single'. Will override the 'ROUTE' in env variables", ) + .option( + "--gas-price-multiplier ", + "Option to multiply the gas price fetched from the rpc as percentage, default is 107, ie +7%. Will override the 'GAS_PRICE_MULTIPLIER' in env variables", + ) .description( [ "A NodeJS app to find and take arbitrage trades for Rain Orderbook orders against some DeFi liquidity providers, requires NodeJS v18 or higher.", @@ -197,6 +202,7 @@ const getOptions = async (argv: any, version?: string) => { cmdOptions.walletCount = cmdOptions.walletCount || ENV_OPTIONS.walletCount; cmdOptions.topupAmount = cmdOptions.topupAmount || ENV_OPTIONS.topupAmount; cmdOptions.selfFundOrders = cmdOptions.selfFundOrders || ENV_OPTIONS.selfFundOrders; + cmdOptions.gasPriceMultiplier = cmdOptions.gasPriceMultiplier || ENV_OPTIONS.gasPriceMultiplier; cmdOptions.botMinBalance = cmdOptions.botMinBalance || ENV_OPTIONS.botMinBalance; cmdOptions.route = cmdOptions.route || ENV_OPTIONS.route; cmdOptions.bundle = cmdOptions.bundle ? ENV_OPTIONS.bundle : false; @@ -368,6 +374,21 @@ export async function startup(argv: any, version?: string, tracer?: Tracer, ctx? if (!options.botMinBalance || !/^[0-9]+(.[0-9]+)?$/.test(options.botMinBalance)) { throw "expected a valid value for --bot-min-balance, it should be an number greater than 0"; } + if (options.gasPriceMultiplier) { + if (typeof options.gasPriceMultiplier === "number") { + if (options.gasPriceMultiplier <= 0 || !Number.isInteger(options.gasPriceMultiplier)) + throw "invalid gasPriceMultiplier value, must be an integer greater than zero"; + } else if ( + typeof options.gasPriceMultiplier === "string" && + /^[0-9]+$/.test(options.gasPriceMultiplier) + ) { + options.gasPriceMultiplier = Number(options.gasPriceMultiplier); + if (options.gasPriceMultiplier <= 0) + throw "invalid gasPriceMultiplier value, must be an integer greater than zero"; + } else throw "invalid gasPriceMultiplier value, must be an integer greater than zero"; + } else { + options.gasPriceMultiplier = 107; + } const poolUpdateInterval = _poolUpdateInterval * 60 * 1000; let ordersDetails: any[] = []; if (!process?.env?.TEST) diff --git a/src/error.ts b/src/error.ts index 11063e47..7ae4bc30 100644 --- a/src/error.ts +++ b/src/error.ts @@ -2,6 +2,7 @@ import { BaseError, RpcRequestError, ExecutionRevertedError, + InsufficientFundsError, // InvalidInputRpcError, // TransactionRejectedRpcError, } from "viem"; @@ -49,6 +50,7 @@ export function containsNodeError(err: BaseError): boolean { // err instanceof TransactionRejectedRpcError || // err instanceof InvalidInputRpcError || err instanceof ExecutionRevertedError || + err instanceof InsufficientFundsError || (err instanceof RpcRequestError && err.code === ExecutionRevertedError.code) || ("cause" in err && containsNodeError(err.cause as any)) ); diff --git a/src/index.ts b/src/index.ts index 02e9d794..a189aa02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -210,6 +210,7 @@ export async function getConfig( config.walletKey = walletKey; config.route = route; config.rpcRecords = rpcRecords; + config.gasPriceMultiplier = options.gasPriceMultiplier; // init accounts const { mainAccount, accounts } = await initAccounts(walletKey, config, options, tracer, ctx); diff --git a/src/processOrders.ts b/src/processOrders.ts index 21dcfc26..5ebcf575 100644 --- a/src/processOrders.ts +++ b/src/processOrders.ts @@ -445,7 +445,7 @@ export async function processPair(args: { let gasPrice; try { const gasPriceBigInt = await viemClient.getGasPrice(); - gasPrice = ethers.BigNumber.from(gasPriceBigInt).mul("107").div("100"); + gasPrice = ethers.BigNumber.from(gasPriceBigInt).mul(config.gasPriceMultiplier).div("100"); spanAttributes["details.gasPrice"] = gasPrice.toString(); } catch (e) { result.reason = ProcessPairHaltReason.FailedToGetGasPrice; diff --git a/src/types.ts b/src/types.ts index 9a90e067..f351bebf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,6 +45,7 @@ export type CliOptions = { selfFundOrders?: SelfFundOrder[]; tokens?: TokenDetails[]; route?: string; + gasPriceMultiplier: number; }; export type TokenDetails = { @@ -143,6 +144,7 @@ export type BotConfig = { walletKey: string; route?: "multi" | "single"; rpcRecords: Record; + gasPriceMultiplier: number; onFetchRequest?: (request: Request) => void; onFetchResponse?: (request: Response) => void; }; diff --git a/test/cli.test.js b/test/cli.test.js index 9b5f6b3e..2689bc78 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -183,6 +183,8 @@ describe("Test cli", async function () { `0x${"2".repeat(40)}`, "--bot-min-balance", "0.123", + "--gas-price-multiplier", + "120", ]); const expected = { roundGap: 10000, @@ -201,9 +203,11 @@ describe("Test cli", async function () { cache: {}, }, }, + gasPriceMultiplier: 120, }, options: { botMinBalance: "0.123", + gasPriceMultiplier: 120, }, }; await sleep(1000); @@ -215,5 +219,7 @@ describe("Test cli", async function () { assert.equal(result.config.route, expected.config.route); assert.deepEqual(result.config.rpcRecords, expected.config.rpcRecords); assert.equal(result.options.botMinBalance, expected.options.botMinBalance); + assert.equal(result.options.gasPriceMultiplier, expected.options.gasPriceMultiplier); + assert.equal(result.config.gasPriceMultiplier, expected.config.gasPriceMultiplier); }); }); diff --git a/test/data.js b/test/data.js index fa7e5897..17e3cfeb 100644 --- a/test/data.js +++ b/test/data.js @@ -66,6 +66,7 @@ const config = { decimals: token2.decimals, symbol: token2.symbol, }, + gasPriceMultiplier: 107, }; const vaultBalance1 = BigNumber.from("10000000000000000000"); diff --git a/test/e2e/e2e.test.js b/test/e2e/e2e.test.js index ed704212..81cd52ac 100644 --- a/test/e2e/e2e.test.js +++ b/test/e2e/e2e.test.js @@ -249,6 +249,7 @@ for (let i = 0; i < testData.length; i++) { config.accounts = []; config.mainAccount = bot; config.quoteRpc = [mockServer.url + "/rpc"]; + config.gasPriceMultiplier = 107; const { reports } = await clear(config, orders, tracer, ctx); // should have cleared correct number of orders @@ -579,6 +580,7 @@ for (let i = 0; i < testData.length; i++) { config.accounts = []; config.mainAccount = bot; config.quoteRpc = [mockServer.url + "/rpc"]; + config.gasPriceMultiplier = 107; const { reports } = await clear(config, orders, tracer, ctx); // should have cleared correct number of orders @@ -930,6 +932,7 @@ for (let i = 0; i < testData.length; i++) { config.accounts = []; config.mainAccount = bot; config.quoteRpc = [mockServer.url + "/rpc"]; + config.gasPriceMultiplier = 107; const { reports } = await clear(config, orders, tracer, ctx); // should have cleared correct number of orders From e4c4627aefd15e55b0435190150e304cf4d1acb7 Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Fri, 15 Nov 2024 21:43:12 +0000 Subject: [PATCH 2/2] update --- src/processOrders.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/processOrders.ts b/src/processOrders.ts index 5ebcf575..2b1b1544 100644 --- a/src/processOrders.ts +++ b/src/processOrders.ts @@ -585,6 +585,8 @@ export async function processPair(args: { if (e.noneNodeError) { spanAttributes["details.noneNodeError"] = true; result.error = e.noneNodeError; + } else { + spanAttributes["details.noneNodeError"] = false; } result.report = { status: ProcessPairReportStatus.NoOpportunity,