Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Jan 29, 2025
1 parent 7978e46 commit 50f9ca3
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 99 deletions.
77 changes: 77 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,83 @@ export async function getDataFetcher(
}
}

/**
* Get the bounty check ensure task bytecode
* @param inputToEthPrice - Input token to Eth price
* @param outputToEthPrice - Output token to Eth price
* @param minimumExcepted - Minimum expected amount
*/
export function getBountyEnsureBytecode(
inputToEthPrice: BigNumber,
outputToEthPrice: BigNumber,
minimumExcepted: BigNumber,
sender: string,
): string {
const inputPrice = inputToEthPrice.toHexString().substring(2).padStart(64, "0");
const outputPrice = outputToEthPrice.toHexString().substring(2).padStart(64, "0");
const minimum = minimumExcepted.toHexString().substring(2).padStart(64, "0");
const msgSender = sender.substring(2).padStart(64, "0").toLowerCase();
// rainlang bytecode:
// :ensure(equal-to(sender context<0 0>()) \"unknown sender\"),
// :ensure(
// greater-than-or-equal-to(
// add(
// mul(inputToEthPrice context<1 0>())
// mul(outputToEthPrice context<1 1>())
// )
// minimumExcepted
// )
// \"minimum sender output\"
// );
return `0x0000000000000000000000000000000000000000000000000000000000000006${msgSender}8e756e6b6e6f776e2073656e6465720000000000000000000000000000000000${inputPrice}${outputPrice}${minimum}956d696e696d756d2073656e646572206f7574707574000000000000000000000000000000000000000000000000000000000000000000000000000000000047010000100500000110000103100000011000001e1200001d020000011000050110000403100101011000033d12000003100001011000023d1200002b120000211200001d020000`;
}

/**
* Get the bounty check ensure task bytecode for clear2 withdraw
* @param botAddress - Bot wallet address
* @param inputToken - Input token address
* @param outputToken - Output token address
* @param orgInputBalance - Input token original balance
* @param orgOutputBalance - Output token original balance
* @param inputToEthPrice - Input token to Eth price
* @param outputToEthPrice - Output token to Eth price
* @param minimumExcepted - Minimum expected amount
*/
export function getWithdrawEnsureBytecode(
botAddress: string,
inputToken: string,
outputToken: string,
orgInputBalance: BigNumber,
orgOutputBalance: BigNumber,
inputToEthPrice: BigNumber,
outputToEthPrice: BigNumber,
minimumExcepted: BigNumber,
sender: string,
): string {
const bot = botAddress.substring(2).padStart(64, "0");
const input = inputToken.substring(2).padStart(64, "0");
const output = outputToken.substring(2).padStart(64, "0");
const inputBalance = orgInputBalance.toHexString().substring(2).padStart(64, "0");
const outputBalance = orgOutputBalance.toHexString().substring(2).padStart(64, "0");
const inputPrice = inputToEthPrice.toHexString().substring(2).padStart(64, "0");
const outputPrice = outputToEthPrice.toHexString().substring(2).padStart(64, "0");
const minimum = minimumExcepted.toHexString().substring(2).padStart(64, "0");
const msgSender = sender.substring(2).padStart(64, "0").toLowerCase();
// rainlang bytecode:
// :ensure(equal-to(sender context<0 0>()) \"unknown sender\"),
// :ensure(
// greater-than-or-equal-to(
// add(
// mul(sub(erc20-balance-of(inputToken botAddress) originalInputBalance) inputToEthPrice)
// mul(sub(erc20-balance-of(outputToken botAddress) originalOutputBalance) outputToEthPrice)
// )
// minimumSenderOutput
// )
// \"minimumSenderOutput\"
// );
return `0x000000000000000000000000000000000000000000000000000000000000000b${msgSender}8e756e6b6e6f776e2073656e6465720000000000000000000000000000000000${input}${bot}${inputBalance}${inputPrice}${output}${outputBalance}${outputPrice}${minimum}936d696e696d756d53656e6465724f75747075740000000000000000000000000000000000000000000000000000000000000000000000000000000000000067010000180700000110000103100000011000001e1200001d0200000110000a011000090110000801100007011000030110000611120000471200003d1200000110000501100004011000030110000211120000471200003d1200002b120000211200001d020000`;
}

