@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
33 lines • 1.78 kB
JavaScript
export const pow10 = (exponant) => 10n ** BigInt(exponant);
export const ORACLE_PRICE_SCALE = pow10(36);
export const WAD = pow10(18);
export const SECONDS_PER_YEAR = 3600 * 24 * 365;
export const VIRTUAL_ASSETS = 1n;
export const VIRTUAL_SHARES = 10n ** 6n;
export const MAX_UINT256 = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
/// -----------------------------------------------
/// -------------------- UTILS --------------------
/// -----------------------------------------------
export const wMulDown = (x, y) => mulDivDown(x, y, WAD);
export const wDivDown = (x, y) => mulDivDown(x, WAD, y);
export const wDivUp = (x, y) => mulDivUp(x, WAD, y);
export const mulDivDown = (x, y, d) => (BigInt(x) * BigInt(y)) / BigInt(d);
const mulDivUp = (x, y, d) => (BigInt(x) * BigInt(y) + (BigInt(d) - 1n)) / BigInt(d);
export const wTaylorCompounded = (x, n) => {
const firstTerm = BigInt(x) * BigInt(n);
const secondTerm = mulDivDown(firstTerm, firstTerm, 2n * WAD);
const thirdTerm = mulDivDown(secondTerm, firstTerm, 3n * WAD);
return firstTerm + secondTerm + thirdTerm;
};
export const toAssetsDown = (shares, totalAssets, totalShares) => {
return mulDivDown(shares, totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
};
/// @dev Calculates the value of `shares` quoted in assets, rounding down.
export const toSharesDown = (assets, totalAssets, totalShares) => {
return mulDivDown(assets, totalShares + VIRTUAL_SHARES, totalAssets + VIRTUAL_ASSETS);
};
/// @dev Calculates the value of `shares` quoted in assets, rounding up.
export const toAssetsUp = (shares, totalAssets, totalShares) => {
return mulDivUp(shares, totalAssets + VIRTUAL_ASSETS, totalShares + VIRTUAL_SHARES);
};
//# sourceMappingURL=math.js.map