Skip to content

Commit

Permalink
Merge branch 'master' into 2024-10-25-owner-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt authored Nov 15, 2024
2 parents 2f9042a + e72cf2f commit b855d57
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Other optional arguments are:
- `-t` or `--topup-amount`, The initial topup amount of excess wallets, requirs `--mnemonic`. Will override the 'TOPUP_AMOUNT' in env variables
- `--owner-profile`, Specifies the owner limit, example: --owner-profile 0x123456=12 . Will override the 'OWNER_PROFILE' in env variables
- `--public-rpc`, Allows to use public RPCs as fallbacks, default is false. Will override the 'PUBLIC_RPC' 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 @@ -256,6 +257,8 @@ PUBLIC_RPC=
# 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 @@ -85,6 +85,9 @@ PUBLIC_RPC=
# 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
22 changes: 22 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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,
ownerProfile: process?.env?.OWNER_PROFILE
? Array.from(process?.env?.OWNER_PROFILE.matchAll(/[^,\s]+/g)).map((v) => v[0])
Expand Down Expand Up @@ -179,6 +180,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 @@ -216,6 +221,8 @@ const getOptions = async (argv: any, version?: string) => {
cmdOptions.walletCount = cmdOptions.walletCount || getEnv(ENV_OPTIONS.walletCount);
cmdOptions.topupAmount = cmdOptions.topupAmount || getEnv(ENV_OPTIONS.topupAmount);
cmdOptions.selfFundOrders = cmdOptions.selfFundOrders || getEnv(ENV_OPTIONS.selfFundOrders);
cmdOptions.gasPriceMultiplier =
cmdOptions.gasPriceMultiplier || getEnv(ENV_OPTIONS.gasPriceMultiplier);
cmdOptions.botMinBalance = cmdOptions.botMinBalance || getEnv(ENV_OPTIONS.botMinBalance);
cmdOptions.ownerProfile = cmdOptions.ownerProfile || getEnv(ENV_OPTIONS.ownerProfile);
cmdOptions.route = cmdOptions.route || getEnv(ENV_OPTIONS.route);
Expand Down Expand Up @@ -384,6 +391,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: SgOrder[] = [];
if (!process?.env?.CLI_STARTUP_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 @@ -206,6 +206,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 @@ -439,7 +439,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 @@ -579,6 +579,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 @@ -46,6 +46,7 @@ export type CliOptions = {
ownerProfile?: Record<string, number>;
publicRpc: boolean;
route?: string;
gasPriceMultiplier: number;
};

export type TokenDetails = {
Expand Down Expand Up @@ -168,6 +169,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 @@ -65,6 +65,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;
orders = prepareOrdersForRound(
await getOrderbookOwnersProfileMapFromSg(orders, viemClient, []),
false,
Expand Down Expand Up @@ -582,6 +583,7 @@ for (let i = 0; i < testData.length; i++) {
config.accounts = [];
config.mainAccount = bot;
config.quoteRpc = [mockServer.url + "/rpc"];
config.gasPriceMultiplier = 107;
orders = prepareOrdersForRound(
await getOrderbookOwnersProfileMapFromSg(orders, viemClient, []),
false,
Expand Down Expand Up @@ -933,6 +935,7 @@ for (let i = 0; i < testData.length; i++) {
config.accounts = [];
config.mainAccount = bot;
config.quoteRpc = [mockServer.url + "/rpc"];
config.gasPriceMultiplier = 107;
orders = prepareOrdersForRound(
await getOrderbookOwnersProfileMapFromSg(orders, viemClient, []),
false,
Expand Down

0 comments on commit b855d57

Please sign in to comment.