From 7e68881736727083b04c05ac81faa581b0c73e62 Mon Sep 17 00:00:00 2001 From: OxMarco <> Date: Thu, 17 Oct 2024 16:59:15 +0200 Subject: [PATCH] return data needed for the swap --- src/aggregator.ts | 62 ++++++++++++++++++++++++++++------------- src/velar.ts | 2 +- test/aggregator.test.ts | 48 +++++++++++++++++++++++++++++++ test/alex.test.ts | 4 --- 4 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 test/aggregator.test.ts diff --git a/src/aggregator.ts b/src/aggregator.ts index 1c1dce7..1ee9c9a 100644 --- a/src/aggregator.ts +++ b/src/aggregator.ts @@ -11,28 +11,50 @@ export async function getBestPath( tokenOutAddress: string, network: "mainnet" | "testnet", ) { - const velarAmtOut = await velarQuoter( - velar, - amtIn, - tokenInName, - tokenInAddress, - tokenOutName, - tokenOutAddress, - network, - ); + let velarAmtOut; - const alexAmtOut = await alexQuoter( - alex, - amtIn, - tokenInName, - tokenInAddress, - tokenOutName, - tokenOutAddress, - network, - ); + try { + velarAmtOut = await velarQuoter( + velar, + amtIn, + tokenInName, + tokenInAddress, + tokenOutName, + tokenOutAddress, + network, + ); + } catch { + velarAmtOut = 0n; + } + + let alexAmtOut; + + try { + alexAmtOut = await alexQuoter( + alex, + amtIn, + tokenInName, + tokenInAddress, + tokenOutName, + tokenOutAddress, + network, + ); + } catch { + alexAmtOut = 0n; + } + + const dex = alexAmtOut > velarAmtOut ? "alex" : "velar"; + let data: any = {}; + + if (dex == "velar") + data = { + shareFeeTo: + "SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to", + }; return { - dex: alexAmtOut > velarAmtOut ? "alex" : "velar", - amtOut: Math.max(alexAmtOut, velarAmtOut), + dex, + amtOut: alexAmtOut > velarAmtOut ? alexAmtOut : velarAmtOut, + data, }; } diff --git a/src/velar.ts b/src/velar.ts index 92d3c41..3c32d83 100644 --- a/src/velar.ts +++ b/src/velar.ts @@ -24,7 +24,7 @@ export async function getVelarPoolData( }; const poolId: any = await callReadOnlyFunction(options); - if (poolId.type == 8) throw "Pool does not exist"; + if (poolId.type == 8 || poolId.type == 9) throw "Pool does not exist"; options.functionName = "lookup-pool"; const poolData: any = await callReadOnlyFunction(options); diff --git a/test/aggregator.test.ts b/test/aggregator.test.ts new file mode 100644 index 0000000..9acd146 --- /dev/null +++ b/test/aggregator.test.ts @@ -0,0 +1,48 @@ +import { getBestPath } from "../src"; +import { + aeusdcContractAddress, + aeusdcContractName, + alex, + ststxContractAddress, + ststxContractName, + susdtContractAddress, + susdtContractName, + velar, + wstxContractAddress, + wstxContractName, +} from "./constants"; + +describe("Aggregator quoter", () => { + it("alex is the best path", async () => { + const res = await getBestPath( + velar, + alex, + 100n, + wstxContractName, + wstxContractAddress, + susdtContractName, + susdtContractAddress, + "mainnet", + ); + + expect(res.dex).toBe("alex"); + }); + + it("velar is the best path", async () => { + const res = await getBestPath( + velar, + alex, + 100n, + ststxContractName, + ststxContractAddress, + aeusdcContractName, + aeusdcContractAddress, + "mainnet", + ); + + expect(res.dex).toBe("velar"); + expect(res.data?.shareFeeTo).toBe( + "SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to", + ); + }); +}); diff --git a/test/alex.test.ts b/test/alex.test.ts index 2667c98..92625f5 100644 --- a/test/alex.test.ts +++ b/test/alex.test.ts @@ -1,10 +1,6 @@ import { alexQuoter, getAlexPoolData } from "../src"; import { - aeusdcContractAddress, - aeusdcContractName, alex, - ststxContractAddress, - ststxContractName, susdtContractAddress, susdtContractName, wstxContractAddress,