Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Dec 3, 2024
1 parent 14de1f8 commit a13a0e9
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 58 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Other optional arguments are:
- `--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
- `--gas-limit-multiplier`, Option to multiply the gas limit estimation from the rpc as percentage, default is 105, ie +5%. Will override the 'GAS_LIMIT_MULTIPLIER' in env variables
- `--tx-gas`, Option to set a static gas limit for all submitting txs. Will override the 'TX_GAS' in env variables
- `--quote-gas`, Option to set a static gas limit for quote read calls, default is 1 milion. Will override the 'QUOTE_GAS' in env variables
- `-V` or `--version`, output the version number
- `-h` or `--help`, output usage information

Expand Down Expand Up @@ -267,6 +268,9 @@ GAS_LIMIT_MULTIPLIER=

# Option to set a static gas limit for all submitting txs
TX_GAS=

# Option to set a static gas limit for quote read calls, default is 1 milion
QUOTE_GAS=
```
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 @@ -94,6 +94,9 @@ GAS_LIMIT_MULTIPLIER=
# Option to set a static gas limit for all submitting txs
TX_GAS=

# Option to set a static gas limit for quote read calls, default is 1 milion
QUOTE_GAS=


# test rpcs vars
TEST_POLYGON_RPC=
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@opentelemetry/resources": "^1.22.0",
"@opentelemetry/sdk-trace-base": "^1.22.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@rainlanguage/orderbook": "^0.0.1-alpha.1",
"@rainlanguage/orderbook": "^0.0.1-alpha.6",
"axios": "^1.3.4",
"commander": "^11.0.0",
"dotenv": "^16.0.3",
Expand Down
15 changes: 15 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const ENV_OPTIONS = {
gasPriceMultiplier: process?.env?.GAS_PRICE_MULTIPLIER,
gasLimitMultiplier: process?.env?.GAS_LIMIT_MULTIPLIER,
txGas: process?.env?.TX_GAS,
quoteGas: process?.env?.QUOTE_GAS,
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 @@ -200,6 +201,10 @@ const getOptions = async (argv: any, version?: string) => {
"--tx-gas <integer>",
"Option to set a static gas limit for all submitting txs. Will override the 'TX_GAS' in env variables",
)
.option(
"--quote-gas <integer>",
"Option to set a static gas limit for quote read calls, default is 1 milion. Will override the 'QUOTE_GAS' 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 @@ -242,6 +247,7 @@ const getOptions = async (argv: any, version?: string) => {
cmdOptions.gasLimitMultiplier =
cmdOptions.gasLimitMultiplier || getEnv(ENV_OPTIONS.gasLimitMultiplier);
cmdOptions.txGas = cmdOptions.txGas || getEnv(ENV_OPTIONS.txGas);
cmdOptions.quoteGas = cmdOptions.quoteGas || getEnv(ENV_OPTIONS.quoteGas);
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 @@ -459,6 +465,15 @@ export async function startup(argv: any, version?: string, tracer?: Tracer, ctx?
throw "invalid txGas value, must be an integer greater than zero";
} else throw "invalid txGas value, must be an integer greater than zero";
}
if (options.quoteGas) {
try {
options.quoteGas = BigInt(options.quoteGas);
} catch {
throw "invalid quoteGas value, must be an integer greater than equal zero";
}
} else {
options.quoteGas = 1_000_000n; // default
}
const poolUpdateInterval = _poolUpdateInterval * 60 * 1000;
let ordersDetails: SgOrder[] = [];
if (!process?.env?.CLI_STARTUP_TEST) {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export async function getConfig(
config.gasPriceMultiplier = options.gasPriceMultiplier;
config.gasLimitMultiplier = options.gasLimitMultiplier;
config.txGas = options.txGas;
config.quoteGas = options.quoteGas;

// init accounts
const { mainAccount, accounts } = await initAccounts(walletKey, config, options, tracer, ctx);
Expand Down
21 changes: 16 additions & 5 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,23 @@ export async function findOpp({
// filter out the same owner orders
const opposingOrders = {
...v,
takeOrders: v.takeOrders.filter(
(e) =>
e.takeOrder.order.owner.toLowerCase() !==
orderPairObject.takeOrders[0].takeOrder.order.owner.toLowerCase(),
),
takeOrders: v.takeOrders
.filter(
(e) =>
e.takeOrder.order.owner.toLowerCase() !==
orderPairObject.takeOrders[0].takeOrder.order.owner.toLowerCase() &&
e.quote &&
e.quote.maxOutput.gt(0),
)
.sort((a, b) =>
a.quote!.ratio.lt(b.quote!.ratio)
? -1
: a.quote!.ratio.gt(b.quote!.ratio)
? 1
: 0,
),
};
if (!opposingOrders.takeOrders.length) throw "";
return dryrun({
orderPairObject,
opposingOrders,
Expand Down
3 changes: 3 additions & 0 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ export async function findOpp({
orderPairObject.takeOrders[0].takeOrder.order.owner.toLowerCase() &&
// only orders that (priceA x priceB < 1) can be profitbale
v.quote!.ratio.mul(orderPairObject.takeOrders[0].quote!.ratio).div(ONE).lt(ONE),
)
.sort((a, b) =>
a.quote!.ratio.lt(b.quote!.ratio) ? -1 : a.quote!.ratio.gt(b.quote!.ratio) ? 1 : 0,
);
if (!opposingOrders || !opposingOrders.length) throw undefined;

Expand Down
13 changes: 2 additions & 11 deletions src/processOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
toNumber,
getIncome,
getEthPrice,
quoteOrders,
routeExists,
PoolBlackList,
getMarketQuote,
Expand Down Expand Up @@ -151,16 +150,6 @@ export const processOrders = async (
span.end();
});

// batch quote orders to establish the orders to loop over
try {
await quoteOrders(
bundledOrders,
(config as any).isTest ? (config as any).quoteRpc : config.rpc,
);
} catch (e) {
throw errorSnapshot("Failed to batch quote orders", e);
}

let avgGasCost: BigNumber | undefined;
const reports: Report[] = [];
for (const orderbookOrders of bundledOrders) {
Expand Down Expand Up @@ -460,6 +449,8 @@ export async function processPair(args: {
await quoteSingleOrder(
orderPairObject,
(config as any).isTest ? (config as any).quoteRpc : config.rpc,
undefined,
config.quoteGas,
);
if (orderPairObject.takeOrders[0].quote?.maxOutput.isZero()) {
result.report = {
Expand Down
5 changes: 3 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export async function getOrderChanges(
timeout?: number,
span?: Span,
) {
timeout;
let skip_ = skip;
let count = 0;
const allResults: SgTx[] = [];
Expand All @@ -267,7 +268,7 @@ export async function getOrderChanges(
const res = await axios.post(
subgraph,
{ query: getTxsQuery(startTimestamp, skip_) },
{ headers: { "Content-Type": "application/json" }, timeout },
{ headers: { "Content-Type": "application/json" } },
);
if (typeof res?.data?.data?.transactions !== "undefined") {
const txs = res.data.data.transactions;
Expand All @@ -282,7 +283,7 @@ export async function getOrderChanges(
break;
}
} catch (error) {
span?.addEvent(errorSnapshot(`Failed to get order changes ${subgraph}`, error));
span?.addEvent(errorSnapshot(`Failed to get orders changes ${subgraph}`, error));
throw error;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type CliOptions = {
gasPriceMultiplier: number;
gasLimitMultiplier: number;
txGas?: bigint;
quoteGas: bigint;
};

export type TokenDetails = {
Expand Down Expand Up @@ -176,6 +177,7 @@ export type BotConfig = {
gasPriceMultiplier: number;
gasLimitMultiplier: number;
txGas?: bigint;
quoteGas: bigint;
onFetchRequest?: (request: Request) => void;
onFetchResponse?: (request: Response) => void;
};
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ export async function quoteOrders(
orderDetails: BundledOrders[][],
rpcs: string[],
blockNumber?: bigint,
gas?: bigint,
multicallAddressOverride?: string,
): Promise<BundledOrders[][]> {
let quoteResults: any[] = [];
Expand All @@ -762,6 +763,7 @@ export async function quoteOrders(
targets,
rpc,
blockNumber,
gas,
multicallAddressOverride,
);
break;
Expand Down Expand Up @@ -819,6 +821,7 @@ export async function quoteSingleOrder(
orderDetails: BundledOrders,
rpcs: string[],
blockNumber?: bigint,
gas?: bigint,
multicallAddressOverride?: string,
) {
for (let i = 0; i < rpcs.length; i++) {
Expand All @@ -834,6 +837,7 @@ export async function quoteSingleOrder(
] as any as QuoteTarget[],
rpc,
blockNumber,
gas,
multicallAddressOverride,
)
)[0];
Expand Down
71 changes: 36 additions & 35 deletions test/e2e/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,27 +527,6 @@ for (let i = 0; i < testData.length; i++) {
}

// mock quote responses
await mockServer
.forPost("/rpc")
.once()
.thenSendJsonRpcResult(
encodeQuoteResponse([
...tokens.slice(1).map((v) => [
true, // success
v.depositAmount.mul("1" + "0".repeat(18 - v.decimals)), //maxout
ethers.constants.Zero, // ratio
]),
...tokens
.slice(1)
.map(() => [
true,
tokens[0].depositAmount.mul(
"1" + "0".repeat(18 - tokens[0].decimals),
),
ethers.constants.Zero,
]),
]),
);
for (let i = 1; i < tokens.length; i++) {
const output = tokens[i].depositAmount.mul(
"1" + "0".repeat(18 - tokens[i].decimals),
Expand Down Expand Up @@ -590,6 +569,24 @@ for (let i = 0; i < testData.length; i++) {
await getOrderbookOwnersProfileMapFromSg(orders, viemClient, []),
false,
);

// mock init quotes
orders.forEach((ob) => {
ob.forEach((pair) => {
pair.takeOrders.forEach((takeOrder) => {
takeOrder.quote = {
ratio: ethers.constants.Zero,
maxOutput: tokens
.find(
(t) =>
t.contract.address.toLowerCase() ===
pair.sellToken.toLowerCase(),
)
?.depositAmount.mul("1" + "0".repeat(18 - ob.decimals)),
};
});
});
});
const { reports } = await clear(config, orders, tracer, ctx);

// should have cleared correct number of orders
Expand Down Expand Up @@ -888,20 +885,6 @@ for (let i = 0; i < testData.length; i++) {
for (let i = 0; i < tokens.length - 1; i++) {
t0.push(tokens[0]);
}
await mockServer
.forPost("/rpc")
.once()
.thenSendJsonRpcResult(
encodeQuoteResponse([
...[tokens[1], ...t0, ...tokens.slice(2)].flatMap((v) => [
[
true, // success
v.depositAmount.mul("1" + "0".repeat(18 - v.decimals)), //maxout
ethers.constants.Zero, // ratio
],
]),
]),
);
for (let i = 1; i < tokens.length; i++) {
const output = tokens[i].depositAmount.mul(
"1" + "0".repeat(18 - tokens[i].decimals),
Expand Down Expand Up @@ -943,6 +926,24 @@ for (let i = 0; i < testData.length; i++) {
await getOrderbookOwnersProfileMapFromSg(orders, viemClient, []),
false,
);

// mock init quotes
orders.forEach((ob) => {
ob.forEach((pair) => {
pair.takeOrders.forEach((takeOrder) => {
takeOrder.quote = {
ratio: ethers.constants.Zero,
maxOutput: tokens
.find(
(t) =>
t.contract.address.toLowerCase() ===
pair.sellToken.toLowerCase(),
)
?.depositAmount.mul("1" + "0".repeat(18 - ob.decimals)),
};
});
});
});
const { reports } = await clear(config, orders, tracer, ctx);

// should have cleared correct number of orders
Expand Down

0 comments on commit a13a0e9

Please sign in to comment.