Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Merge 804b2e7 into 0d93c1c
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Feb 6, 2020
2 parents 0d93c1c + 804b2e7 commit 4e71bcd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/dex-js",
"version": "0.1.9",
"version": "0.1.11-RC.0",
"description": "dFusion JS integration: utils, contracts and other goodies",
"license": "MIT",
"bugs": {
Expand Down
11 changes: 7 additions & 4 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export const TWO = new BN(2)
export const TEN = new BN(10)

// Max allowance value for ERC20 approve
export const ALLOWANCE_MAX_VALUE = new BN(2).pow(new BN(256)).sub(ONE) // 115792089237316195423570985008687907853269984665640564039457584007913129639935

export const ALLOWANCE_MAX_VALUE = TWO.pow(new BN(256)).sub(ONE) // 115792089237316195423570985008687907853269984665640564039457584007913129639935
// Arbitrarily big number for checking if the token is enabled
export const ALLOWANCE_FOR_ENABLED_TOKEN = new BN(2).pow(new BN(128)) // 340282366920938463463374607431768211456
export const ALLOWANCE_FOR_ENABLED_TOKEN = TWO.pow(new BN(128)) // 340282366920938463463374607431768211456

// Default formatting constants
export const DEFAULT_DECIMALS = 4
export const DEFAULT_PRECISION = 18

// Model constants
export const FEE_DENOMINATOR = 1000 // Fee is 1/fee_denominator i.e. 1/1000 = 0.1%
Expand All @@ -21,5 +24,5 @@ export const FEE_PERCENTAGE = (1 / FEE_DENOMINATOR) * 100 // syntatic sugar for
// Amount for an order to be considered unlimited, from contract's point of view: https://github.com/gnosis/dex-contracts/blob/master/contracts/BatchExchange.sol#L35
export const UNLIMITED_ORDER_AMOUNT = TWO.pow(new BN(128)).sub(ONE)

// Batch ID of orders without expiration date set
// Furthest batch id possible (uint32), must be a js Number
export const MAX_BATCH_ID = 2 ** 32 - 1
3 changes: 2 additions & 1 deletion src/tokenList.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
"symbol": "sETH",
"decimals": 18,
"addressByNetwork": {
"1": "0x5e74C9036fb86BD7eCdcb084a0673EFc32eA31cb"
"1": "0x5e74C9036fb86BD7eCdcb084a0673EFc32eA31cb",
"4": "0xA83AbFdC9E8Ee990C3C6C0f56a4B06e0faAd583C"
},
"website": "https://www.synthetix.io",
"description": "Tracks the price of Ether (ETH) through price feeds supplied by an oracle",
Expand Down
61 changes: 39 additions & 22 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import BN from 'bn.js'
import { TEN } from 'const'
import { TEN, DEFAULT_PRECISION } from 'const'
import { TokenDetails } from 'types'

import BigNumber from 'bignumber.js'
const DEFAULT_DECIMALS = 4
const ELLIPSIS = '...'

function _getLocaleSymbols(): { thousands: string; decimals: string } {
// Check number representation in default locale
const formattedNumber = new Intl.NumberFormat(undefined).format(1000.1)
Expand All @@ -14,11 +13,9 @@ function _getLocaleSymbols(): { thousands: string; decimals: string } {
}
}
const { thousands: THOUSANDS_SYMBOL, decimals: DECIMALS_SYMBOL } = _getLocaleSymbols()

function _formatNumber(num: string): string {
return num.replace(/(\d)(?=(?:\d{3})+(?!\d))/g, '$1' + THOUSANDS_SYMBOL)
}

function _decomposeBn(amount: BN, amountPrecision: number, decimals: number): { integerPart: BN; decimalPart: BN } {
// Discard the decimals we don't need
// i.e. for WETH (precision=18, decimals=4) --> amount / 1e14
Expand All @@ -29,14 +26,23 @@ function _decomposeBn(amount: BN, amountPrecision: number, decimals: number): {
const amountRaw = amount.divRound(TEN.pow(new BN(amountPrecision - decimals)))
const integerPart = amountRaw.div(TEN.pow(new BN(decimals))) // 165000 / 10000 = 16
const decimalPart = amountRaw.mod(TEN.pow(new BN(decimals))) // 165000 % 10000 = 5000

// Discard the decimals we don't need
// i.e. for WETH (precision=18, decimals=4) --> amount / 1e14
// 1, 18: 16.5*1e18 ---> 165000

return { integerPart, decimalPart }
}

export function formatAmount(
amount: BN,
amountPrecision: number,
decimals?: number,
thousandSeparator?: boolean,
): string
export function formatAmount(
amount: null | undefined,
amountPrecision: number,
decimals?: number,
thousandSeparator?: boolean,
): null
export function formatAmount(
amount: BN | null | undefined,
amountPrecision: number,
Expand All @@ -48,9 +54,7 @@ export function formatAmount(
}
const actualDecimals = Math.min(amountPrecision, decimals)
const { integerPart, decimalPart } = _decomposeBn(amount, amountPrecision, actualDecimals)

const integerPartFmt = thousandSeparator ? _formatNumber(integerPart.toString()) : integerPart.toString()

if (decimalPart.isZero()) {
// Return just the integer part
return integerPartFmt
Expand All @@ -59,24 +63,22 @@ export function formatAmount(
.toString()
.padStart(actualDecimals, '0') // Pad the decimal part with leading zeros
.replace(/0+$/, '') // Remove the right zeros

// Return the formated integer plus the decimal
return integerPartFmt + DECIMALS_SYMBOL + decimalFmt
}
}

export function formatAmountFull(amount: BN, amountPrecision?: number, thousandSeparator?: boolean): string
export function formatAmountFull(amount?: undefined): null
export function formatAmountFull(
amount: BN | null | undefined,
amountPrecision: number,
amount?: BN,
amountPrecision = DEFAULT_PRECISION,
thousandSeparator = true,
): string | null {
if (!amount) {
return null
}

return formatAmount(amount, amountPrecision, amountPrecision, thousandSeparator)
}

/**
* Adjust the decimal precision of the given decimal value, without converting to/from BN or Number
* Takes in a string and returns a string
Expand All @@ -91,29 +93,24 @@ export function adjustPrecision(value: string | undefined | null, precision: num
if (!value) {
return ''
}

const regexp = new RegExp(`(\\.\\d{${precision}})\\d+$`)
return value.replace(regexp, '$1')
}

export function parseAmount(amountFmt: string, amountPrecision: number): BN | null {
if (!amountFmt) {
return null
}

const adjustedAmount = adjustPrecision(amountFmt, amountPrecision)
const groups = /^(\d+)(?:\.(\d+))?$/.exec(adjustedAmount)
if (groups) {
const [, integerPart, decimalPart = ''] = groups

const decimalBN = new BN(decimalPart.padEnd(amountPrecision, '0'))
const factor = TEN.pow(new BN(amountPrecision))
return new BN(integerPart).mul(factor).add(decimalBN)
} else {
return null
}
}

export function abbreviateString(inputString: string, prefixLength: number, suffixLength = 0): string {
// abbreviate only if it makes sense, and make sure ellipsis fits into word
// 1. always add ellipsis
Expand All @@ -128,7 +125,27 @@ export function abbreviateString(inputString: string, prefixLength: number, suff
const suffix = suffixLength > 0 ? inputString.slice(-suffixLength) : ''
return prefix + ELLIPSIS + suffix
}

export function safeTokenName(token: TokenDetails): string {
return token.symbol || token.name || abbreviateString(token.address, 6, 4)
}
export function safeFilledToken<T extends TokenDetails>(token: T): T {
return {
...token,
name: token.name || token.symbol || abbreviateString(token.address, 6, 4),
symbol: token.symbol || token.name || abbreviateString(token.address, 6, 4),
}
}
export function calculatePriceBigNumber(numerator?: BigNumber, denominator?: BigNumber): BigNumber | null {
if (!numerator || !denominator || denominator.isZero()) {
return null
}
return numerator.dividedBy(denominator)
}
export function formatPrice(
numerator?: BigNumber,
denominator?: BigNumber,
decimals = DEFAULT_DECIMALS,
): string | null {
const price = calculatePriceBigNumber(numerator, denominator)
return price ? price.toFixed(decimals) : null
}

0 comments on commit 4e71bcd

Please sign in to comment.