Skip to content

Commit 768e1a3

Browse files
committed
update
1 parent 434a92a commit 768e1a3

File tree

16 files changed

+269
-323
lines changed

16 files changed

+269
-323
lines changed

scripts/sweep.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { routeProcessor3Abi } from "../src/abis";
88
import { setWatchedTokens } from "../src/account";
99
import { Native, Token, WNATIVE } from "sushi/currency";
1010
import { ROUTE_PROCESSOR_4_ADDRESS } from "sushi/config";
11-
import { createViemClient, getDataFetcher } from "../src/config";
12-
import { getRpSwap, PoolBlackList, processLps, sleep } from "../src/utils";
11+
import { getRpSwap, PoolBlackList, sleep } from "../src/utils";
12+
import { createViemClient, getDataFetcher, processLps } from "../src/config";
1313
import { HDAccount, mnemonicToAccount, PrivateKeyAccount } from "viem/accounts";
1414

1515
/**

src/abis.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const DefaultArbEvaluable = {
9797
export const TakeOrderV2EventAbi = parseAbi([orderbookAbi[13]]);
9898
export const OrderbookQuoteAbi = parseAbi([orderbookAbi[14]]);
9999
export const VaultBalanceAbi = parseAbi([orderbookAbi[3]]);
100+
export const AfterClearAbi = parseAbi([orderbookAbi[2]]);
100101
export const DeployerAbi = parseAbi(deployerAbi);
101102
export const MulticallAbi = parseAbi(multicall3Abi);
102103

src/config.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getSgOrderbooks } from "./sg";
2-
import { sendTransaction } from "./tx";
2+
// import { sendTransaction } from "./tx";
33
import { WNATIVE } from "sushi/currency";
44
import { ChainId, ChainKey } from "sushi/chain";
55
import { DataFetcher, LiquidityProviders } from "sushi/router";
@@ -31,6 +31,15 @@ import {
3131
ROUTE_PROCESSOR_3_2_ADDRESS,
3232
} from "sushi/config";
3333

34+
/**
35+
* List of liquidity provider that are excluded
36+
*/
37+
export const ExcludedLiquidityProviders = [
38+
LiquidityProviders.CurveSwap,
39+
LiquidityProviders.Camelot,
40+
LiquidityProviders.Trident,
41+
] as const;
42+
3443
/**
3544
* Get the chain config for a given chain id
3645
* @param chainId - The chain id
@@ -127,9 +136,9 @@ export async function createViemClient(
127136

128137
// set injected properties
129138
client.BUSY = false;
130-
client.sendTx = async (tx) => {
131-
return await sendTransaction(client, tx);
132-
};
139+
// client.sendTx = async (tx) => {
140+
// return await sendTransaction(client, tx);
141+
// };
133142

134143
return client;
135144
}
@@ -262,6 +271,30 @@ export async function getMetaInfo(config: BotConfig, sg: string[]): Promise<Reco
262271
}
263272
}
264273

274+
/**
275+
* Resolves an array of case-insensitive names to LiquidityProviders, ignores the ones that are not valid
276+
* @param liquidityProviders - List of liquidity providers
277+
*/
278+
export function processLps(liquidityProviders?: string[]): LiquidityProviders[] {
279+
const LP = Object.values(LiquidityProviders);
280+
if (
281+
!liquidityProviders ||
282+
!Array.isArray(liquidityProviders) ||
283+
!liquidityProviders.length ||
284+
!liquidityProviders.every((v) => typeof v === "string")
285+
) {
286+
return LP.filter((v) => !ExcludedLiquidityProviders.includes(v as any));
287+
}
288+
const lps: LiquidityProviders[] = [];
289+
for (let i = 0; i < liquidityProviders.length; i++) {
290+
const index = LP.findIndex(
291+
(v) => v.toLowerCase() === liquidityProviders[i].toLowerCase().trim(),
292+
);
293+
if (index > -1 && !lps.includes(LP[index])) lps.push(LP[index]);
294+
}
295+
return lps.length ? lps : LP.filter((v) => !ExcludedLiquidityProviders.includes(v as any));
296+
}
297+
265298
/**
266299
* Chain specific fallback data
267300
*/

