@pooltogether/utilities
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -306,3 +306,4 @@ declare const _default: { | ||
getLootBoxBatchName: (lootBoxAddress: any, lootBoxId: any) => string; | ||
calculateShareAmountFromUnderlyingTokenAmount: (underlyingTokenAmount: import("ethers").BigNumber, usersTicketBalance: import("ethers").BigNumber, usersTicketUnderlyingBalance: import("ethers").BigNumber) => import("ethers").BigNumber; | ||
underlyingAmountToSharesAmount: (underlyingAmount: import("ethers").BigNumber, pricePerShare: import("ethers").BigNumber, decimals?: string) => import("ethers").BigNumber; | ||
sharesAmountToUnderlyingAmount: (sharesAmount: import("ethers").BigNumber, pricePerShare: import("ethers").BigNumber, decimals?: string) => import("ethers").BigNumber; | ||
getChainIdByAlias: (networkAlias: string) => number; | ||
@@ -309,0 +310,0 @@ getNetworkNameAliasByChainId: (chainId: number) => string; |
import { BigNumber } from '@ethersproject/bignumber'; | ||
/** | ||
* Since pod shares don't map 1:1 to the underlying tokens, we need to calculate | ||
* the amount of pod shares based on the amount of underlying tokens requested | ||
* and the current conversion rate. | ||
* | ||
* Ex. Withdrawing pod tokens. We show the user has 101 underlying tokens, | ||
* but they really have 100 shares which is worth 101 underlying tokens. If | ||
* the user wants to withdraw 101 underlying tokens, we need to convert that | ||
* amount into shares. | ||
* | ||
* @param underlyingTokenAmount the amount to convert to shares | ||
* @param usersTicketBalance the users pod balance | ||
* @param usersTicketUnderlyingBalance the amount of underlying tokens | ||
* usersTicketBalance is worth | ||
* All values are unformatted (contain decimal padding) | ||
* @param underlyingAmount the amount to convert to shares | ||
* @param pricePerShare the conversion rate | ||
* @param decimals the decimals used for the pod | ||
* @returns share amount | ||
*/ | ||
export declare const calculateShareAmountFromUnderlyingTokenAmount: (underlyingTokenAmount: BigNumber, usersTicketBalance: BigNumber, usersTicketUnderlyingBalance: BigNumber) => BigNumber; | ||
export declare const underlyingAmountToSharesAmount: (underlyingAmount: BigNumber, pricePerShare: BigNumber, decimals?: string) => BigNumber; | ||
/** | ||
* All values are unformatted (contain decimal padding) | ||
* @param sharesAmount the amount to convert to underlying tokens | ||
* @param pricePerShare the conversion rate | ||
* @param decimals the decimals used for the pod | ||
* @returns underlying amount | ||
*/ | ||
export declare const sharesAmountToUnderlyingAmount: (sharesAmount: BigNumber, pricePerShare: BigNumber, decimals?: string) => BigNumber; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.calculateShareAmountFromUnderlyingTokenAmount = void 0; | ||
exports.sharesAmountToUnderlyingAmount = exports.underlyingAmountToSharesAmount = void 0; | ||
const units_1 = require("@ethersproject/units"); | ||
const CONSTANT_ROUNDING = units_1.parseUnits('1', 18); | ||
/** | ||
* Since pod shares don't map 1:1 to the underlying tokens, we need to calculate | ||
* the amount of pod shares based on the amount of underlying tokens requested | ||
* and the current conversion rate. | ||
* | ||
* Ex. Withdrawing pod tokens. We show the user has 101 underlying tokens, | ||
* but they really have 100 shares which is worth 101 underlying tokens. If | ||
* the user wants to withdraw 101 underlying tokens, we need to convert that | ||
* amount into shares. | ||
* | ||
* @param underlyingTokenAmount the amount to convert to shares | ||
* @param usersTicketBalance the users pod balance | ||
* @param usersTicketUnderlyingBalance the amount of underlying tokens | ||
* usersTicketBalance is worth | ||
* All values are unformatted (contain decimal padding) | ||
* @param underlyingAmount the amount to convert to shares | ||
* @param pricePerShare the conversion rate | ||
* @param decimals the decimals used for the pod | ||
* @returns share amount | ||
*/ | ||
const calculateShareAmountFromUnderlyingTokenAmount = (underlyingTokenAmount, usersTicketBalance, usersTicketUnderlyingBalance) => { | ||
const userSharesToUnderlyingPercentage = usersTicketBalance | ||
.mul(CONSTANT_ROUNDING) | ||
.div(usersTicketUnderlyingBalance); | ||
const shareAmount = underlyingTokenAmount | ||
.mul(userSharesToUnderlyingPercentage) | ||
.div(CONSTANT_ROUNDING); | ||
return shareAmount; | ||
const underlyingAmountToSharesAmount = (underlyingAmount, pricePerShare, decimals = '18') => { | ||
return underlyingAmount.mul(units_1.parseUnits('1', decimals)).div(pricePerShare); | ||
}; | ||
exports.calculateShareAmountFromUnderlyingTokenAmount = calculateShareAmountFromUnderlyingTokenAmount; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9kcy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9wb2RzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUNBLGdEQUFpRDtBQUVqRCxNQUFNLGlCQUFpQixHQUFHLGtCQUFVLENBQUMsR0FBRyxFQUFFLEVBQUUsQ0FBQyxDQUFBO0FBRTdDOzs7Ozs7Ozs7Ozs7Ozs7R0FlRztBQUNJLE1BQU0sNkNBQTZDLEdBQUcsQ0FDM0QscUJBQWdDLEVBQ2hDLGtCQUE2QixFQUM3Qiw0QkFBdUMsRUFDNUIsRUFBRTtJQUNiLE1BQU0sZ0NBQWdDLEdBQUcsa0JBQWtCO1NBQ3hELEdBQUcsQ0FBQyxpQkFBaUIsQ0FBQztTQUN0QixHQUFHLENBQUMsNEJBQTRCLENBQUMsQ0FBQTtJQUNwQyxNQUFNLFdBQVcsR0FBRyxxQkFBcUI7U0FDdEMsR0FBRyxDQUFDLGdDQUFnQyxDQUFDO1NBQ3JDLEdBQUcsQ0FBQyxpQkFBaUIsQ0FBQyxDQUFBO0lBQ3pCLE9BQU8sV0FBVyxDQUFBO0FBQ3BCLENBQUMsQ0FBQTtBQVpZLFFBQUEsNkNBQTZDLGlEQVl6RCJ9 | ||
exports.underlyingAmountToSharesAmount = underlyingAmountToSharesAmount; | ||
/** | ||
* All values are unformatted (contain decimal padding) | ||
* @param sharesAmount the amount to convert to underlying tokens | ||
* @param pricePerShare the conversion rate | ||
* @param decimals the decimals used for the pod | ||
* @returns underlying amount | ||
*/ | ||
const sharesAmountToUnderlyingAmount = (sharesAmount, pricePerShare, decimals = '18') => { | ||
return sharesAmount.mul(pricePerShare).div(units_1.parseUnits('1', decimals)); | ||
}; | ||
exports.sharesAmountToUnderlyingAmount = sharesAmountToUnderlyingAmount; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9kcy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9wb2RzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUNBLGdEQUFpRDtBQUVqRDs7Ozs7O0dBTUc7QUFDSSxNQUFNLDhCQUE4QixHQUFHLENBQzVDLGdCQUEyQixFQUMzQixhQUF3QixFQUN4QixXQUFtQixJQUFJLEVBQ1osRUFBRTtJQUNiLE9BQU8sZ0JBQWdCLENBQUMsR0FBRyxDQUFDLGtCQUFVLENBQUMsR0FBRyxFQUFFLFFBQVEsQ0FBQyxDQUFDLENBQUMsR0FBRyxDQUFDLGFBQWEsQ0FBQyxDQUFBO0FBQzNFLENBQUMsQ0FBQTtBQU5ZLFFBQUEsOEJBQThCLGtDQU0xQztBQUVEOzs7Ozs7R0FNRztBQUNJLE1BQU0sOEJBQThCLEdBQUcsQ0FDNUMsWUFBdUIsRUFDdkIsYUFBd0IsRUFDeEIsV0FBbUIsSUFBSSxFQUNaLEVBQUU7SUFDYixPQUFPLFlBQVksQ0FBQyxHQUFHLENBQUMsYUFBYSxDQUFDLENBQUMsR0FBRyxDQUFDLGtCQUFVLENBQUMsR0FBRyxFQUFFLFFBQVEsQ0FBQyxDQUFDLENBQUE7QUFDdkUsQ0FBQyxDQUFBO0FBTlksUUFBQSw4QkFBOEIsa0NBTTFDIn0= |
{ | ||
"name": "@pooltogether/utilities", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Shared utility functions across PoolTogether apps", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
206460
3422