Skip to content

Commit

Permalink
add suniv2 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Mar 27, 2024
1 parent ae1c2bf commit 4c6a25a
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 21 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ node arb-bot -k 12ab... -r https://... --orderbook-address 0x1a2b... --arb-addre
The app requires these arguments (all arguments can be set in env variables alternatively, more details below):
- `-k` or `--key`, Private key of wallet that performs the transactions. Will override the 'BOT_WALLET_PRIVATEKEY' in env variables
- `-r` or `--rpc`, RPC URL(s) that will be provider for interacting with evm, use different providers if more than 1 is specified to prevent banning. Will override the 'RPC_URL' in env variables
- `-m` or `--mode`, Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter`, Will override the 'MODE' in env variables
- `-m` or `--mode`, Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter` or `suniv2`, Will override the 'MODE' in env variables
- `--orderbook-address`, Address of the deployed orderbook contract, Will override the 'ORDERBOOK_ADDRESS' in env variables
- `--arb-address`, Address of the deployed arb contract, Will override the 'ARB_ADDRESS' in env variables
- `--arb-contract-type`, Type of the Arb contract, can be either of `flash-loan-v2` or `flash-loan-v3` or `order-taker`, not availabe for `srouter` mode since it is a specialized mode, Will override the 'ARB_TYPE' in env variables
Expand All @@ -61,6 +61,8 @@ Other optional arguments are:
- `--flashbot-rpc`, Optional flashbot rpc url to submit transaction to, Will override the 'FLASHBOT_RPC' in env variables
- `--interpreter-v2`, Flag for operating with interpreter V2, note that 'flash-loan-v2' is NOT compatible with interpreter v2. Will override the 'INTERPRETERV2' in env variables
- `--no-bundle`, Flag for not bundling orders based on pairs and clear each order individually. Will override the 'NO_BUNDLE' in env variables
- `--hops`, Option to specify how many hops the binary search should do in srouter mode, default is 11 if left unspecified, Will override the 'HOPS' in env variables
- `--rp32`, Option to use sushi RouteProcessor v3.2, defaults to v3 if not passed, Will override the 'RP3_2' in env variables
- `-V` or `--version`, output the version number
- `-h` or `--help`, output usage information

Expand Down Expand Up @@ -107,7 +109,7 @@ which will show:
Options:
-k, --key <private-key> Private key of wallet that performs the transactions. Will override the 'BOT_WALLET_PRIVATEKEY' in env variables
-r, --rpc <url...> RPC URL(s) that will be provider for interacting with evm, use different providers if more than 1 is specified to prevent banning. Will override the 'RPC_URL' in env variables
-m, --mode <string> Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter`, Will override the 'MODE' in env variables
-m, --mode <string> Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter` or `suniv2`, Will override the 'MODE' in env variables
-o, --orders <path> The path to a local json file containing the orders details, can be used in combination with --subgraph, Will override the 'ORDERS' in env variables
-s, --subgraph <url...> Subgraph URL(s) to read orders details from, can be used in combination with --orders, Will override the 'SUBGRAPH' in env variables
--orderbook-address <address> Address of the deployed orderbook contract, Will override the 'ORDERBOOK_ADDRESS' in env variables
Expand All @@ -129,6 +131,8 @@ which will show:
--use-public-rpcs Option to use public rpcs as fallback option for 'srouter' and 'router' mode, Will override the 'USE_PUBLIC_RPCS' in env variables
--interpreter-v2 Flag for operating with interpreter V2, note that 'flash-loan-v2' is NOT compatible with interpreter v2. Will override the 'INTERPRETERV2' in env variables
--no-bundle Flag for not bundling orders based on pairs and clear each order individually. Will override the 'NO_BUNDLE' in env variables
--hops <integer> Option to specify how many hops the binary search should do in srouter mode, default is 11 if left unspecified, Will override the 'HOPS' in env variables
--rp32 Option to use sushi RouteProcessor v3.2, defaults to v3 if not passed, Will override the 'RP3_2' in env variables
-V, --version output the version number
-h, --help display help for command
<br>
Expand All @@ -145,7 +149,7 @@ RPC_URL="https://polygon-mainnet.g.alchemy.com/v2/{API_KEY}, https://rpc.ankr.co
# Option to submit transactions using the flashbot RPC.
FLASHBOT_RPC=""

# bot running mode, one of "router", "0x", "curve", "crouter", "srouter"
# bot running mode, one of "router", "0x", "curve", "crouter", "srouter", "suniv2"
MODE="router"

# arb contract address
Expand Down Expand Up @@ -213,6 +217,9 @@ NO_BUNDLE="false"

# number of hops of binary search in srouter mode, if left unspecified will be 11 by default
HOPS=11