src/gas.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ChainId } from "sushi";
22
import { BigNumber } from "ethers";
33
import { getQuoteConfig } from "./utils";
44
import { publicActionsL2 } from "viem/op-stack";
5-
import { encodeFunctionData, multicall3Abi, toHex } from "viem";
5+
import { encodeFunctionData, multicall3Abi } from "viem";
66
import { BotConfig, BundledOrders, OperationState, RawTx, ViemClient } from "./types";
77
import { ArbitrumNodeInterfaceAbi, ArbitrumNodeInterfaceAddress, OrderbookQuoteAbi } from "./abis";
88

@@ -103,10 +103,7 @@ export async function getQuoteGas(
103103
): Promise<bigint> {
104104
if (config.chain.id === ChainId.ARBITRUM) {
105105
// build the calldata of a quote call
106-
const quoteConfig = getQuoteConfig(orderDetails.takeOrders[0]) as any;
107-
quoteConfig.inputIOIndex = BigInt(quoteConfig.inputIOIndex);
108-
quoteConfig.outputIOIndex = BigInt(quoteConfig.outputIOIndex);
109-
quoteConfig.order.evaluable.bytecode = toHex(quoteConfig.order.evaluable.bytecode);
106+
const quoteConfig = getQuoteConfig(orderDetails.takeOrders[0]);
110107
const multicallConfig = {
111108
target: orderDetails.orderbook as `0x${string}`,
112109
allowFailure: true,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ethers } from "ethers";
33
import { ChainId } from "sushi";
44
import { versions } from "process";
55
import { PublicClient } from "viem";
6-
import { processLps } from "./utils";
76
import { DeployerAbi } from "./abis";
87
import { initAccounts } from "./account";
98
import { processOrders } from "./processOrders";
@@ -21,6 +20,7 @@ import {
2120
OperationState,
2221
} from "./types";
2322
import {
23+
processLps,
2424
getChainConfig,
2525
getDataFetcher,
2626
onFetchRequest,

src/modes/interOrderbook.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
extendSpanAttributes,
1414
} from "../utils";
1515

16+
const obInterface = new ethers.utils.Interface(orderbookAbi);
17+
1618
/**
1719
* Executes a extimateGas call for an inter-orderbook arb() tx, to determine if the tx is successfull ot not
1820
*/
@@ -65,7 +67,6 @@ export async function dryrun({
6567
);
6668

6769
// encode takeOrders2()
68-
const obInterface = new ethers.utils.Interface(orderbookAbi);
6970
const encodedFN = obInterface.encodeFunctionData("takeOrders2", [
7071
{
7172
minimumInput: ethers.constants.One,

src/modes/intraOrderbook.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
TakeOrderDetails,
1515
} from "../types";
1616

17+
const obInterface = new ethers.utils.Interface(orderbookAbi);
18+
1719
/**
1820
* Executes a extimateGas call for an intra-orderbook tx (clear2()), to determine if the tx is successfull ot not
1921
*/
@@ -51,7 +53,6 @@ export async function dryrun({
5153

5254
const inputBountyVaultId = "1";
5355
const outputBountyVaultId = "1";
54-
const obInterface = new ethers.utils.Interface(orderbookAbi);
5556
const task = {
5657
evaluable: {
5758
interpreter: config.dispair.interpreter,

src/modes/routeProcessor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,11 @@ export async function findOppWithRetries({
541541
if (allPromises.some((v) => v.status === "fulfilled")) {
542542
let choice;
543543
for (let i = 0; i < allPromises.length; i++) {
544-
// from retries, choose the one that can clear the most
545-
// ie its maxInput is the greatest
544+
// from retries, choose the one that has the highest profit
546545
const prom = allPromises[i];
547546
if (prom.status === "fulfilled") {
548547
if (!choice || choice.estimatedProfit.lt(prom.value.value!.estimatedProfit)) {
549-
// record the attributes of the choosing one
548+
// record the attributes
550549
for (const attrKey in prom.value.spanAttributes) {
551550
spanAttributes[attrKey] = prom.value.spanAttributes[attrKey];
552551
}

src/order.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { ethers } from "ethers";
22
import { SgOrder } from "./query";
33
import { Span } from "@opentelemetry/api";
4-
import { OrderV3, VaultBalanceAbi } from "./abis";
5-
import { decodeAbiParameters, erc20Abi, parseAbiParameters } from "viem";
6-
import { doQuoteTargets, QuoteTarget } from "@rainlanguage/orderbook/quote";
4+
import { OrderbookQuoteAbi, OrderV3, VaultBalanceAbi } from "./abis";
75
import { shuffleArray, sleep, addWatchedToken, getQuoteConfig } from "./utils";
6+
import {
7+
erc20Abi,
8+
encodeFunctionData,
9+
parseAbiParameters,
10+
decodeAbiParameters,
11+
decodeFunctionResult,
12+
} from "viem";
813
import {
914
Pair,
1015
Order,
@@ -648,47 +653,39 @@ export function getOrdersTokens(ordersDetails: SgOrder[]): TokenDetails[] {
648653
/**
649654
* Quotes a single order
650655
* @param orderDetails - Order details to quote
651-
* @param rpcs - RPC urls
656+
* @param viemClient - Viem client
652657
* @param blockNumber - Optional block number
653-
* @param multicallAddressOverride - Optional multicall address
658+
* @param gas - Optional read gas
654659
*/
655660
export async function quoteSingleOrder(
656661
orderDetails: BundledOrders,
657-
rpcs: string[],
662+
viemClient: ViemClient,
658663
blockNumber?: bigint,
659664
gas?: bigint,
660-
multicallAddressOverride?: string,
661665
) {
662-
for (let i = 0; i < rpcs.length; i++) {
663-
const rpc = rpcs[i];
664-
try {
665-
const quoteResult = (
666-
await doQuoteTargets(
667-
[
668-
{
669-
orderbook: orderDetails.orderbook,
670-
quoteConfig: getQuoteConfig(orderDetails.takeOrders[0]),
671-
},
672-
] as any as QuoteTarget[],
673-
rpc,
674-
blockNumber,
675-
gas,
676-
multicallAddressOverride,
677-
)
678-
)[0];
679-
if (typeof quoteResult !== "string") {
680-
orderDetails.takeOrders[0].quote = {
681-
maxOutput: ethers.BigNumber.from(quoteResult.maxOutput),
682-
ratio: ethers.BigNumber.from(quoteResult.ratio),
683-
};
684-
return;
685-
} else {
686-
return Promise.reject(`failed to quote order, reason: ${quoteResult}`);
687-
}
688-
} catch (e) {
689-
// throw only after every available rpc has been tried and failed
690-
if (i === rpcs.length - 1) throw (e as Error)?.message;
691-
}
666+
const { data } = await viemClient.call({
667+
to: orderDetails.orderbook as `0x${string}`,
668+
data: encodeFunctionData({
669+
abi: OrderbookQuoteAbi,
670+
functionName: "quote",
671+
args: [getQuoteConfig(orderDetails.takeOrders[0])],
672+
}),
673+
blockNumber,
674+
gas,
675+
});
676+
if (typeof data !== "undefined") {
677+
const quoteResult = decodeFunctionResult({
678+
abi: OrderbookQuoteAbi,
679+
functionName: "quote",
680+
data,
681+
});
682+
orderDetails.takeOrders[0].quote = {
683+
maxOutput: ethers.BigNumber.from(quoteResult[1]),
684+
ratio: ethers.BigNumber.from(quoteResult[2]),
685+
};
686+
return;
687+
} else {
688+
return Promise.reject(`Failed to quote order, reason: reqtured no data`);
692689
}
693690
}
694691

src/processOrders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { ChainId } from "sushi";
22
import { findOpp } from "./modes";
3-
import { getGasPrice, getQuoteGas } from "./gas";
43
import { PublicClient } from "viem";
54
import { Token } from "sushi/currency";
65
import { quoteSingleOrder } from "./order";
76
import { createViemClient } from "./config";
87
import { arbAbis, orderbookAbi } from "./abis";
8+
import { getGasPrice, getQuoteGas } from "./gas";
99
import { getSigner, handleTransaction } from "./tx";
1010
import { privateKeyToAccount } from "viem/accounts";
1111
import { BigNumber, Contract, ethers } from "ethers";
@@ -441,7 +441,7 @@ export async function processPair(args: {
441441
try {
442442
await quoteSingleOrder(
443443
orderPairObject,
444-
isE2eTest ? (config as any).quoteRpc : config.rpc,
444+
viemClient as any as ViemClient,
445445
undefined,
446446
isE2eTest ? config.quoteGas : await getQuoteGas(config, orderPairObject),
447447
);

0 commit comments

Comments
 (0)