UNPKG

@fleupold/dex-contracts

Version:

Contracts for dFusion multi-token batch auction exchange

57 lines (56 loc) 3.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUnlimitedOrderAmounts = exports.getBuyAmountFromPrice = exports.getUnitPrice = void 0; const bn_js_1 = __importDefault(require("bn.js")); const assert_1 = __importDefault(require("assert")); const fraction_1 = require("./fraction"); const MAX128 = new bn_js_1.default(2).pow(new bn_js_1.default(128)).subn(1); /** * Modifies the price to work with ERC20 units * @param price - Amount of buy token in exchange for one sell token * @param sellTokenDecimals - Number of decimals of the sell token * @param buyTokenDecimals - Number of decimals of the buy token * Returns Fraction representing the number of buy tokens in exchange for one unit of sell token */ function getUnitPrice(price, sellTokenDecimals, buyTokenDecimals) { assert_1.default(sellTokenDecimals >= 0, "sell token decimals must be non-negative"); assert_1.default(buyTokenDecimals >= 0, "buy token decimals must be non-negative"); return fraction_1.Fraction.fromNumber(price).mul(new fraction_1.Fraction(new bn_js_1.default(10).pow(new bn_js_1.default(buyTokenDecimals)), new bn_js_1.default(10).pow(new bn_js_1.default(sellTokenDecimals)))); } exports.getUnitPrice = getUnitPrice; /** * Computes the amount of output token units from their price and the amount of input token units * Note that the price is expressed in terms of tokens, while the amounts are in terms of token units * @param price - Amount of buy token in exchange for one sell token * @param sellAmount - Amount of sell token units that are exchanged at price * @param sellTokenDecimals - Number of decimals of the sell token * @param buyTokenDecimals - Number of decimals of the buy token * Returns amount of output token units obtained */ function getBuyAmountFromPrice(price, sellAmount, sellTokenDecimals, buyTokenDecimals) { const unitPrice = getUnitPrice(price, sellTokenDecimals, buyTokenDecimals); const buyAmount = unitPrice.mul(new fraction_1.Fraction(sellAmount, 1)); return buyAmount.toBN(); } exports.getBuyAmountFromPrice = getBuyAmountFromPrice; /** * Computes the buy and sell token amounts required for an unlimited order in the exchange * @param price - Price of the buyToken relative to one sell token * @param sellTokenDecimals - Number of decimals of the sell token * @param buyTokenDecimals - Number of decimals of the buy token * Returns amounts of sell-buy token for an unlimited order at the input price */ function getUnlimitedOrderAmounts(price, sellTokenDecimals, buyTokenDecimals) { let sellAmount = MAX128.clone(); let buyAmount = getBuyAmountFromPrice(price, sellAmount, sellTokenDecimals, buyTokenDecimals); if (buyAmount.gt(sellAmount)) { buyAmount = MAX128.clone(); sellAmount = getBuyAmountFromPrice(1 / price, buyAmount, buyTokenDecimals, sellTokenDecimals); assert_1.default(buyAmount.gte(sellAmount), "Error: unable to create unlimited order"); } return { base: sellAmount, quote: buyAmount }; } exports.getUnlimitedOrderAmounts = getUnlimitedOrderAmounts;