Skip to content

Commit

Permalink
Merge pull request #257 from rainlanguage/2024-11-15-gas-price-multip…
Browse files Browse the repository at this point in the history
…lier-option

gas price ultiplier option
  • Loading branch information
rouzwelt authored Nov 15, 2024
2 parents 17c4d7a + e4c4627 commit e72cf2f
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 21 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -163,6 +164,10 @@ const getOptions = async (argv: any, version?: string) => {
"--route <string>",
"Specifies the routing mode 'multi' or 'single' or 'full', default is 'single'. Will override the 'ROUTE' in env variables",
)
.option(
"--gas-price-multiplier <integer>",
"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.",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BaseError,
RpcRequestError,
ExecutionRevertedError,
InsufficientFundsError,
// InvalidInputRpcError,
// TransactionRejectedRpcError,
} from "viem";
Expand Down Expand Up @@ -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))
);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type CliOptions = {
selfFundOrders?: SelfFundOrder[];
tokens?: TokenDetails[];
route?: string;
gasPriceMultiplier: number;
};

export type TokenDetails = {
Expand Down Expand Up @@ -143,6 +144,7 @@ export type BotConfig = {
walletKey: string;
route?: "multi" | "single";
rpcRecords: Record<string, RpcRecord>;
gasPriceMultiplier: number;
onFetchRequest?: (request: Request) => void;
onFetchResponse?: (request: Response) => void;
};
Expand Down
6 changes: 6 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -201,9 +203,11 @@ describe("Test cli", async function () {
cache: {},
},
},
gasPriceMultiplier: 120,
},
options: {
botMinBalance: "0.123",
gasPriceMultiplier: 120,
},
};
await sleep(1000);
Expand All @@ -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);
});
});
1 change: 1 addition & 0 deletions test/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const config = {
decimals: token2.decimals,
symbol: token2.symbol,
},
gasPriceMultiplier: 107,
};

const vaultBalance1 = BigNumber.from("10000000000000000000");
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e72cf2f

Please sign in to comment.