@saberhq/stableswap-sdk
Advanced tools
Comparing version 1.12.66 to 1.12.67
@@ -7,2 +7,3 @@ /// <reference types="node" /> | ||
import { StableSwap } from "../stable-swap"; | ||
import type { StableSwapState } from "../state"; | ||
import type { Fees } from "../state/fees"; | ||
@@ -45,2 +46,9 @@ /** | ||
/** | ||
* Calculates the amp factor of a swap. | ||
* @param state | ||
* @param now | ||
* @returns | ||
*/ | ||
export declare const calculateAmpFactor: (state: Pick<StableSwapState, "initialAmpFactor" | "targetAmpFactor" | "startRampTimestamp" | "stopRampTimestamp">, now?: number) => JSBI; | ||
/** | ||
* Creates an IExchangeInfo from parameters. | ||
@@ -47,0 +55,0 @@ * @returns |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.loadExchangeInfoFromSwapAccount = exports.makeExchange = exports.loadExchangeInfo = exports.makeExchangeInfo = void 0; | ||
exports.loadExchangeInfoFromSwapAccount = exports.makeExchange = exports.loadExchangeInfo = exports.makeExchangeInfo = exports.calculateAmpFactor = void 0; | ||
const tslib_1 = require("tslib"); | ||
const token_utils_1 = require("@saberhq/token-utils"); | ||
const bn_js_1 = tslib_1.__importDefault(require("bn.js")); | ||
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant")); | ||
const constants_1 = require("../constants"); | ||
@@ -9,2 +12,29 @@ const stable_swap_1 = require("../stable-swap"); | ||
/** | ||
* Calculates the amp factor of a swap. | ||
* @param state | ||
* @param now | ||
* @returns | ||
*/ | ||
const calculateAmpFactor = (state, now = Date.now() / 1000) => { | ||
const { initialAmpFactor, targetAmpFactor, startRampTimestamp, stopRampTimestamp, } = state; | ||
// The most common case is that there is no ramp in progress. | ||
if (now >= stopRampTimestamp) { | ||
return (0, token_utils_1.parseBigintIsh)(targetAmpFactor); | ||
} | ||
// If the ramp is about to start, use the initial amp. | ||
if (now <= startRampTimestamp) { | ||
return (0, token_utils_1.parseBigintIsh)(initialAmpFactor); | ||
} | ||
(0, tiny_invariant_1.default)(stopRampTimestamp >= startRampTimestamp, "stop must be after start"); | ||
// Calculate how far we are along the ramp curve. | ||
const percent = now >= stopRampTimestamp | ||
? 1 | ||
: now <= startRampTimestamp | ||
? 0 | ||
: (now - startRampTimestamp) / (stopRampTimestamp - startRampTimestamp); | ||
const diff = Math.floor(parseFloat(targetAmpFactor.sub(initialAmpFactor).toString()) * percent); | ||
return (0, token_utils_1.parseBigintIsh)(initialAmpFactor.add(new bn_js_1.default(diff))); | ||
}; | ||
exports.calculateAmpFactor = calculateAmpFactor; | ||
/** | ||
* Creates an IExchangeInfo from parameters. | ||
@@ -19,5 +49,5 @@ * @returns | ||
: undefined; | ||
const ampFactor = (0, exports.calculateAmpFactor)(swap.state); | ||
return { | ||
// TODO(igm): this should be calculated dynamically | ||
ampFactor: (0, token_utils_1.parseBigintIsh)(swap.state.targetAmpFactor.toString()), | ||
ampFactor, | ||
fees: swap.state.fees, | ||
@@ -24,0 +54,0 @@ lpTotalSupply: new token_utils_1.TokenAmount(exchange.lpToken, poolMintSupply !== null && poolMintSupply !== void 0 ? poolMintSupply : 0), |
@@ -7,2 +7,3 @@ /// <reference types="node" /> | ||
import { StableSwap } from "../stable-swap"; | ||
import type { StableSwapState } from "../state"; | ||
import type { Fees } from "../state/fees"; | ||
@@ -45,2 +46,9 @@ /** | ||
/** | ||
* Calculates the amp factor of a swap. | ||
* @param state | ||
* @param now | ||
* @returns | ||
*/ | ||
export declare const calculateAmpFactor: (state: Pick<StableSwapState, "initialAmpFactor" | "targetAmpFactor" | "startRampTimestamp" | "stopRampTimestamp">, now?: number) => JSBI; | ||
/** | ||
* Creates an IExchangeInfo from parameters. | ||
@@ -47,0 +55,0 @@ * @returns |
import { deserializeAccount, deserializeMint, parseBigintIsh, Token, TokenAmount, } from "@saberhq/token-utils"; | ||
import BN from "bn.js"; | ||
import invariant from "tiny-invariant"; | ||
import { SWAP_PROGRAM_ID } from "../constants"; | ||
@@ -6,2 +8,28 @@ import { StableSwap } from "../stable-swap"; | ||
/** | ||
* Calculates the amp factor of a swap. | ||
* @param state | ||
* @param now | ||
* @returns | ||
*/ | ||
export const calculateAmpFactor = (state, now = Date.now() / 1000) => { | ||
const { initialAmpFactor, targetAmpFactor, startRampTimestamp, stopRampTimestamp, } = state; | ||
// The most common case is that there is no ramp in progress. | ||
if (now >= stopRampTimestamp) { | ||
return parseBigintIsh(targetAmpFactor); | ||
} | ||
// If the ramp is about to start, use the initial amp. | ||
if (now <= startRampTimestamp) { | ||
return parseBigintIsh(initialAmpFactor); | ||
} | ||
invariant(stopRampTimestamp >= startRampTimestamp, "stop must be after start"); | ||
// Calculate how far we are along the ramp curve. | ||
const percent = now >= stopRampTimestamp | ||
? 1 | ||
: now <= startRampTimestamp | ||
? 0 | ||
: (now - startRampTimestamp) / (stopRampTimestamp - startRampTimestamp); | ||
const diff = Math.floor(parseFloat(targetAmpFactor.sub(initialAmpFactor).toString()) * percent); | ||
return parseBigintIsh(initialAmpFactor.add(new BN(diff))); | ||
}; | ||
/** | ||
* Creates an IExchangeInfo from parameters. | ||
@@ -16,5 +44,5 @@ * @returns | ||
: undefined; | ||
const ampFactor = calculateAmpFactor(swap.state); | ||
return { | ||
// TODO(igm): this should be calculated dynamically | ||
ampFactor: parseBigintIsh(swap.state.targetAmpFactor.toString()), | ||
ampFactor, | ||
fees: swap.state.fees, | ||
@@ -21,0 +49,0 @@ lpTotalSupply: new TokenAmount(exchange.lpToken, poolMintSupply !== null && poolMintSupply !== void 0 ? poolMintSupply : 0), |
@@ -13,3 +13,3 @@ { | ||
"author": "Saber Team <team@saber.so>", | ||
"version": "1.12.66", | ||
"version": "1.12.67", | ||
"main": "dist/cjs/index.js", | ||
@@ -30,3 +30,3 @@ "module": "dist/esm/index.js", | ||
"@types/lodash.mapvalues": "^4.6.7", | ||
"@types/node": "^17.0.25", | ||
"@types/node": "^17.0.27", | ||
"bn.js": "^5.2.0", | ||
@@ -41,6 +41,7 @@ "jsbi": "^4.3.0" | ||
"dependencies": { | ||
"@saberhq/solana-contrib": "^1.12.66", | ||
"@saberhq/token-utils": "^1.12.66", | ||
"@saberhq/solana-contrib": "^1.12.67", | ||
"@saberhq/token-utils": "^1.12.67", | ||
"@solana/buffer-layout": "^4.0.0", | ||
"lodash.mapvalues": "^4.6.0", | ||
"tiny-invariant": "^1.2.0", | ||
"tslib": "^2.4.0" | ||
@@ -55,3 +56,3 @@ }, | ||
}, | ||
"gitHead": "d16c89a492a5e821f89d71047188ba61b8c7557e" | ||
"gitHead": "8c2326383819d4dda1c21da554b2f706453c7484" | ||
} |
@@ -10,6 +10,9 @@ import type { TokenInfo } from "@saberhq/token-utils"; | ||
import type { Connection, PublicKey } from "@solana/web3.js"; | ||
import BN from "bn.js"; | ||
import type JSBI from "jsbi"; | ||
import invariant from "tiny-invariant"; | ||
import { SWAP_PROGRAM_ID } from "../constants"; | ||
import { StableSwap } from "../stable-swap"; | ||
import type { StableSwapState } from "../state"; | ||
import type { Fees } from "../state/fees"; | ||
@@ -57,2 +60,52 @@ import { loadProgramAccount } from "../util/account"; | ||
/** | ||
* Calculates the amp factor of a swap. | ||
* @param state | ||
* @param now | ||
* @returns | ||
*/ | ||
export const calculateAmpFactor = ( | ||
state: Pick< | ||
StableSwapState, | ||
| "initialAmpFactor" | ||
| "targetAmpFactor" | ||
| "startRampTimestamp" | ||
| "stopRampTimestamp" | ||
>, | ||
now = Date.now() / 1_000 | ||
): JSBI => { | ||
const { | ||
initialAmpFactor, | ||
targetAmpFactor, | ||
startRampTimestamp, | ||
stopRampTimestamp, | ||
} = state; | ||
// The most common case is that there is no ramp in progress. | ||
if (now >= stopRampTimestamp) { | ||
return parseBigintIsh(targetAmpFactor); | ||
} | ||
// If the ramp is about to start, use the initial amp. | ||
if (now <= startRampTimestamp) { | ||
return parseBigintIsh(initialAmpFactor); | ||
} | ||
invariant( | ||
stopRampTimestamp >= startRampTimestamp, | ||
"stop must be after start" | ||
); | ||
// Calculate how far we are along the ramp curve. | ||
const percent = | ||
now >= stopRampTimestamp | ||
? 1 | ||
: now <= startRampTimestamp | ||
? 0 | ||
: (now - startRampTimestamp) / (stopRampTimestamp - startRampTimestamp); | ||
const diff = Math.floor( | ||
parseFloat(targetAmpFactor.sub(initialAmpFactor).toString()) * percent | ||
); | ||
return parseBigintIsh(initialAmpFactor.add(new BN(diff))); | ||
}; | ||
/** | ||
* Creates an IExchangeInfo from parameters. | ||
@@ -81,5 +134,6 @@ * @returns | ||
const ampFactor = calculateAmpFactor(swap.state); | ||
return { | ||
// TODO(igm): this should be calculated dynamically | ||
ampFactor: parseBigintIsh(swap.state.targetAmpFactor.toString()), | ||
ampFactor, | ||
fees: swap.state.fees, | ||
@@ -86,0 +140,0 @@ lpTotalSupply: new TokenAmount(exchange.lpToken, poolMintSupply ?? 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
601327
7902
9
+ Addedtiny-invariant@^1.2.0