Skip to content

Commit

Permalink
aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
OxMarco committed Oct 18, 2024
1 parent 7e68881 commit 6705e29
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
23 changes: 20 additions & 3 deletions src/aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { alexQuoter } from "./alex";
import { velarQuoter } from "./velar";
import { getVelarPoolData, velarQuoter } from "./velar";

export async function getBestPath(
velar: string,
Expand Down Expand Up @@ -28,9 +28,10 @@ export async function getBestPath(
}

let alexAmtOut;
let alexPoolData: any = {};

try {
alexAmtOut = await alexQuoter(
alexPoolData = await alexQuoter(
alex,
amtIn,
tokenInName,
Expand All @@ -39,18 +40,34 @@ export async function getBestPath(
tokenOutAddress,
network,
);
alexAmtOut = alexPoolData.amtOut;
} catch {
alexAmtOut = 0n;
}

const dex = alexAmtOut > velarAmtOut ? "alex" : "velar";
let data: any = {};

if (dex == "velar")
let poolData: any = {};
if (dex == "velar") {
poolData = await getVelarPoolData(
velar,
tokenInName,
tokenInAddress,
tokenOutName,
tokenOutAddress,
network,
);
data = {
...poolData,
shareFeeTo:
"SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to",
};
} else {
data = {
factor: alexPoolData.factor,
};
}

return {
dex,
Expand Down
4 changes: 2 additions & 2 deletions src/alex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function alexQuoter(
tokenOutAddress: string,
network: "mainnet" | "testnet",
sender: string = "ST2F4BK4GZH6YFBNHYDDGN4T1RKBA7DA1BJZPJEJJ",
): Promise<number> {
) {
const alexFactor0 = 100000000;
const alexFactor1 = 5000000;

Expand Down Expand Up @@ -72,5 +72,5 @@ export async function alexQuoter(
if (amtOut.type == 8) throw "Quoter contract error";
}

return amtOut.value.value;
return { amtOut: amtOut.value.value, factor: options.functionArgs[2] };
}
2 changes: 2 additions & 0 deletions src/velar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function getVelarPoolData(
poolId: poolId.value.value,
flipped: poolData.value.data.flipped.type == 4 ? false : true,
lpToken: cvToJSON(poolData.value.data.pool.data["lp-token"]).value,
token0: cvToJSON(poolData.value.data.pool.data.token0).value,
token1: cvToJSON(poolData.value.data.pool.data.token1).value,
reserve0: poolData.value.data.pool.data.reserve0.value,
reserve1: poolData.value.data.pool.data.reserve1.value,
swapFee: {
Expand Down
15 changes: 15 additions & 0 deletions test/aggregator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ describe("Aggregator quoter", () => {
"SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to",
);
});

it("extra", async () => {
const res = await getBestPath(
velar,
alex,
30438954n,
"velar-token",
"SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1",
aeusdcContractName,
aeusdcContractAddress,
"mainnet",
);

console.log(res);
});
});
4 changes: 2 additions & 2 deletions test/alex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Alex quoter", () => {
);
expect(Number(poolData.poolId)).toBe(19);

const amtOut = await alexQuoter(
const data = await alexQuoter(
alex,
100n,
wstxContractName,
Expand All @@ -30,6 +30,6 @@ describe("Alex quoter", () => {
"mainnet",
);

expect(Number(amtOut)).toBeGreaterThan(150);
expect(Number(data.amtOut)).toBeGreaterThan(150);
});
});

0 comments on commit 6705e29

Please sign in to comment.