Skip to content

Commit

Permalink
Merge pull request #32 from rainprotocol/2023-10-27-flashbot-tx
Browse files Browse the repository at this point in the history
Added timeout for transactions [MINOR]
  • Loading branch information
Siddharth2207 authored Oct 30, 2023
2 parents 42665b7 + d892e6f commit de02d8e
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 83 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Other optional arguments are:
- `--max-profit`, Option to maximize profit for 'srouter' mode, comes at the cost of more RPC calls, Will override the 'MAX_PROFIT' in env variables
- `--max-ratio`, Option to maximize maxIORatio for 'srouter' mode, Will override the 'MAX_RATIO' in env variables
- `--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
- `--timeout`, Optional seconds to wait for the transaction to mine before disregarding it, Will override the 'TIMEOUT' in env variables
- `--flashbot-rpc`, Optional flashbot rpc url to submit transaction to, Will override the 'FLASHBOT_RPC' in env variables
- `-V` or `--version`, output the version number
- `-h` or `--help`, output usage information

Expand Down Expand Up @@ -102,7 +104,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
-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
-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
Expand All @@ -118,6 +120,8 @@ which will show:
--order-interpreter <address> Option to filter the subgraph query results with a specific order's interpreter address, Will override the 'ORDER_INTERPRETER' in env variables
--monthly-ratelimit <integer> 0x monthly rate limit, if not specified will not respect any 0x monthly ratelimit, Will override the 'MONTHLY_RATELIMIT' in env variables
--sleep <integer> Seconds to wait between each arb round, default is 10, Will override the 'SLEPP' in env variables
--flashbot-rpc <url> Optional flashbot rpc url to submit transaction to, Will override the 'FLASHBOT_RPC' in env variables
--timeout <integer> Optional seconds to wait for the transaction to mine before disregarding it, Will override the 'TIMEOUT' in env variables
--max-profit Option to maximize profit for 'srouter' mode, comes at the cost of more RPC calls, Will override the 'MAX_PROFIT' in env variables
--max-ratio Option to maximize maxIORatio for 'srouter' mode, Will override the 'MAX_RATIO' in env variables
--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
Expand All @@ -134,6 +138,9 @@ BOT_WALLET_PRIVATEKEY="123..."
# for specifying more than 1 RPC in the env, separate them by a comma and a space
RPC_URL="https://polygon-mainnet.g.alchemy.com/v2/{API_KEY}, https://rpc.ankr.com/polygon/{API_KEY}"

# Option to submit transactions using the flashbot RPC.
FLASHBOT_RPC=""

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

Expand Down Expand Up @@ -189,6 +196,9 @@ MAX_RATIO="true"

# Option to use public rpcs as fallback option for 'srouter' and 'router' mode
USE_PUBLIC_RPCS="true"

# Optional seconds to wait for the transaction to mine before disregarding it
TIMEOUT=""
```
If both env variables and CLI argument are set, the CLI arguments will be prioritized and override the env variables.