/**
* List of L2 chains that require SEPARATE L1 gas actions.
* other L2 chains that dont require separate L1 gas actions
Expand Down
44 changes: 16 additions & 28 deletions src/modes/interOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { estimateGasCost } from "../gas";
import { BaseError, PublicClient } from "viem";
import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { getBountyEnsureRainlang, parseRainlang } from "../config";
import { getBountyEnsureBytecode } from "../config";
import { BotConfig, BundledOrders, ViemClient, DryrunResult, SpanAttrs } from "../types";
import {
ONE18,
Expand Down Expand Up @@ -93,15 +93,11 @@ export async function dryrun({
bytecode:
config.gasCoveragePercentage === "0"
? "0x"
: await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
ethers.constants.Zero,
signer.account.address,
),
config.viemClient,
config.dispair,
: getBountyEnsureBytecode(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
ethers.constants.Zero,
signer.account.address,
),
},
signedContext: [],
Expand Down Expand Up @@ -153,15 +149,11 @@ export async function dryrun({
// sender output which is already called above
if (config.gasCoveragePercentage !== "0") {
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(headroom).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getBountyEnsureBytecode(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(headroom).div("100"),
signer.account.address,
);
rawtx.data = arb.interface.encodeFunctionData("arb3", [
orderPairObject.orderbook,
Expand All @@ -177,15 +169,11 @@ export async function dryrun({
.div(100);
rawtx.gas = gasLimit.toBigInt();
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getBountyEnsureBytecode(
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
);
rawtx.data = arb.interface.encodeFunctionData("arb3", [
orderPairObject.orderbook,
Expand Down
74 changes: 31 additions & 43 deletions src/modes/intraOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { estimateGasCost } from "../gas";
import { BigNumber, ethers } from "ethers";
import { BaseError, erc20Abi, PublicClient } from "viem";
import { containsNodeError, errorSnapshot } from "../error";
import { getWithdrawEnsureRainlang, parseRainlang } from "../config";
import { getWithdrawEnsureBytecode } from "../config";
import { estimateProfit, scale18, withBigintSerializer } from "../utils";
import {
SpanAttrs,
Expand Down Expand Up @@ -56,20 +56,16 @@ export async function dryrun({
evaluable: {
interpreter: config.dispair.interpreter,
store: config.dispair.store,
bytecode: await parseRainlang(
await getWithdrawEnsureRainlang(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
ethers.constants.Zero,
signer.account.address,
),
config.viemClient,
config.dispair,
bytecode: getWithdrawEnsureBytecode(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
ethers.constants.Zero,
signer.account.address,
),
},
signedContext: [],
Expand Down Expand Up @@ -146,20 +142,16 @@ export async function dryrun({
// sender output which is already called above
if (config.gasCoveragePercentage !== "0") {
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
task.evaluable.bytecode = await parseRainlang(
await getWithdrawEnsureRainlang(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(headroom).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getWithdrawEnsureBytecode(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(headroom).div("100"),
signer.account.address,
);
withdrawOutputCalldata = obInterface.encodeFunctionData("withdraw2", [
orderPairObject.sellToken,
Expand All @@ -179,20 +171,16 @@ export async function dryrun({
.div(100);
rawtx.gas = gasLimit.toBigInt();
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
task.evaluable.bytecode = await parseRainlang(
await getWithdrawEnsureRainlang(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getWithdrawEnsureBytecode(
signer.account.address,
orderPairObject.buyToken,
orderPairObject.sellToken,
inputBalance,
outputBalance,
ethers.utils.parseUnits(inputToEthPrice),
ethers.utils.parseUnits(outputToEthPrice),
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
);
withdrawOutputCalldata = obInterface.encodeFunctionData("withdraw2", [
orderPairObject.sellToken,
Expand Down
44 changes: 16 additions & 28 deletions src/modes/routeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseError, PublicClient } from "viem";
import { ChainId, DataFetcher, Router } from "sushi";
import { BigNumber, Contract, ethers } from "ethers";
import { containsNodeError, errorSnapshot } from "../error";
import { getBountyEnsureRainlang, parseRainlang } from "../config";
import { getBountyEnsureBytecode } from "../config";
import { SpanAttrs, BotConfig, ViemClient, DryrunResult, BundledOrders } from "../types";
import {
ONE18,
Expand Down Expand Up @@ -155,15 +155,11 @@ export async function dryrun({
bytecode:
config.gasCoveragePercentage === "0"
? "0x"
: await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
ethers.constants.Zero,
signer.account.address,
),
config.viemClient,
config.dispair,
: getBountyEnsureBytecode(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
ethers.constants.Zero,
signer.account.address,
),
},
signedContext: [],
Expand Down Expand Up @@ -219,15 +215,11 @@ export async function dryrun({
// sender output which is already called above
if (config.gasCoveragePercentage !== "0") {
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
gasCost.mul(headroom).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getBountyEnsureBytecode(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
gasCost.mul(headroom).div("100"),
signer.account.address,
);
rawtx.data = arb.interface.encodeFunctionData("arb3", [
orderPairObject.orderbook,
Expand All @@ -243,15 +235,11 @@ export async function dryrun({
.div(100);
rawtx.gas = gasLimit.toBigInt();
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
task.evaluable.bytecode = await parseRainlang(
await getBountyEnsureRainlang(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
),
config.viemClient,
config.dispair,
task.evaluable.bytecode = getBountyEnsureBytecode(
ethers.utils.parseUnits(ethPrice),
ethers.constants.Zero,
gasCost.mul(config.gasCoveragePercentage).div("100"),
signer.account.address,
);
rawtx.data = arb.interface.encodeFunctionData("arb3", [
orderPairObject.orderbook,
Expand Down

0 comments on commit 50f9ca3

Please sign in to comment.