Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@saberhq/stableswap-sdk

Package Overview
Dependencies
Maintainers
1
Versions
190
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saberhq/stableswap-sdk - npm Package Compare versions

Comparing version 1.0.0-alpha.25 to 1.0.0-alpha.26

58

lib/calculator/amounts.d.ts

@@ -1,6 +0,5 @@

import { u64 } from "@solana/spl-token";
import { Token, TokenAmount } from "@saberhq/spl-token";
import { Fraction } from "@ubeswap/token-math";
import JSBI from "jsbi";
import { IExchangeInfo } from "../entities/exchange";
import { IToken } from "../entities/token";
import { ITokenAmount } from "../entities/tokenAmount";
import { Fees } from "../fees";

@@ -10,6 +9,5 @@ /**

* @param exchange
* @param token The token to calculate the price of, relative to the other token.
* @returns
*/
export declare const calculateVirtualPrice: (exchange: IExchangeInfo, token?: IToken | undefined) => Fraction | null;
export declare const calculateVirtualPrice: (exchange: IExchangeInfo) => Fraction | null;
/**

@@ -21,18 +19,18 @@ * Calculates the estimated output amount of a swap.

*/
export declare const calculateEstimatedSwapOutputAmount: (exchange: IExchangeInfo, fromAmount: ITokenAmount) => {
outputAmountBeforeFees: ITokenAmount;
outputAmount: ITokenAmount;
fee: ITokenAmount;
lpFee: ITokenAmount;
adminFee: ITokenAmount;
export declare const calculateEstimatedSwapOutputAmount: (exchange: IExchangeInfo, fromAmount: TokenAmount) => {
outputAmountBeforeFees: TokenAmount;
outputAmount: TokenAmount;
fee: TokenAmount;
lpFee: TokenAmount;
adminFee: TokenAmount;
};
export interface IWithdrawOneResult {
withdrawAmount: ITokenAmount;
withdrawAmountBeforeFees: ITokenAmount;
swapFee: ITokenAmount;
withdrawFee: ITokenAmount;
lpSwapFee: ITokenAmount;
lpWithdrawFee: ITokenAmount;
adminSwapFee: ITokenAmount;
adminWithdrawFee: ITokenAmount;
withdrawAmount: TokenAmount;
withdrawAmountBeforeFees: TokenAmount;
swapFee: TokenAmount;
withdrawFee: TokenAmount;
lpSwapFee: TokenAmount;
lpWithdrawFee: TokenAmount;
adminSwapFee: TokenAmount;
adminWithdrawFee: TokenAmount;
}

@@ -45,4 +43,4 @@ /**

exchange: IExchangeInfo;
poolTokenAmount: ITokenAmount;
withdrawToken: IToken;
poolTokenAmount: TokenAmount;
withdrawToken: Token;
}) => IWithdrawOneResult;

@@ -52,3 +50,3 @@ /**

*/
export declare const normalizedTradeFee: ({ trade }: Fees, n_coins: u64, amount: u64) => Fraction;
export declare const normalizedTradeFee: ({ trade }: Fees, n_coins: JSBI, amount: JSBI) => Fraction;
export declare const calculateEstimatedWithdrawAmount: ({ poolTokenAmount, reserves, fees, lpTotalSupply, }: {

@@ -58,7 +56,7 @@ /**

*/
poolTokenAmount: ITokenAmount;
poolTokenAmount: TokenAmount;
} & Pick<IExchangeInfo, "fees" | "reserves" | "lpTotalSupply">) => {
withdrawAmounts: readonly [ITokenAmount, ITokenAmount];
withdrawAmountsBeforeFees: readonly [ITokenAmount, ITokenAmount];
fees: readonly [ITokenAmount, ITokenAmount];
withdrawAmounts: readonly [TokenAmount, TokenAmount];
withdrawAmountsBeforeFees: readonly [TokenAmount, TokenAmount];
fees: readonly [TokenAmount, TokenAmount];
};

@@ -72,7 +70,7 @@ /**

*/
export declare const calculateEstimatedMintAmount: (exchange: IExchangeInfo, depositAmountA: u64, depositAmountB: u64) => {
mintAmountBeforeFees: ITokenAmount;
mintAmount: ITokenAmount;
fees: ITokenAmount;
export declare const calculateEstimatedMintAmount: (exchange: IExchangeInfo, depositAmountA: JSBI, depositAmountB: JSBI) => {
mintAmountBeforeFees: TokenAmount;
mintAmount: TokenAmount;
fees: TokenAmount;
};
//# sourceMappingURL=amounts.d.ts.map

@@ -7,8 +7,6 @@ "use strict";

exports.calculateEstimatedMintAmount = exports.calculateEstimatedWithdrawAmount = exports.normalizedTradeFee = exports.calculateEstimatedWithdrawOneAmount = exports.calculateEstimatedSwapOutputAmount = exports.calculateVirtualPrice = void 0;
const spl_token_1 = require("@solana/spl-token");
const spl_token_1 = require("@saberhq/spl-token");
const token_math_1 = require("@ubeswap/token-math");
const bn_js_1 = __importDefault(require("bn.js"));
const jsbi_1 = __importDefault(require("jsbi"));
const lodash_1 = require("lodash");
const bigint_1 = require("../entities/bigint");
const token_1 = require("../entities/token");
const curve_1 = require("./curve");

@@ -18,26 +16,11 @@ /**

* @param exchange
* @param token The token to calculate the price of, relative to the other token.
* @returns
*/
const calculateVirtualPrice = (exchange, token) => {
var _a;
const amount = (_a = exchange.lpTotalSupply) === null || _a === void 0 ? void 0 : _a.amount;
if (amount === undefined || amount.isZero()) {
const calculateVirtualPrice = (exchange) => {
const amount = exchange.lpTotalSupply;
if (amount === undefined || amount.equalTo(0)) {
// pool has no tokens
return null;
}
const price = bigint_1.divide(curve_1.computeD(exchange.ampFactor, exchange.reserves[0].amount.amount, exchange.reserves[1].amount.amount), amount);
if (token) {
const tokenIsLarger = (token_1.tokenEquals(token, exchange.reserves[1].amount.token) &&
exchange.reserves[1].amount.amount.gt(exchange.reserves[0].amount.amount)) ||
(token_1.tokenEquals(token, exchange.reserves[0].amount.token) &&
exchange.reserves[0].amount.amount.gt(exchange.reserves[1].amount.amount));
if (
// if token is larger, price should be less than 1
(tokenIsLarger && price.greaterThan(1)) ||
// if token is small, price should be greater than than 1
(!tokenIsLarger && price.lessThan(1))) {
return price.invert();
}
}
const price = new token_math_1.Fraction(curve_1.computeD(exchange.ampFactor, exchange.reserves[0].amount.raw, exchange.reserves[1].amount.raw), amount.raw);
return price;

@@ -56,7 +39,4 @@ };

: [exchange.reserves[1], exchange.reserves[0]];
if (fromAmount.amount.isZero()) {
const zero = {
token: toReserves.amount.token,
amount: new spl_token_1.u64(0),
};
if (fromAmount.equalTo(0)) {
const zero = new spl_token_1.TokenAmount(toReserves.amount.token, spl_token_1.ZERO);
return {

@@ -71,20 +51,18 @@ outputAmountBeforeFees: zero,

const amp = exchange.ampFactor;
const amountBeforeFees = toReserves.amount.amount.sub(curve_1.computeY(amp, fromReserves.amount.amount.add(fromAmount.amount), curve_1.computeD(amp, fromReserves.amount.amount, toReserves.amount.amount)));
const lpFee = exchange.fees.trade.multiply(amountBeforeFees.toString()).asFraction;
const adminFee = lpFee.multiply(exchange.fees.adminTrade);
const outputAmountBeforeFees = {
token: toReserves.amount.token,
amount: new spl_token_1.u64(amountBeforeFees),
};
const outputAmount = Object.assign(Object.assign({}, outputAmountBeforeFees), { amount: bigint_1.fromFraction(new token_math_1.Fraction(amountBeforeFees.toString(), 1).subtract(lpFee)) });
const amountBeforeFees = jsbi_1.default.subtract(toReserves.amount.raw, curve_1.computeY(amp, jsbi_1.default.add(fromReserves.amount.raw, fromAmount.raw), curve_1.computeD(amp, fromReserves.amount.raw, toReserves.amount.raw)));
const outputAmountBeforeFees = new spl_token_1.TokenAmount(toReserves.amount.token, amountBeforeFees);
const fee = new spl_token_1.TokenAmount(toReserves.amount.token, exchange.fees.trade.asFraction.multiply(amountBeforeFees).toFixed(0));
const adminFee = new spl_token_1.TokenAmount(toReserves.amount.token, exchange.fees.adminTrade.asFraction.multiply(fee.raw).toFixed(0));
const lpFee = fee.subtract(adminFee);
const outputAmount = new spl_token_1.TokenAmount(toReserves.amount.token, jsbi_1.default.subtract(amountBeforeFees, fee.raw));
return {
outputAmountBeforeFees,
outputAmount,
fee: Object.assign(Object.assign({}, outputAmountBeforeFees), { amount: bigint_1.fromFraction(lpFee) }),
lpFee: Object.assign(Object.assign({}, outputAmountBeforeFees), { amount: bigint_1.fromFraction(lpFee.subtract(adminFee)) }),
adminFee: Object.assign(Object.assign({}, outputAmountBeforeFees), { amount: bigint_1.fromFraction(adminFee) }),
fee: fee,
lpFee,
adminFee,
};
};
exports.calculateEstimatedSwapOutputAmount = calculateEstimatedSwapOutputAmount;
const N_COINS = new spl_token_1.u64(2);
const N_COINS = jsbi_1.default.BigInt(2);
/**

@@ -96,31 +74,28 @@ * Calculates the amount of tokens withdrawn if only withdrawing one token.

var _a, _b, _c, _d;
if (poolTokenAmount.amount.isZero()) {
if (poolTokenAmount.equalTo(0)) {
// final quantities
const quantities = {
withdrawAmount: bigint_1.ZERO,
withdrawAmountBeforeFees: bigint_1.ZERO,
swapFee: bigint_1.ZERO,
withdrawFee: bigint_1.ZERO,
lpSwapFee: bigint_1.ZERO,
lpWithdrawFee: bigint_1.ZERO,
adminSwapFee: bigint_1.ZERO,
adminWithdrawFee: bigint_1.ZERO,
withdrawAmount: spl_token_1.ZERO,
withdrawAmountBeforeFees: spl_token_1.ZERO,
swapFee: spl_token_1.ZERO,
withdrawFee: spl_token_1.ZERO,
lpSwapFee: spl_token_1.ZERO,
lpWithdrawFee: spl_token_1.ZERO,
adminSwapFee: spl_token_1.ZERO,
adminWithdrawFee: spl_token_1.ZERO,
};
return lodash_1.mapValues(quantities, (q) => ({
token: withdrawToken,
amount: q,
}));
return lodash_1.mapValues(quantities, (q) => new spl_token_1.TokenAmount(withdrawToken, q));
}
const { ampFactor, fees } = exchange;
const [baseReserves, quoteReserves] = [
(_b = (_a = exchange.reserves.find((r) => token_1.tokenEquals(r.amount.token, withdrawToken))) === null || _a === void 0 ? void 0 : _a.amount.amount) !== null && _b !== void 0 ? _b : new bn_js_1.default(0),
(_d = (_c = exchange.reserves.find((r) => !token_1.tokenEquals(r.amount.token, withdrawToken))) === null || _c === void 0 ? void 0 : _c.amount.amount) !== null && _d !== void 0 ? _d : new bn_js_1.default(0),
(_b = (_a = exchange.reserves.find((r) => spl_token_1.tokensEqual(r.amount.token, withdrawToken))) === null || _a === void 0 ? void 0 : _a.amount.raw) !== null && _b !== void 0 ? _b : spl_token_1.ZERO,
(_d = (_c = exchange.reserves.find((r) => !spl_token_1.tokensEqual(r.amount.token, withdrawToken))) === null || _c === void 0 ? void 0 : _c.amount.raw) !== null && _d !== void 0 ? _d : spl_token_1.ZERO,
];
const d_0 = curve_1.computeD(ampFactor, baseReserves, quoteReserves);
const d_1 = d_0.sub(poolTokenAmount.amount.mul(d_0).div(exchange.lpTotalSupply.amount));
const d_1 = jsbi_1.default.subtract(d_0, jsbi_1.default.divide(jsbi_1.default.multiply(poolTokenAmount.raw, d_0), exchange.lpTotalSupply.raw));
const new_y = curve_1.computeY(ampFactor, quoteReserves, d_1);
// expected_base_amount = swap_base_amount * d_1 / d_0 - new_y;
const expected_base_amount = baseReserves.mul(d_1).div(d_0).sub(new_y);
const expected_base_amount = jsbi_1.default.subtract(jsbi_1.default.divide(jsbi_1.default.multiply(baseReserves, d_1), d_0), new_y);
// expected_quote_amount = swap_quote_amount - swap_quote_amount * d_1 / d_0;
const expected_quote_amount = quoteReserves.sub(quoteReserves.mul(d_1).div(d_0));
const expected_quote_amount = jsbi_1.default.subtract(quoteReserves, jsbi_1.default.divide(jsbi_1.default.multiply(quoteReserves, d_1), d_0));
// new_base_amount = swap_base_amount - expected_base_amount * fee / fee_denominator;

@@ -130,4 +105,4 @@ const new_base_amount = new token_math_1.Fraction(baseReserves.toString(), 1).subtract(exports.normalizedTradeFee(fees, N_COINS, expected_base_amount));

const new_quote_amount = new token_math_1.Fraction(quoteReserves.toString(), 1).subtract(exports.normalizedTradeFee(fees, N_COINS, expected_quote_amount));
const dy = new_base_amount.subtract(curve_1.computeY(ampFactor, new bn_js_1.default(new_quote_amount.toFixed(0)), d_1).toString());
const dy_0 = baseReserves.sub(new_y);
const dy = new_base_amount.subtract(curve_1.computeY(ampFactor, jsbi_1.default.BigInt(new_quote_amount.toFixed(0)), d_1).toString());
const dy_0 = jsbi_1.default.subtract(baseReserves, new_y);
// lp fees

@@ -155,6 +130,3 @@ const swapFee = new token_math_1.Fraction(dy_0.toString(), 1).subtract(dy);

};
return lodash_1.mapValues(quantities, (q) => ({
token: withdrawToken,
amount: bigint_1.fromFraction(q),
}));
return lodash_1.mapValues(quantities, (q) => new spl_token_1.TokenAmount(withdrawToken, q.toFixed(0)));
};

@@ -166,14 +138,9 @@ exports.calculateEstimatedWithdrawOneAmount = calculateEstimatedWithdrawOneAmount;

const normalizedTradeFee = ({ trade }, n_coins, amount) => {
const adjustedTradeFee = new token_math_1.Fraction(n_coins.toString(), n_coins.sub(new bn_js_1.default(1)).mul(new bn_js_1.default(4)).toString());
return new token_math_1.Fraction(amount.toString(), 1)
.multiply(trade)
.multiply(adjustedTradeFee);
const adjustedTradeFee = new token_math_1.Fraction(n_coins, jsbi_1.default.multiply(jsbi_1.default.subtract(n_coins, spl_token_1.ONE), jsbi_1.default.BigInt(4)));
return new token_math_1.Fraction(amount, 1).multiply(trade).multiply(adjustedTradeFee);
};
exports.normalizedTradeFee = normalizedTradeFee;
const calculateEstimatedWithdrawAmount = ({ poolTokenAmount, reserves, fees, lpTotalSupply, }) => {
if (lpTotalSupply.amount.isZero()) {
const zero = reserves.map((r) => ({
token: r.amount.token,
amount: new spl_token_1.u64(0),
}));
if (lpTotalSupply.equalTo(0)) {
const zero = reserves.map((r) => new spl_token_1.TokenAmount(r.amount.token, spl_token_1.ZERO));
return {

@@ -185,23 +152,18 @@ withdrawAmounts: zero,

}
const share = new token_math_1.Fraction(poolTokenAmount.amount.toString(), lpTotalSupply.amount.toString());
const withdrawAmounts = reserves.map(({ amount: { token, amount } }) => {
const baseAmount = share.multiply(amount.toString());
const share = poolTokenAmount.divide(lpTotalSupply);
const withdrawAmounts = reserves.map(({ amount }) => {
const baseAmount = share.multiply(amount.raw);
const fee = baseAmount.multiply(fees.withdraw);
return {
token,
beforeFees: bigint_1.fromFraction(baseAmount),
fee: bigint_1.fromFraction(fee),
amount: bigint_1.fromFraction(baseAmount.subtract(fee)),
};
return [
new spl_token_1.TokenAmount(amount.token, jsbi_1.default.BigInt(baseAmount.subtract(fee).toFixed(0))),
{
beforeFees: jsbi_1.default.BigInt(baseAmount.toFixed(0)),
fee: jsbi_1.default.BigInt(fee.toFixed(0)),
},
];
});
return {
withdrawAmountsBeforeFees: withdrawAmounts.map((amt) => ({
token: amt.token,
amount: amt.beforeFees,
})),
withdrawAmounts,
fees: withdrawAmounts.map((amt) => ({
token: amt.token,
amount: amt.fee,
})),
withdrawAmountsBeforeFees: withdrawAmounts.map(([amt, { beforeFees }]) => new spl_token_1.TokenAmount(amt.token, beforeFees)),
withdrawAmounts: [withdrawAmounts[0][0], withdrawAmounts[1][0]],
fees: withdrawAmounts.map(([amt, { fee }]) => new spl_token_1.TokenAmount(amt.token, fee)),
};

@@ -218,7 +180,4 @@ };

const calculateEstimatedMintAmount = (exchange, depositAmountA, depositAmountB) => {
if (depositAmountA.isZero() && depositAmountB.isZero()) {
const zero = {
token: exchange.lpTotalSupply.token,
amount: new spl_token_1.u64(0),
};
if (jsbi_1.default.equal(depositAmountA, spl_token_1.ZERO) && jsbi_1.default.equal(depositAmountB, spl_token_1.ZERO)) {
const zero = new spl_token_1.TokenAmount(exchange.lpTotalSupply.token, spl_token_1.ZERO);
return {

@@ -232,34 +191,32 @@ mintAmountBeforeFees: zero,

const [reserveA, reserveB] = exchange.reserves;
const d0 = curve_1.computeD(amp, reserveA.amount.amount, reserveB.amount.amount);
const d1 = curve_1.computeD(amp, reserveA.amount.amount.add(depositAmountA), reserveB.amount.amount.add(depositAmountB));
if (d1.lt(d0)) {
const d0 = curve_1.computeD(amp, reserveA.amount.raw, reserveB.amount.raw);
const d1 = curve_1.computeD(amp, jsbi_1.default.add(reserveA.amount.raw, depositAmountA), jsbi_1.default.add(reserveB.amount.raw, depositAmountB));
if (jsbi_1.default.lessThan(d1, d0)) {
throw new Error("New D cannot be less than previous D");
}
const oldBalances = exchange.reserves.map((r) => r.amount.amount);
const oldBalances = exchange.reserves.map((r) => r.amount.raw);
const newBalances = [
reserveA.amount.amount.add(depositAmountA),
reserveB.amount.amount.add(depositAmountB),
jsbi_1.default.add(reserveA.amount.raw, depositAmountA),
jsbi_1.default.add(reserveB.amount.raw, depositAmountB),
];
const adjustedBalances = newBalances.map((newBalance, i) => {
const oldBalance = oldBalances[i];
const idealBalance = new token_math_1.Fraction(d1.toString(), d0.toString()).multiply(oldBalance.toString());
const difference = idealBalance.subtract(newBalance.toString());
const idealBalance = new token_math_1.Fraction(d1, d0).multiply(oldBalance);
const difference = idealBalance.subtract(newBalance);
const diffAbs = difference.greaterThan(0)
? difference
: difference.multiply(-1);
const fee = exports.normalizedTradeFee(exchange.fees, N_COINS, bigint_1.fromFraction(diffAbs));
return newBalance.sub(bigint_1.fromFraction(fee));
const fee = exports.normalizedTradeFee(exchange.fees, N_COINS, jsbi_1.default.BigInt(diffAbs.toFixed(0)));
return jsbi_1.default.subtract(newBalance, jsbi_1.default.BigInt(fee.toFixed(0)));
});
const d2 = curve_1.computeD(amp, adjustedBalances[0], adjustedBalances[1]);
const lpSupply = exchange.lpTotalSupply.amount;
const mintAmountRaw = bigint_1.fromFraction(new token_math_1.Fraction(lpSupply.mul(d2.sub(d0)).toString(), d0.toString()));
const mintAmount = {
token: exchange.lpTotalSupply.token,
amount: mintAmountRaw,
};
const mintAmountRawBeforeFees = bigint_1.fromFraction(new token_math_1.Fraction(lpSupply.mul(d1.sub(d0)).toString(), d0.toString()));
const fees = Object.assign(Object.assign({}, mintAmount), { amount: new spl_token_1.u64(mintAmountRawBeforeFees.sub(mintAmountRaw)) });
const lpSupply = exchange.lpTotalSupply;
const mintAmountRaw = jsbi_1.default.divide(jsbi_1.default.multiply(lpSupply.raw, jsbi_1.default.subtract(d2, d0)), d0);
const mintAmount = new spl_token_1.TokenAmount(exchange.lpTotalSupply.token, mintAmountRaw);
const mintAmountRawBeforeFees = jsbi_1.default.divide(jsbi_1.default.multiply(lpSupply.raw, jsbi_1.default.subtract(d1, d0)), d0);
const fees = new spl_token_1.TokenAmount(exchange.lpTotalSupply.token, jsbi_1.default.subtract(mintAmountRawBeforeFees, mintAmountRaw));
const mintAmountBeforeFees = new spl_token_1.TokenAmount(exchange.lpTotalSupply.token, mintAmountRawBeforeFees);
return {
mintAmount,
mintAmountBeforeFees: Object.assign(Object.assign({}, mintAmount), { amount: mintAmountRawBeforeFees }),
mintAmountBeforeFees,
fees,

@@ -266,0 +223,0 @@ };

@@ -1,2 +0,2 @@

import BN from "bn.js";
import JSBI from "jsbi";
/**

@@ -9,3 +9,3 @@ * Compute the StableSwap invariant

*/
export declare const computeD: (ampFactor: BN, amountA: BN, amountB: BN) => BN;
export declare const computeD: (ampFactor: JSBI, amountA: JSBI, amountB: JSBI) => JSBI;
/**

@@ -18,3 +18,3 @@ * Compute Y amount in respect to X on the StableSwap curve

*/
export declare const computeY: (ampFactor: BN, x: BN, d: BN) => BN;
export declare const computeY: (ampFactor: JSBI, x: JSBI, d: JSBI) => JSBI;
//# sourceMappingURL=curve.d.ts.map

@@ -7,6 +7,11 @@ "use strict";

exports.computeY = exports.computeD = void 0;
const bn_js_1 = __importDefault(require("bn.js"));
const ZERO = new bn_js_1.default(0);
const ONE = new bn_js_1.default(1);
const N_COINS = new bn_js_1.default(2); // n
const spl_token_1 = require("@saberhq/spl-token");
const jsbi_1 = __importDefault(require("jsbi"));
const N_COINS = jsbi_1.default.BigInt(2); // n
const abs = (a) => {
if (jsbi_1.default.greaterThan(a, spl_token_1.ZERO)) {
return a;
}
return jsbi_1.default.unaryMinus(a);
};
/**

@@ -20,19 +25,17 @@ * Compute the StableSwap invariant

const computeD = (ampFactor, amountA, amountB) => {
const Ann = ampFactor.mul(N_COINS); // A*n^n
const S = amountA.add(amountB); // sum(x_i), a.k.a S
if (S.isZero()) {
return S;
const Ann = jsbi_1.default.multiply(ampFactor, N_COINS); // A*n^n
const S = jsbi_1.default.add(amountA, amountB); // sum(x_i), a.k.a S
if (jsbi_1.default.equal(S, spl_token_1.ZERO)) {
return spl_token_1.ZERO;
}
let dPrev = ZERO;
let dPrev = spl_token_1.ZERO;
let d = S;
while (d.sub(dPrev).abs().gt(ONE)) {
while (jsbi_1.default.greaterThan(abs(jsbi_1.default.subtract(d, dPrev)), spl_token_1.ONE)) {
dPrev = d;
let dP = d;
dP = dP.mul(d).div(new bn_js_1.default(amountA.mul(N_COINS)));
dP = dP.mul(d).div(new bn_js_1.default(amountB.mul(N_COINS)));
const dNumerator = d.mul(new bn_js_1.default(Ann.mul(S).add(new bn_js_1.default(dP.mul(N_COINS)))));
const dDenominator = d
.mul(new bn_js_1.default(Ann.sub(ONE)))
.add(dP.mul(new bn_js_1.default(N_COINS.add(ONE))));
d = dNumerator.div(dDenominator);
dP = jsbi_1.default.divide(jsbi_1.default.multiply(dP, d), jsbi_1.default.multiply(amountA, N_COINS));
dP = jsbi_1.default.divide(jsbi_1.default.multiply(dP, d), jsbi_1.default.multiply(amountB, N_COINS));
const dNumerator = jsbi_1.default.multiply(d, jsbi_1.default.add(jsbi_1.default.multiply(Ann, S), jsbi_1.default.multiply(dP, N_COINS)));
const dDenominator = jsbi_1.default.add(jsbi_1.default.multiply(d, jsbi_1.default.subtract(Ann, spl_token_1.ONE)), jsbi_1.default.multiply(dP, jsbi_1.default.add(N_COINS, spl_token_1.ONE)));
d = jsbi_1.default.divide(dNumerator, dDenominator);
}

@@ -50,17 +53,12 @@ return d;

const computeY = (ampFactor, x, d) => {
const Ann = ampFactor.mul(N_COINS); // A*n^n
const Ann = jsbi_1.default.multiply(ampFactor, N_COINS); // A*n^n
// sum' = prod' = x
const b = x.add(d.div(Ann)).sub(d); // b = sum' - (A*n**n - 1) * D / (A * n**n)
const c = d // c = D ** (n + 1) / (n ** (2 * n) * prod' * A)
.mul(d)
.mul(d)
.div(new bn_js_1.default(N_COINS.mul(N_COINS).mul(x).mul(Ann)));
let yPrev = ZERO;
const b = jsbi_1.default.subtract(jsbi_1.default.add(x, jsbi_1.default.divide(d, Ann)), d); // b = sum' - (A*n**n - 1) * D / (A * n**n)
const c = jsbi_1.default.divide(jsbi_1.default.multiply(jsbi_1.default.multiply(d, // c = D ** (n + 1) / (n ** (2 * n) * prod' * A)
d), d), jsbi_1.default.multiply(N_COINS, jsbi_1.default.multiply(N_COINS, jsbi_1.default.multiply(x, Ann))));
let yPrev = spl_token_1.ZERO;
let y = d;
while (y.sub(yPrev).abs().gt(ONE)) {
while (jsbi_1.default.greaterThan(abs(jsbi_1.default.subtract(y, yPrev)), spl_token_1.ONE)) {
yPrev = y;
y = y
.mul(y)
.add(c)
.div(new bn_js_1.default(N_COINS.mul(y).add(b)));
y = jsbi_1.default.divide(jsbi_1.default.add(jsbi_1.default.multiply(y, y), c), jsbi_1.default.add(jsbi_1.default.multiply(N_COINS, y), b));
}

@@ -67,0 +65,0 @@ return y;

@@ -1,4 +0,4 @@

import { u64 } from "@solana/spl-token";
import { Percent } from "@ubeswap/token-math";
export declare const calculateTokenAmountAfterSlippage: (slippagePercentage: Percent, tokenAmount?: u64) => u64;
import JSBI from "jsbi";
export declare const calculateTokenAmountAfterSlippage: (slippagePercentage: Percent, tokenAmount?: JSBI) => JSBI;
//# sourceMappingURL=slippage.d.ts.map
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateTokenAmountAfterSlippage = void 0;
const spl_token_1 = require("@saberhq/spl-token");
const token_math_1 = require("@ubeswap/token-math");
const bigint_1 = require("../entities/bigint");
const jsbi_1 = __importDefault(require("jsbi"));
const format_1 = require("../util/format");
const MIN_SLIPPAGE_PERCENTAGE = new token_math_1.Percent(0, 1);
const MAX_SLIPPAGE_PERCENTAGE = new token_math_1.Percent(1, 1);
const calculateTokenAmountAfterSlippage = (slippagePercentage, tokenAmount = bigint_1.ZERO) => {
const calculateTokenAmountAfterSlippage = (slippagePercentage, tokenAmount = spl_token_1.ZERO) => {
if (slippagePercentage.lessThan(MIN_SLIPPAGE_PERCENTAGE) ||

@@ -14,8 +18,10 @@ slippagePercentage.greaterThan(MAX_SLIPPAGE_PERCENTAGE)) {

}
if (tokenAmount.isZero()) {
return bigint_1.ZERO;
if (jsbi_1.default.equal(tokenAmount, spl_token_1.ZERO)) {
return spl_token_1.ZERO;
}
return bigint_1.fromFraction(new token_math_1.Fraction(tokenAmount.toString(), 1).subtract(new token_math_1.Fraction(tokenAmount.toString(), 1).multiply(slippagePercentage)));
return jsbi_1.default.BigInt(new token_math_1.Fraction(tokenAmount, 1)
.subtract(new token_math_1.Fraction(tokenAmount, 1).multiply(slippagePercentage))
.toFixed(0));
};
exports.calculateTokenAmountAfterSlippage = calculateTokenAmountAfterSlippage;
//# sourceMappingURL=slippage.js.map

@@ -0,4 +1,4 @@

import { TokenAmount } from "@saberhq/spl-token";
import { u64 } from "@solana/spl-token";
import { Fraction } from "@ubeswap/token-math";
import { IToken } from "./token";
export declare const ZERO: u64;

@@ -16,6 +16,3 @@ export declare const divToNumber: (a: u64, b: u64) => number;

}
export declare const formatTokenAmountNumber: (amount: {
token: Pick<IToken, "decimals">;
amount: u64;
}, { numberFormatOptions, locale }?: IFormatUint) => string;
export declare const formatTokenAmountNumber: (amount: TokenAmount, { numberFormatOptions, locale }?: IFormatUint) => string;
/**

@@ -22,0 +19,0 @@ * Creates a u64 from a fraction

@@ -21,4 +21,3 @@ "use strict";

const formatTokenAmountNumber = (amount, { numberFormatOptions, locale } = {}) => {
const fraction = new token_math_1.Fraction(amount.amount.toString(), Math.pow(10, amount.token.decimals));
const asExactString = fraction.toFixed(amount.token.decimals);
const asExactString = amount.toFixed(amount.token.decimals);
const asNumber = parseFloat(asExactString);

@@ -25,0 +24,0 @@ return `${numberFormatOptions !== undefined

/// <reference types="node" />
import { u64 } from "@solana/spl-token";
import { Token, TokenAmount } from "@saberhq/spl-token";
import { PublicKey } from "@solana/web3.js";
import JSBI from "jsbi";
import { Fees } from "../fees";
import { StableSwap } from "../stable-swap";
import { IToken } from "./token";
import { ITokenAmount } from "./tokenAmount";
export interface IReserve {

@@ -20,3 +19,3 @@ /**

*/
amount: ITokenAmount;
amount: TokenAmount;
}

@@ -29,4 +28,4 @@ /**

swapAccount: PublicKey;
lpToken: IToken;
tokens: readonly [IToken, IToken];
lpToken: Token;
tokens: readonly [Token, Token];
}

@@ -37,5 +36,5 @@ /**

export interface IExchangeInfo {
ampFactor: u64;
ampFactor: JSBI;
fees: Fees;
lpTotalSupply: ITokenAmount;
lpTotalSupply: TokenAmount;
reserves: readonly [IReserve, IReserve];

@@ -42,0 +41,0 @@ }

@@ -13,3 +13,3 @@ "use strict";

exports.loadExchangeInfo = exports.makeExchangeInfo = void 0;
const spl_token_1 = require("@solana/spl-token");
const spl_token_1 = require("@saberhq/spl-token");
const layout_1 = require("../layout");

@@ -29,8 +29,5 @@ const account_1 = require("../util/account");

// TODO(igm): this should be calculated dynamically
ampFactor: swap.state.targetAmpFactor,
ampFactor: spl_token_1.parseBigintIsh(swap.state.targetAmpFactor.toString()),
fees: swap.state.fees,
lpTotalSupply: {
token: exchange.lpToken,
amount: poolMintSupply !== null && poolMintSupply !== void 0 ? poolMintSupply : new spl_token_1.u64(0),
},
lpTotalSupply: new spl_token_1.TokenAmount(exchange.lpToken, poolMintSupply !== null && poolMintSupply !== void 0 ? poolMintSupply : 0),
reserves: [

@@ -40,6 +37,3 @@ {

adminFeeAccount: swap.state.tokenA.adminFeeAccount,
amount: {
token: exchange.tokens[0],
amount: swapAmountA,
},
amount: new spl_token_1.TokenAmount(exchange.tokens[0], swapAmountA),
},

@@ -49,6 +43,3 @@ {

adminFeeAccount: swap.state.tokenB.adminFeeAccount,
amount: {
token: exchange.tokens[1],
amount: swapAmountB,
},
amount: new spl_token_1.TokenAmount(exchange.tokens[1], swapAmountB),
},

@@ -55,0 +46,0 @@ ],

export * from "./bigint";
export * from "./exchange";
export * from "./token";
export * from "./tokenAmount";
//# sourceMappingURL=index.d.ts.map

@@ -15,4 +15,2 @@ "use strict";

__exportStar(require("./exchange"), exports);
__exportStar(require("./token"), exports);
__exportStar(require("./tokenAmount"), exports);
//# sourceMappingURL=index.js.map
{
"name": "@saberhq/stableswap-sdk",
"version": "1.0.0-alpha.25",
"version": "1.0.0-alpha.26",
"main": "lib/index.js",

@@ -51,2 +51,3 @@ "types": "lib/index.d.ts",

"dependencies": {
"@saberhq/spl-token": "^0.1.0-beta.4",
"@types/bn.js": "^5.1.0",

@@ -57,2 +58,3 @@ "@types/yargs": "^17.0.0",

"buffer-layout": "^1.2.1",
"jsbi": "^3.1.4",
"yargs": "^17.0.1"

@@ -59,0 +61,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc