@catalabs/catalyst-sdk
Version:
Catalyst AMM SDK
77 lines • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendAssetVol = sendAssetVol;
exports.getAssetVol = getAssetVol;
exports.receiveAssetVol = receiveAssetVol;
exports.withdrawEqualVol = withdrawEqualVol;
exports.solvWithdrawEqualVol = solvWithdrawEqualVol;
exports.withdrawMixedVol = withdrawMixedVol;
const math_lib_1 = require("../math.lib");
const math_volatile_integrals_1 = require("./math.volatile.integrals");
function sendAssetVol(fromBalance, input, fromWeight) {
return fromWeight * (0, math_lib_1.lnWad)(((fromBalance + input) * math_lib_1.WAD) / fromBalance);
}
function getAssetVol(fromBalance, input, fromWeight) {
return fromWeight * (0, math_lib_1.lnWad)((fromBalance * math_lib_1.WAD) / (fromBalance - input));
}
function receiveAssetVol(toBalance, U, toWeight) {
return (toBalance * (math_lib_1.WAD - (0, math_lib_1.expWad)((U / toWeight) * -1n))) / math_lib_1.WAD;
}
function withdrawEqualVol(vaultTokens, totalSupply, escrowedVaultTokens, tokenWeights, poolBalance, escrowedTokens) {
if (poolBalance.length !== tokenWeights.length) {
throw new Error('Different length between poolBalance and tokenWeights');
}
if (poolBalance.length !== escrowedTokens.length) {
throw new Error('Different length between poolBalance and escrowedTokens');
}
const ts = totalSupply + escrowedVaultTokens;
const amounts = [];
for (let i = 0; i < poolBalance.length; i++) {
const workingBalance = poolBalance[i] - escrowedTokens[i];
const tkAmount = (workingBalance * vaultTokens) / ts;
amounts.push(tkAmount);
}
return amounts;
}
function solvWithdrawEqualVol(referenceTokenWithdraw, referenceTokenIndex, totalSupply, escrowedVaultTokens, tokenWeights, poolBalance, escrowedTokens) {
if (poolBalance.length !== tokenWeights.length) {
throw new Error('Different length between poolBalance and tokenWeights');
}
if (poolBalance.length !== escrowedTokens.length) {
throw new Error('Different length between poolBalance and escrowedTokens');
}
const ts = totalSupply + escrowedVaultTokens;
const workingBalance = poolBalance[referenceTokenIndex] - escrowedTokens[referenceTokenIndex];
return (referenceTokenWithdraw * ts) / workingBalance;
}
function withdrawMixedVol(vaultTokens, withdrawRatio, totalSupply, escrowedVaultTokens, tokenWeights, poolBalance, escrowedTokens) {
if (poolBalance.length !== tokenWeights.length) {
throw new Error('Different length between poolBalance and tokenWeights');
}
if (poolBalance.length !== escrowedTokens.length) {
throw new Error('Different length between poolBalance and escrowedTokens');
}
if (poolBalance.length !== withdrawRatio.length) {
throw new Error('Different length between poolBalance and withdrawRatio');
}
let wsum = 0n;
for (const weight of tokenWeights) {
wsum = wsum + weight;
}
const ts = totalSupply + escrowedVaultTokens;
let U = (0, math_volatile_integrals_1.calcVaultTokensToUnitsVol)(vaultTokens, ts, wsum);
const amounts = [];
for (let i = 0; i < poolBalance.length; i++) {
const U_i = (U * withdrawRatio[i]) / math_lib_1.WAD;
if (U_i === 0n) {
amounts.push(0n);
continue;
}
U = U - U_i;
const workingBalance = poolBalance[i] - escrowedTokens[i];
const tokenAmount = (0, math_volatile_integrals_1.calcPriceCurveLimitVol)(U_i, workingBalance, tokenWeights[i]);
amounts.push(tokenAmount);
}
return amounts;
}
//# sourceMappingURL=math.volatile.functions.js.map