@jovanvuleta/test-dex-sdk
Version:
Utilities to query Stacks AMMs
226 lines (221 loc) • 7.73 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
alexQuoter: () => alexQuoter,
getAlexPoolData: () => getAlexPoolData,
getBestPath: () => getBestPath,
getVelarPoolData: () => getVelarPoolData,
velarQuoter: () => velarQuoter
});
module.exports = __toCommonJS(src_exports);
// src/velar.ts
var import_transactions = require("@stacks/transactions");
async function getVelarPoolData(velarContractAddress, tokenInName, tokenInAddress, tokenOutName, tokenOutAddress, network, sender = "ST2F4BK4GZH6YFBNHYDDGN4T1RKBA7DA1BJZPJEJJ") {
const contractName = "univ2-core";
const functionName = "get-pool-id";
const options = {
contractAddress: velarContractAddress,
contractName,
functionName,
functionArgs: [
import_transactions.Cl.contractPrincipal(tokenInAddress, tokenInName),
import_transactions.Cl.contractPrincipal(tokenOutAddress, tokenOutName)
],
network,
senderAddress: sender
};
const poolId = await (0, import_transactions.callReadOnlyFunction)(options);
if (poolId.type == 8 || poolId.type == 9) throw "Pool does not exist";
options.functionName = "lookup-pool";
const poolData = await (0, import_transactions.callReadOnlyFunction)(options);
if (poolData.type == 8) throw "Quoter contract error";
return {
poolId: poolId.value.value,
flipped: poolData.value.data.flipped.type == 4 ? false : true,
lpToken: (0, import_transactions.cvToJSON)(poolData.value.data.pool.data["lp-token"]).value,
token0: (0, import_transactions.cvToJSON)(poolData.value.data.pool.data.token0).value,
token1: (0, import_transactions.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: {
num: poolData.value.data.pool.data["swap-fee"].data.num.value,
den: poolData.value.data.pool.data["swap-fee"].data.den.value
},
shareFee: {
num: poolData.value.data.pool.data["share-fee"].data.num.value,
den: poolData.value.data.pool.data["share-fee"].data.den.value
},
protocolFee: {
num: poolData.value.data.pool.data["protocol-fee"].data.num.value,
den: poolData.value.data.pool.data["protocol-fee"].data.den.value
}
};
}
async function velarQuoter(velarContractAddress, amtIn, tokenInName, tokenInAddress, tokenOutName, tokenOutAddress, network, sender = "ST2F4BK4GZH6YFBNHYDDGN4T1RKBA7DA1BJZPJEJJ") {
const result = await getVelarPoolData(
velarContractAddress,
tokenInName,
tokenInAddress,
tokenOutName,
tokenOutAddress,
"mainnet"
);
const contractName = "univ2-library";
const functionName = "get-amount-out";
const options = {
contractAddress: velarContractAddress,
contractName,
functionName,
functionArgs: [
import_transactions.Cl.uint(amtIn),
import_transactions.Cl.uint(result.reserve0),
import_transactions.Cl.uint(result.reserve1),
import_transactions.Cl.tuple({
num: import_transactions.Cl.uint(result.swapFee.num),
den: import_transactions.Cl.uint(result.swapFee.den)
})
],
network,
senderAddress: sender
};
const amtOut = await (0, import_transactions.callReadOnlyFunction)(options);
if (amtOut.type == 8) throw "Quoter contract error";
return amtOut.value.value;
}
// src/alex.ts
var import_transactions2 = require("@stacks/transactions");
async function getAlexPoolData(alexContractAddress, tokenInName, tokenInAddress, tokenOutName, tokenOutAddress, factor, network, sender = "ST2F4BK4GZH6YFBNHYDDGN4T1RKBA7DA1BJZPJEJJ") {
const contractName = "amm-registry-v2-01";
const functionName = "get-pool-details";
const options = {
contractAddress: alexContractAddress,
contractName,
functionName,
functionArgs: [
import_transactions2.Cl.contractPrincipal(tokenInAddress, tokenInName),
import_transactions2.Cl.contractPrincipal(tokenOutAddress, tokenOutName),
import_transactions2.Cl.uint(factor)
],
network,
senderAddress: sender
};
const poolData = await (0, import_transactions2.callReadOnlyFunction)(options);
if (poolData.type == 8) throw "Pool does not exist";
return {
poolId: poolData.value.data["pool-id"].value,
balanceX: poolData.value.data["balance-x"].value,
balanceY: poolData.value.data["balance-y"].value
};
}
async function alexQuoter(alexContractAddress, amtIn, tokenInName, tokenInAddress, tokenOutName, tokenOutAddress, network, sender = "ST2F4BK4GZH6YFBNHYDDGN4T1RKBA7DA1BJZPJEJJ") {
const alexFactor0 = 1e8;
const alexFactor1 = 5e6;
const contractName = "amm-pool-v2-01";
const functionName = "get-helper";
const options = {
contractAddress: alexContractAddress,
contractName,
functionName,
functionArgs: [
import_transactions2.Cl.contractPrincipal(tokenInAddress, tokenInName),
import_transactions2.Cl.contractPrincipal(tokenOutAddress, tokenOutName),
import_transactions2.Cl.uint(alexFactor0),
import_transactions2.Cl.uint(amtIn)
// dx
],
network,
senderAddress: sender
};
let amtOut = await (0, import_transactions2.callReadOnlyFunction)(options);
if (amtOut.type == 8) {
options.functionArgs[2] = import_transactions2.Cl.uint(alexFactor1);
amtOut = await (0, import_transactions2.callReadOnlyFunction)(options);
if (amtOut.type == 8) throw "Quoter contract error";
}
return { amtOut: amtOut.value.value, factor: options.functionArgs[2] };
}
// src/aggregator.ts
async function getBestPath(velar, alex, amtIn, tokenInName, tokenInAddress, tokenOutName, tokenOutAddress, network) {
let velarAmtOut;
try {
velarAmtOut = await velarQuoter(
velar,
amtIn,
tokenInName,
tokenInAddress,
tokenOutName,
tokenOutAddress,
network
);
} catch {
velarAmtOut = 0n;
}
let alexAmtOut;
let alexPoolData = {};
try {
alexPoolData = await alexQuoter(
alex,
amtIn,
tokenInName,
tokenInAddress,
tokenOutName,
tokenOutAddress,
network
);
alexAmtOut = alexPoolData.amtOut;
} catch {
alexAmtOut = 0n;
}
const dex = alexAmtOut > velarAmtOut ? "alex" : "velar";
let data = {};
let poolData = {};
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,
amtOut: alexAmtOut > velarAmtOut ? alexAmtOut : velarAmtOut,
data
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
alexQuoter,
getAlexPoolData,
getBestPath,
getVelarPoolData,
velarQuoter
});