# Option to use sushi RouteProcessorv3.2, default is v3
RP3_2="true"
```
If both env variables and CLI argument are set, the CLI arguments will be prioritized and override the env variables.

Expand Down Expand Up @@ -241,6 +248,8 @@ const configOptions = {
timeout : 300, // seconds to wait for tx to mine before disregarding it
interpreterv2 : true, // if interpreter v2 should be used, not compatible with flash-loan-v2 arb contract
bundle : true, // if orders should be bundled based on token pair or be handled individually
hops : 6, // The amount of hops of binary search for sorouter mode
rp32 : true, // Option to use sushi RouteProcessorv3.2, default is v3
liquidityProviders : [ // list of liquidity providers for "router" mode to get quotes from (optional)
"sushiswapv2",
"uniswapv2"
Expand All @@ -266,7 +275,7 @@ const sgFilters = { // fil
const orderDetails = await RainArbBot.getOrderDetails(subgraphs, ordersJson, config.signer, sgFilters);

// to run the clearing process and get the report object which holds the report of cleared orders
const mode = "srouter" // mode can be one of "router" or "0x" or "curve" or "crouter" or "srouter"
const mode = "srouter" // mode can be one of "router" or "0x" or "curve" or "crouter" or "srouter" or "suniv2"
const reports = await RainArbBot.clear(mode, config, orderDetails, ...[clearOptions])
```
<br>
Expand Down
6 changes: 5 additions & 1 deletion arb-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const ENV_OPTIONS = {
timeout : process?.env?.TIMEOUT,
flashbotRpc : process?.env?.FLASHBOT_RPC,
hops : process?.env?.HOPS,
rp32 : process?.env?.RP3_2?.toLowerCase() === "true" ? true : false,
rpc : process?.env?.RPC_URL
? Array.from(process?.env?.RPC_URL.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined,
Expand All @@ -46,7 +47,7 @@ const getOptions = async argv => {
const cmdOptions = new Command("node arb-bot")
.option("-k, --key <private-key>", "Private key of wallet that performs the transactions. Will override the 'BOT_WALLET_PRIVATEKEY' in env variables")
.option("-r, --rpc <url...>", "RPC URL(s) that will be provider for interacting with evm, use different providers if more than 1 is specified to prevent banning. Will override the 'RPC_URL' in env variables")
.option("-m, --mode <string>", "Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter`, Will override the 'MODE' in env variables")
.option("-m, --mode <string>", "Running mode of the bot, must be one of: `0x` or `curve` or `router` or `crouter` or `srouter` or `suniv2`, Will override the 'MODE' in env variables")
.option("-o, --orders <path>", "The path to a local json file containing the orders details, can be used in combination with --subgraph, Will override the 'ORDERS' in env variables")
.option("-s, --subgraph <url...>", "Subgraph URL(s) to read orders details from, can be used in combination with --orders, Will override the 'SUBGRAPH' in env variables")
.option("--orderbook-address <address>", "Address of the deployed orderbook contract, Will override the 'ORDERBOOK_ADDRESS' in env variables")
Expand All @@ -69,6 +70,7 @@ const getOptions = async argv => {
.option("--interpreter-v2", "Flag for operating with interpreter V2, note that 'flash-loan-v2' is NOT compatible with interpreter v2. Will override the 'INTERPRETERV2' in env variables")
.option("--no-bundle", "Flag for not bundling orders based on pairs and clear each order individually. Will override the 'NO_BUNDLE' in env variables")
.option("--hops <integer>", "Option to specify how many hops the binary search should do in srouter mode, default is 11 if left unspecified, Will override the 'HOPS' in env variables")
.option("--rp32", "Option to use sushi RouteProcessor v3.2, defaults to v3 if not passed, Will override the 'RP3_2' 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.",
"- Use \"node arb-bot [options]\" command alias for running the app from its repository workspace",
Expand Down Expand Up @@ -104,6 +106,7 @@ const getOptions = async argv => {
cmdOptions.timeout = cmdOptions.timeout || ENV_OPTIONS.timeout;
cmdOptions.interpreterv2 = cmdOptions.interpreterv2 || ENV_OPTIONS.interpreterv2;
cmdOptions.hops = cmdOptions.hops || ENV_OPTIONS.hops;
cmdOptions.rp32 = cmdOptions.rp32 || ENV_OPTIONS.rp32;
cmdOptions.bundle = cmdOptions.bundle ? ENV_OPTIONS.bundle : false;

return cmdOptions;
Expand Down Expand Up @@ -136,6 +139,7 @@ const arbRound = async options => {
interpreterv2 : options.interpreterv2,
bundle : options.bundle,
hops : options.hops,
rp32 : options.rp32,
liquidityProviders : options.lps
? Array.from(options.lps.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined,
Expand Down
37 changes: 37 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1015,5 +1015,42 @@
"address": "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
"decimals": 18
}
},
{
"network": "flare mainnet",
"chainId": 14,
"explorer": "https://flarescan.com/",
"routeProcessor3Address": "0x9B3F1D56D9004e6C69d8247d402F38DE5F87A27c",
"uniV2Router02Address": "0x088EeCB467B3968Da36c71F05023A1d3133B2B83",
"zeroEx": {
},
"curve": {
"pools": []
},
"enosys":{
"pools": [
{
"address": "0x7520005032F43229F606d3ACeae97045b9D6F7ea",
"token0": "0x1D80c49BbBCd1C0911346656B529DF9E5c2F783d",
"token1": "0x96B41289D90444B8adD57e6F265DB5aE8651DF29"
}
]
},
"nativeToken": {
"symbol": "FLR",
"address": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"decimals": 18
},
"nativeWrappedToken": {
"symbol": "WFLR",
"address": "0x1D80c49BbBCd1C0911346656B529DF9E5c2F783d",
"decimals": 18
},
"liquidityProviders": [

],
"stableTokens": {

}
}
]
7 changes: 5 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RPC_URL="https://polygon-mainnet.g.alchemy.com/v2/{API_KEY}, https://rpc.ankr.co
# Option to submit transactions using the flashbot RPC.
FLASHBOT_RPC=""

# bot running mode, one of "router", "0x", "curve", "crouter", "srouter"
# bot running mode, one of "router", "0x", "curve", "crouter", "srouter", "suniv2"
MODE="router"

# arb contract address
Expand Down Expand Up @@ -78,4 +78,7 @@ INTERPRETERV2="true"
NO_BUNDLE="false"

# number of hops of binary search in srouter mode, if left unspecified will be 11 by default
HOPS=11
HOPS=11

# Option to use sushi RouteProcessorv3.2, default is v3
RP3_2="true"
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { zeroExClear } = require("./modes/zeroex");
const { routerClear } = require("./modes/router");
const { crouterClear } = require("./modes/crouter");
const { srouterClear } = require("./modes/srouter");
const { suniv2Clear } = require("./modes/suniv2");
const { getOrderDetailsFromJson, appGlobalLogger } = require("./utils");


Expand Down Expand Up @@ -68,7 +69,11 @@ const configOptions = {
/**
* The amount of hops of binary search for sorouter mode
*/
hops: 11
hops: 11,
/**
* Option to use sushi RouteProcessorv3.2, default is v3
*/
rp32: false
};

/**
Expand Down Expand Up @@ -236,6 +241,7 @@ const getConfig = async(
config.usePublicRpcs = !!options?.usePublicRpcs;
config.interpreterv2 = !!options?.interpreterv2;
config.hops = hops;
config.rp32 = !!options?.rp32;

return config;
};
Expand All @@ -258,14 +264,11 @@ const clear = async(
const _mode = mode.toLowerCase();
const version = versions.node;
const majorVersion = Number(version.slice(0, version.indexOf(".")));
// const prioritization = options.prioritization !== undefined
// ? !!options.prioritization
// : clearOptions.prioritization;
const gasCoveragePercentage = options.gasCoveragePercentage !== undefined
? options.gasCoveragePercentage
: clearOptions.gasCoveragePercentage;

if (_mode !== "srouter") {
if (_mode !== "srouter" && _mode !== "suniv2") {
if (!config.arbType) throw "undefined arb contract type";
if (!/^flash-loan-v[23]$|^order-taker$/.test(config.arbType)) {
throw "invalid arb contract type, must be either of: 'flash-loan-v2' or 'flash-loan-v3' or 'order-taker'";
Expand Down Expand Up @@ -315,6 +318,15 @@ const clear = async(
);
else throw `NodeJS v18 or higher is required for running the app in "router" mode, current version: ${version}`;
}
else if (_mode === "suniv2") {
if (majorVersion >= 18) return await suniv2Clear(
config,
ordersDetails,
gasCoveragePercentage,
// prioritization
);
else throw `NodeJS v18 or higher is required for running the app in "router" mode, current version: ${version}`;
}
else throw "unknown mode, must be either of '0x' or 'curve' or 'router' or 'srouter'";
};

Expand Down
12 changes: 9 additions & 3 deletions src/modes/crouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ const crouterClear = async(
fromToken,
toToken,
arb.address,
config.routeProcessor3_2Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
// permits
// "0.005"
);
Expand All @@ -477,8 +479,12 @@ const crouterClear = async(
exchangeData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bytes"],
[
config.routeProcessor3_2Address,
config.routeProcessor3_2Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
fnData
]
);
Expand Down
12 changes: 9 additions & 3 deletions src/modes/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ const routerClear = async(
fromToken,
toToken,
arb.address,
config.routeProcessor3_2Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
// permits
// "0.005"
);
Expand Down Expand Up @@ -297,8 +299,12 @@ const routerClear = async(
const exchangeData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bytes"],
[
config.routeProcessor3_2Address,
config.routeProcessor3_2Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
config.rp32
? config.routeProcessor3_2Address
: config.routeProcessor3Address,
fnData
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/modes/srouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ async function checkArb(
fromToken,
toToken,
arb.address,
config.routeProcessor3_2Address,
config.rp32 ? config.routeProcessor3_2Address : config.routeProcessor3Address,
// permits
// "0.005"
);
Expand Down
Loading

0 comments on commit 4c6a25a

Please sign in to comment.