Skip to content

Commit

Permalink
add maxRatio option [minor]
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Sep 8, 2023
1 parent 194a7b2 commit 4e588c6
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 113 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Other optional arguments are:
- `--monthly-ratelimit`, 0x monthly rate limit, if not specified will not respect any 0x monthly ratelimit, Will override the 'MONTHLY_RATELIMIT' in env variables
- `--sleep`, Seconds to wait between each arb round, default is 10, Will override the 'SLEPP' 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
- `-V` or `--version`, output the version number
- `-h` or `--help`, output usage information

Expand Down Expand Up @@ -116,7 +117,9 @@ 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
--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-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
-V, --version output the version number
-h, --help display help for command
<br>
Expand Down Expand Up @@ -179,6 +182,9 @@ MAX_PROFIT="true"

# Seconds to wait between each arb round, default is 10, Will override the 'SLEPP' in env variables
SLEEP=10

# Option to maximize maxIORatio for 'srouter' mode
MAX_RATIO="true"
```
If both env variables and CLI argument are set, the CLI arguments will be prioritized and override the env variables.

Expand All @@ -201,6 +207,7 @@ const configOptions = {
monthlyRatelimit : 1000000, // 0x monthly rate limit, only used for 0x mode
hideSensitiveData : true, // set to true to hide sensitive data such as wallet private key or rpc url from apearing in logs
maxProfit : true, // option to maximize profit for 'srouter' mode
maxRatio : true // option to maximize the maxIORatio in "srouter" mode
liquidityProviders : [ // list of liquidity providers for "router" mode to get quotes from (optional)
"sushiswapv2",
"uniswapv2"
Expand Down
4 changes: 4 additions & 0 deletions arb-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DEFAULT_OPTIONS = {
monthlyRatelimit : process?.env?.MONTHLY_RATELIMIT,
sleep : process?.env?.SLEEP,
maxProfit : process?.env?.MAX_PROFIT?.toLowerCase() === "true" ? true : false,
maxRatio : process?.env?.MAX_RATIO?.toLowerCase() === "true" ? true : false,
rpc : process?.env?.RPC_URL
? Array.from(process?.env?.RPC_URL.matchAll(/[^,\s]+/g)).map(v => v[0])
: undefined,
Expand Down Expand Up @@ -55,6 +56,7 @@ const getOptions = async argv => {
.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("--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")
.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 @@ -83,6 +85,7 @@ const getOptions = async argv => {
cmdOptions.orderInterpreter = cmdOptions.orderInterpreter || DEFAULT_OPTIONS.orderInterpreter;
cmdOptions.monthlyRatelimit = cmdOptions.monthlyRatelimit || DEFAULT_OPTIONS.monthlyRatelimit;
cmdOptions.maxProfit = cmdOptions.maxProfit || DEFAULT_OPTIONS.maxProfit;
cmdOptions.maxRatio = cmdOptions.maxRatio || DEFAULT_OPTIONS.maxRatio;

return cmdOptions;
};
Expand All @@ -105,6 +108,7 @@ const arbRound = async options => {
zeroExApiKey : options.apiKey,
monthlyRatelimit : options.monthlyRatelimit,
maxProfit : options.maxProfit,
maxRatio : options.maxRatio,
hideSensitiveData : false,
shortenLargeLogs : false,
liquidityProviders : options.lps
Expand Down
2 changes: 1 addition & 1 deletion docs/html/curve.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
10 changes: 5 additions & 5 deletions docs/html/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ <h4 class="name" id="clearOptions"><span class="type-signature">(constant) </spa

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line48">line 48</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line52">line 52</a>
</li></ul></dd>


Expand Down Expand Up @@ -1205,7 +1205,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line191">line 191</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line196">line 196</a>
</li></ul></dd>


Expand Down Expand Up @@ -2891,7 +2891,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line141">line 141</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line145">line 145</a>
</li></ul></dd>


Expand Down Expand Up @@ -3666,7 +3666,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line69">line 69</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line73">line 73</a>
</li></ul></dd>


Expand Down Expand Up @@ -7613,7 +7613,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
9 changes: 7 additions & 2 deletions docs/html/index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ <h1 class="page-title">Source: index.js</h1>
/**
* Maximize profit for "srouter" mode, comes at the cost of RPC calls
*/
maxProfit: false
maxProfit: false,
/**
* Maximize maxIORatio for "srouter" mode, comes at the cost of RPC calls
*/
maxRatio: false
};

/**
Expand Down Expand Up @@ -204,6 +208,7 @@ <h1 class="page-title">Source: index.js</h1>
config.apiKey = options?.zeroExApiKey;
config.monthlyRatelimit = options?.monthlyRatelimit;
config.maxProfit = !!options?.maxProfit;
config.maxRatio = !!options?.maxRatio;
return config;
};

Expand Down Expand Up @@ -295,7 +300,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/query.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
32 changes: 9 additions & 23 deletions docs/html/router.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1 class="page-title">Source: router.js</h1>
getEthPrice,
getDataFetcher,
getActualPrice,
// estimateProfit,
visualizeRoute,
bundleTakeOrders,
fetchPoolsForTokenWrapper
} = require("./utils");
Expand Down Expand Up @@ -326,27 +326,13 @@ <h1 class="page-title">Source: router.js</h1>
// // poolFilter
// );
// if (route.status == "NoWay") throw "could not find any route for this token pair";
let routeText = "";
route.legs.forEach((v, i) => {
if (i === 0) routeText =
routeText +
v.tokenTo.symbol +
"/" +
v.tokenFrom.symbol +
"(" +
v.poolName +
")";
else routeText =
routeText +
" + " +
v.tokenTo.symbol +
"/" +
v.tokenFrom.symbol +
"(" +
v.poolName +
")";
});
console.log(">>> Route portions: ", routeText, "\n");
console.log(">>> Route portions: ", "\n");
console.log(
"\x1b[36m%s\x1b[0m",
visualizeRoute(fromToken.address, toToken.address, route.legs),
"\n"
);

const rpParams = Router.routeProcessor2Params(
pcMap,
route,
Expand Down Expand Up @@ -612,7 +598,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
39 changes: 13 additions & 26 deletions docs/html/srouter.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ <h1 class="page-title">Source: srouter.js</h1>
getEthPrice,
getDataFetcher,
getActualPrice,
// estimateProfit,
visualizeRoute,
bundleTakeOrders,
fetchPoolsForTokenWrapper,
getActualClearAmount
getActualClearAmount,
fetchPoolsForTokenWrapper
} = require("./utils");


Expand Down Expand Up @@ -147,6 +147,7 @@ <h1 class="page-title">Source: srouter.js</h1>
const arbAddress = config.arbAddress;
const orderbookAddress = config.orderbookAddress;
const maxProfit = config.maxProfit;
const maxRatio = config.maxRatio;

// instantiating arb contract
const arb = new ethers.Contract(arbAddress, arbAbis["srouter"], signer);
Expand Down Expand Up @@ -267,27 +268,13 @@ <h1 class="page-title">Source: srouter.js</h1>
);
const price = rateFixed.mul("1" + "0".repeat(18)).div(maximumInputFixed);
console.log(`Current best route price for this token pair: ${ethers.utils.formatEther(price)}`, "\n");
let routeText = "";
route.legs.forEach((v, i) => {
if (i === 0) routeText =
routeText +
v.tokenTo.symbol +
"/" +
v.tokenFrom.symbol +
"(" +
v.poolName +
")";
else routeText =
routeText +
" + " +
v.tokenTo.symbol +
"/" +
v.tokenFrom.symbol +
"(" +
v.poolName +
")";
});
console.log(">>> Route portions: ", routeText, "\n");
console.log(">>> Route portions: ", "\n");
console.log(
"\x1b[36m%s\x1b[0m",
visualizeRoute(fromToken.address, toToken.address, route.legs),
"\n"
);

const rpParams = Router.routeProcessor2Params(
pcMap,
route,
Expand All @@ -301,7 +288,7 @@ <h1 class="page-title">Source: srouter.js</h1>
const takeOrdersConfigStruct = {
minimumInput: ethers.constants.One,
maximumInput,
maximumIORatio: price,
maximumIORatio: maxRatio ? ethers.constants.MaxUint256 : price,
orders: bundledOrders[i].takeOrders.map(v => v.takeOrder),
data: ethers.utils.defaultAbiCoder.encode(
["bytes"],
Expand Down Expand Up @@ -542,7 +529,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
42 changes: 40 additions & 2 deletions docs/html/utils.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,43 @@ <h1 class="page-title">Source: utils.js</h1>
}
};

const visualizeRoute = (fromToken, toToken, legs) => {
return [
...legs.filter(
v => v.tokenTo.address.toLowerCase() === toToken.toLowerCase() &amp;&amp;
v.tokenFrom.address.toLowerCase() === fromToken.toLowerCase()
).map(v => [v]),

...legs.filter(
v => v.tokenFrom.address.toLowerCase() === fromToken.toLowerCase() &amp;&amp;
v.tokenTo.address.toLowerCase() !== toToken.toLowerCase()
).map(v => {
const portoin = [v];
while(portoin.at(-1).tokenTo.address.toLowerCase() !== toToken.toLowerCase()) {
portoin.push(
legs.find(e =>
e.tokenFrom.address.toLowerCase() ===
portoin.at(-1).tokenTo.address.toLowerCase()
)
);
}
return portoin;
})

].sort(
(a, b) => b[0].absolutePortion - a[0].absolutePortion
).map(
v => (v[0].absolutePortion * 100).toFixed(2).padStart(5, "0") + "% ---> " +
v.map(
e => e.tokenTo.symbol + "/" + e.tokenFrom.symbol + " (" + e.poolName + ")"
).join(
" >> "
)
).join(
"\n"
);
};

module.exports = {
fallbackTransports,
bnFromFloat,
Expand All @@ -1261,7 +1298,8 @@ <h1 class="page-title">Source: utils.js</h1>
appGlobalLogger,
promiseTimeout,
getActualClearAmount,
getRouteForTokens
getRouteForTokens,
visualizeRoute
};
</code></pre>
</article>
Expand All @@ -1279,7 +1317,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/html/zeroex.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,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 Thu Sep 07 2023 16:17:56 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Sep 08 2023 16:28:57 GMT+0000 (Coordinated Universal Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
5 changes: 4 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ ARB_TYPE="flash-loan-v2"
MAX_PROFIT="true"

# Seconds to wait between each arb round, default is 10, Will override the 'SLEPP' in env variables
SLEEP=10
SLEEP=10

# Option to maximize maxIORatio for 'srouter' mode
MAX_RATIO="true"
Loading

0 comments on commit 4e588c6

Please sign in to comment.