|
1 | 1 | import { ethers } from "ethers"; |
2 | 2 | import { SgOrder } from "./query"; |
3 | 3 | 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"; |
7 | 5 | import { shuffleArray, sleep, addWatchedToken, getQuoteConfig } from "./utils"; |
| 6 | +import { |
| 7 | + erc20Abi, |
| 8 | + encodeFunctionData, |
| 9 | + parseAbiParameters, |
| 10 | + decodeAbiParameters, |
| 11 | + decodeFunctionResult, |
| 12 | +} from "viem"; |
8 | 13 | import { |
9 | 14 | Pair, |
10 | 15 | Order, |
@@ -648,47 +653,39 @@ export function getOrdersTokens(ordersDetails: SgOrder[]): TokenDetails[] { |
648 | 653 | /** |
649 | 654 | * Quotes a single order |
650 | 655 | * @param orderDetails - Order details to quote |
651 | | - * @param rpcs - RPC urls |
| 656 | + * @param viemClient - Viem client |
652 | 657 | * @param blockNumber - Optional block number |
653 | | - * @param multicallAddressOverride - Optional multicall address |
| 658 | + * @param gas - Optional read gas |
654 | 659 | */ |
655 | 660 | export async function quoteSingleOrder( |
656 | 661 | orderDetails: BundledOrders, |
657 | | - rpcs: string[], |
| 662 | + viemClient: ViemClient, |
658 | 663 | blockNumber?: bigint, |
659 | 664 | gas?: bigint, |
660 | | - multicallAddressOverride?: string, |
661 | 665 | ) { |
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`); |
692 | 689 | } |
693 | 690 | } |
694 | 691 |
|
|
0 commit comments