|
1 | 1 | import { ChainId, RPParams } from "sushi"; |
2 | 2 | import { BigNumber, ethers } from "ethers"; |
| 3 | +import { erc20Abi, PublicClient } from "viem"; |
3 | 4 | import { estimateGasCost, getTxFee } from "./gas"; |
4 | 5 | import { ErrorSeverity, errorSnapshot } from "./error"; |
5 | 6 | import { Native, Token, WNATIVE } from "sushi/currency"; |
6 | 7 | import { ROUTE_PROCESSOR_4_ADDRESS } from "sushi/config"; |
7 | | -import { getRpSwap, PoolBlackList, sleep } from "./utils"; |
8 | 8 | import { createViemClient, getDataFetcher } from "./config"; |
9 | 9 | import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts"; |
10 | | -import { erc20Abi, multicall3Abi, orderbookAbi, routeProcessor3Abi } from "./abis"; |
| 10 | +import { getRpSwap, PoolBlackList, sleep, addWatchedToken } from "./utils"; |
11 | 11 | import { context, Context, SpanStatusCode, trace, Tracer } from "@opentelemetry/api"; |
12 | | -import { parseAbi, hexToNumber, numberToHex, PublicClient, NonceManagerSource } from "viem"; |
| 12 | +import { MulticallAbi, orderbookAbi, routeProcessor3Abi, VaultBalanceAbi } from "./abis"; |
13 | 13 | import { |
14 | 14 | BotConfig, |
15 | 15 | CliOptions, |
16 | 16 | ViemClient, |
17 | 17 | OwnedOrder, |
18 | 18 | TokenDetails, |
19 | 19 | OperationState, |
| 20 | + BundledOrders, |
20 | 21 | } from "./types"; |
21 | 22 |
|
22 | 23 | /** Standard base path for eth accounts */ |
@@ -434,7 +435,7 @@ export async function getBatchEthBalance( |
434 | 435 | viemClient.chain?.contracts?.multicall3?.address) as `0x${string}`, |
435 | 436 | allowFailure: false, |
436 | 437 | chainId: viemClient.chain.id, |
437 | | - abi: parseAbi(multicall3Abi), |
| 438 | + abi: MulticallAbi, |
438 | 439 | functionName: "getEthBalance", |
439 | 440 | args: [v], |
440 | 441 | })), |
@@ -464,7 +465,7 @@ export async function getBatchTokenBalanceForAccount( |
464 | 465 | address: v.address as `0x${string}`, |
465 | 466 | allowFailure: false, |
466 | 467 | chainId: viemClient.chain.id, |
467 | | - abi: parseAbi(erc20Abi), |
| 468 | + abi: erc20Abi, |
468 | 469 | functionName: "balanceOf", |
469 | 470 | args: [address], |
470 | 471 | })), |
@@ -886,25 +887,10 @@ export async function sweepToEth( |
886 | 887 | } |
887 | 888 | } |
888 | 889 |
|
889 | | -export async function setWatchedTokens(account: ViemClient, watchedTokens: TokenDetails[]) { |
| 890 | +export function setWatchedTokens(account: ViemClient, watchedTokens: TokenDetails[]) { |
890 | 891 | account.BOUNTY = [...watchedTokens]; |
891 | 892 | } |
892 | 893 |
|
893 | | -export function addWatchedToken( |
894 | | - token: TokenDetails, |
895 | | - watchedTokens: TokenDetails[], |
896 | | - account?: ViemClient, |
897 | | -) { |
898 | | - if (!watchedTokens.find((v) => v.address.toLowerCase() === token.address.toLowerCase())) { |
899 | | - watchedTokens.push(token); |
900 | | - } |
901 | | - if (account) { |
902 | | - if (!account.BOUNTY.find((v) => v.address.toLowerCase() === token.address.toLowerCase())) { |
903 | | - account.BOUNTY.push(token); |
904 | | - } |
905 | | - } |
906 | | -} |
907 | | - |
908 | 894 | /** |
909 | 895 | * Funds the sepcified bot owned orders from the gas token |
910 | 896 | * @param ownedOrders |
@@ -1082,35 +1068,93 @@ export async function fundOwnedOrders( |
1082 | 1068 | } |
1083 | 1069 |
|
1084 | 1070 | /** |
1085 | | - * Creates a viem account nonce source of truth with "latest" block tag used for reading, |
1086 | | - * viem's default nonce manager uses "pending" block tag to read the nonce of an account |
| 1071 | + * Quotes order details that are already fetched and bundled by bundleOrder() |
| 1072 | + * @param config - Config obj |
| 1073 | + * @param orderDetails - Order details to quote |
| 1074 | + * @param multicallAddressOverride - Optional multicall address |
1087 | 1075 | */ |
1088 | | -export function noneSource(): NonceManagerSource { |
1089 | | - return { |
1090 | | - async get(parameters) { |
1091 | | - const { address, client } = parameters; |
1092 | | - return getTransactionCount(client as any, { |
1093 | | - address, |
1094 | | - blockTag: "latest", |
| 1076 | +export async function checkOwnedOrders( |
| 1077 | + config: BotConfig, |
| 1078 | + orderDetails: BundledOrders[][], |
| 1079 | + multicallAddressOverride?: string, |
| 1080 | +): Promise<OwnedOrder[]> { |
| 1081 | + const ownedOrders: any[] = []; |
| 1082 | + const result: OwnedOrder[] = []; |
| 1083 | + orderDetails.flat().forEach((v) => { |
| 1084 | + v.takeOrders.forEach((order) => { |
| 1085 | + if ( |
| 1086 | + order.takeOrder.order.owner.toLowerCase() === |
| 1087 | + config.mainAccount.account.address.toLowerCase() && |
| 1088 | + !ownedOrders.find( |
| 1089 | + (e) => |
| 1090 | + e.orderbook.toLowerCase() === v.orderbook.toLowerCase() && |
| 1091 | + e.outputToken.toLowerCase() === v.sellToken.toLowerCase() && |
| 1092 | + e.order.takeOrder.order.validOutputs[ |
| 1093 | + e.order.takeOrder.outputIOIndex |
| 1094 | + ].token.toLowerCase() == |
| 1095 | + order.takeOrder.order.validOutputs[ |
| 1096 | + order.takeOrder.outputIOIndex |
| 1097 | + ].token.toLowerCase() && |
| 1098 | + ethers.BigNumber.from( |
| 1099 | + e.order.takeOrder.order.validOutputs[e.order.takeOrder.outputIOIndex] |
| 1100 | + .vaultId, |
| 1101 | + ).eq( |
| 1102 | + order.takeOrder.order.validOutputs[order.takeOrder.outputIOIndex] |
| 1103 | + .vaultId, |
| 1104 | + ), |
| 1105 | + ) |
| 1106 | + ) { |
| 1107 | + ownedOrders.push({ |
| 1108 | + order, |
| 1109 | + orderbook: v.orderbook, |
| 1110 | + outputSymbol: v.sellTokenSymbol, |
| 1111 | + outputToken: v.sellToken, |
| 1112 | + outputDecimals: v.sellTokenDecimals, |
| 1113 | + }); |
| 1114 | + } |
| 1115 | + }); |
| 1116 | + }); |
| 1117 | + if (!ownedOrders.length) return result; |
| 1118 | + try { |
| 1119 | + const multicallResult = await config.viemClient.multicall({ |
| 1120 | + multicallAddress: |
| 1121 | + (multicallAddressOverride as `0x${string}` | undefined) ?? |
| 1122 | + config.viemClient.chain?.contracts?.multicall3?.address, |
| 1123 | + allowFailure: false, |
| 1124 | + contracts: ownedOrders.map((v) => ({ |
| 1125 | + address: v.orderbook, |
| 1126 | + allowFailure: false, |
| 1127 | + chainId: config.chain.id, |
| 1128 | + abi: VaultBalanceAbi, |
| 1129 | + functionName: "vaultBalance", |
| 1130 | + args: [ |
| 1131 | + // owner |
| 1132 | + v.order.takeOrder.order.owner, |
| 1133 | + // token |
| 1134 | + v.order.takeOrder.order.validOutputs[v.order.takeOrder.outputIOIndex].token, |
| 1135 | + // valut id |
| 1136 | + v.order.takeOrder.order.validOutputs[v.order.takeOrder.outputIOIndex].vaultId, |
| 1137 | + ], |
| 1138 | + })), |
| 1139 | + }); |
| 1140 | + for (let i = 0; i < multicallResult.length; i++) { |
| 1141 | + let vaultId = |
| 1142 | + ownedOrders[i].order.takeOrder.order.validOutputs[ |
| 1143 | + ownedOrders[i].order.takeOrder.outputIOIndex |
| 1144 | + ].vaultId; |
| 1145 | + if (vaultId instanceof BigNumber) vaultId = vaultId.toHexString(); |
| 1146 | + result.push({ |
| 1147 | + vaultId, |
| 1148 | + id: ownedOrders[i].order.id, |
| 1149 | + token: ownedOrders[i].outputToken, |
| 1150 | + symbol: ownedOrders[i].outputSymbol, |
| 1151 | + decimals: ownedOrders[i].outputDecimals, |
| 1152 | + orderbook: ownedOrders[i].orderbook, |
| 1153 | + vaultBalance: ethers.BigNumber.from(multicallResult[i]), |
1095 | 1154 | }); |
1096 | | - }, |
1097 | | - set() {}, |
1098 | | - }; |
1099 | | -} |
1100 | | - |
1101 | | -/** |
1102 | | - * Perfomrs a eth_getTransactionCount rpc request |
1103 | | - */ |
1104 | | -async function getTransactionCount( |
1105 | | - client: any, |
1106 | | - { address, blockTag = "latest", blockNumber }: any, |
1107 | | -): Promise<number> { |
1108 | | - const count = await client.request( |
1109 | | - { |
1110 | | - method: "eth_getTransactionCount", |
1111 | | - params: [address, blockNumber ? numberToHex(blockNumber) : blockTag], |
1112 | | - }, |
1113 | | - { dedupe: Boolean(blockNumber) }, |
1114 | | - ); |
1115 | | - return hexToNumber(count); |
| 1155 | + } |
| 1156 | + } catch (e) { |
| 1157 | + /**/ |
| 1158 | + } |
| 1159 | + return result; |
1116 | 1160 | } |
0 commit comments