Skip to content

Commit

Permalink
return data needed for the swap
Browse files Browse the repository at this point in the history
  • Loading branch information
OxMarco committed Oct 17, 2024
1 parent 0cf53c1 commit 7e68881
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
62 changes: 42 additions & 20 deletions src/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
2 changes: 1 addition & 1 deletion src/velar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions test/aggregator.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
});
4 changes: 0 additions & 4 deletions test/alex.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { alexQuoter, getAlexPoolData } from "../src";
import {
aeusdcContractAddress,
aeusdcContractName,
alex,
ststxContractAddress,
ststxContractName,
susdtContractAddress,
susdtContractName,
wstxContractAddress,
Expand Down

0 comments on commit 7e68881

Please sign in to comment.