Expand All @@ -213,6 +223,8 @@ const configOptions = {
maxProfit : true, // option to maximize profit for 'srouter' mode
maxRatio : true, // option to maximize the maxIORatio in "srouter" mode
usePublicRpcs : false, // option to fallback to public rpcs
flashbotRpc : "https://flashbot-rpc-url", // Optional Flashbot RPC URL
timeout : 300, // seconds to wait for tx to mine before disregarding it
liquidityProviders : [ // list of liquidity providers for "router" mode to get quotes from (optional)
"sushiswapv2",
"uniswapv2"
Expand Down
13 changes: 9 additions & 4 deletions arb-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const DEFAULT_OPTIONS = {
maxProfit : process?.env?.MAX_PROFIT?.toLowerCase() === "true" ? true : false,
maxRatio : process?.env?.MAX_RATIO?.toLowerCase() === "true" ? true : false,
usePublicRpcs : process?.env?.USE_PUBLIC_RPCS?.toLowerCase() === "true" ? true : false,
timeout : process?.env?.TIMEOUT,
flashbotRpc : process?.env?.FLASHBOT_RPC,
rpc : process?.env?.RPC_URL
? Array.from(process?.env?.RPC_URL.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined,
flashBotRpc : process?.env?.FLASHBOT_RPC,
subgraph : process?.env?.SUBGRAPH
? Array.from(process?.env?.SUBGRAPH.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined
Expand All @@ -57,9 +58,10 @@ const getOptions = async argv => {
.option("--order-interpreter <address>", "Option to filter the subgraph query results with a specific order's interpreter address, Will override the 'ORDER_INTERPRETER' in env variables")
.option("--monthly-ratelimit <integer>", "0x monthly rate limit, if not specified will not respect any 0x monthly ratelimit, Will override the 'MONTHLY_RATELIMIT' in env variables")
.option("--sleep <integer>", "Seconds to wait between each arb round, default is 10, Will override the 'SLEPP' in env variables")
.option("--flashbot-rpc <url>", "Optional flashbot rpc url to submit transaction to, Will override the 'FLASHBOT_RPC' in env variables")
.option("--timeout <integer>", "Optional seconds to wait for the transaction to mine before disregarding it, Will override the 'TIMEOUT' in env variables")
.option("--max-profit", "Option to maximize profit for 'srouter' mode, comes at the cost of more RPC calls, Will override the 'MAX_PROFIT' in env variables")
.option("--max-ratio", "Option to maximize maxIORatio for 'srouter' mode, Will override the 'MAX_RATIO' in env variables")
.option("--flash-bot-rpc <flashbot url>", "Optional flashbot url to submit transaction to.")
.option("--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")
.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 @@ -91,7 +93,9 @@ const getOptions = async argv => {
cmdOptions.maxProfit = cmdOptions.maxProfit || DEFAULT_OPTIONS.maxProfit;
cmdOptions.maxRatio = cmdOptions.maxRatio || DEFAULT_OPTIONS.maxRatio;
cmdOptions.usePublicRpcs = cmdOptions.usePublicRpcs || DEFAULT_OPTIONS.usePublicRpcs;
cmdOptions.flashBotRpc = cmdOptions.flashBotRpc || DEFAULT_OPTIONS.flashBotRpc;
cmdOptions.flashbotRpc = cmdOptions.flashbotRpc || DEFAULT_OPTIONS.flashbotRpc;
cmdOptions.timeout = cmdOptions.timeout || DEFAULT_OPTIONS.timeout;


return cmdOptions;
};
Expand All @@ -116,9 +120,10 @@ const arbRound = async options => {
maxProfit : options.maxProfit,
maxRatio : options.maxRatio,
usePublicRpcs : options.usePublicRpcs,
flashBotRpc : options.flashBotRpc,
flashbotRpc : options.flashbotRpc,
hideSensitiveData : false,
shortenLargeLogs : false,
timeout : options.timeout,
liquidityProviders : options.lps
? Array.from(options.lps.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined,
Expand Down
22 changes: 19 additions & 3 deletions docs/html/crouter.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ <h1 class="page-title">Source: crouter.js</h1>
getDataFetcher,
getActualPrice,
visualizeRoute,
promiseTimeout,
bundleTakeOrders,
createViemClient
} = require("./utils");
Expand Down Expand Up @@ -191,6 +192,12 @@ <h1 class="page-title">Source: crouter.js</h1>
const arbAddress = config.arbAddress;
const orderbookAddress = config.orderbookAddress;
const arbType = config.arbType;
const flashbotSigner = config.flashbotRpc
? new ethers.Wallet(
signer.privateKey,
new ethers.providers.JsonRpcProvider(config.flashbotRpc)
)
: undefined;

// instantiating arb contract
const arb = new ethers.Contract(arbAddress, arbAbis[arbType], signer);
Expand Down Expand Up @@ -676,13 +683,22 @@ <h1 class="page-title">Source: crouter.js</h1>
]
);
console.log("Block Number: " + await signer.provider.getBlockNumber(), "\n");
const tx = await signer.sendTransaction(rawtx);
const tx = flashbotSigner !== undefined
? await flashbotSigner.sendTransaction(rawtx)
: await signer.sendTransaction(rawtx);
console.log("\x1b[33m%s\x1b[0m", config.explorer + "tx/" + tx.hash, "\n");
console.log(
">>> Transaction submitted successfully to the network, waiting for transaction to mine...",
"\n"
);
const receipt = await tx.wait();

const receipt = config.timeout
? await promiseTimeout(
tx.wait(),
config.timeout,
`Transaction failed to mine after ${config.timeout}ms`
)
: await tx.wait();
const income = getIncome(signer, receipt);
const clearActualPrice = getActualPrice(
receipt,
Expand Down Expand Up @@ -805,7 +821,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Mon Oct 09 2023 22:15:39 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Sat Oct 28 2023 15:50:37 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
23 changes: 19 additions & 4 deletions docs/html/curve.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ <h1 class="page-title">Source: curve.js</h1>
getEthPrice,
getDataFetcher,
getActualPrice,
promiseTimeout,
bundleTakeOrders,
createViemClient
createViemClient,
} = require("./utils");

/**
Expand Down Expand Up @@ -241,6 +242,12 @@ <h1 class="page-title">Source: curve.js</h1>
const arbAddress = config.arbAddress;
const orderbookAddress = config.orderbookAddress;
const arbType = config.arbType;
const flashbotSigner = config.flashbotRpc
? new ethers.Wallet(
signer.privateKey,
new ethers.providers.JsonRpcProvider(config.flashbotRpc)
)
: undefined;

// instantiating arb contract
const arb = new ethers.Contract(arbAddress, arbAbis[arbType], signer);
Expand Down Expand Up @@ -564,14 +571,22 @@ <h1 class="page-title">Source: curve.js</h1>
]
);
console.log("Block Number: " + await signer.provider.getBlockNumber(), "\n");
const tx = await signer.sendTransaction(rawtx);
const tx = flashbotSigner !== undefined
? await flashbotSigner.sendTransaction(rawtx)
: await signer.sendTransaction(rawtx);
console.log("\x1b[33m%s\x1b[0m", config.explorer + "tx/" + tx.hash, "\n");
console.log(
">>> Transaction submitted successfully to the network, waiting for transaction to mine...",
"\n"
);

const receipt = await tx.wait();
const receipt = config.timeout
? await promiseTimeout(
tx.wait(),
config.timeout,
`Transaction failed to mine after ${config.timeout}ms`
)
: await tx.wait();
const income = getIncome(signer, receipt);
const clearActualPrice = getActualPrice(
receipt,
Expand Down Expand Up @@ -693,7 +708,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Mon Oct 09 2023 22:15:39 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Sat Oct 28 2023 15:50:37 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit de02d8e

Please sign in to comment.