mirror of
https://github.com/zoo-labs/zoo.git
synced 2026-11-10 22:32:34 +00:00
27 lines
986 B
TypeScript
27 lines
986 B
TypeScript
// Bootstrap...
|
|
import { BigNumber, BigNumberish } from '@ethersproject/bignumber'
|
|
|
|
import Fraction from './entities/Fraction'
|
|
import { Zero } from '@ethersproject/constants'
|
|
import { parseUnits } from '@ethersproject/units'
|
|
|
|
BigNumber.prototype.mulDiv = function (multiplier: BigNumberish, divisor: BigNumberish): BigNumber {
|
|
return BigNumber.from(divisor).gt(0) ? BigNumber.from(this).mul(multiplier).div(divisor) : Zero
|
|
}
|
|
|
|
BigNumber.prototype.toFraction = function (decimals: BigNumberish = 18): Fraction {
|
|
return Fraction.from(this, decimals ? BigNumber.from(10).pow(decimals) : Zero)
|
|
}
|
|
|
|
BigNumber.prototype.toFixed = function (decimals: BigNumberish = 18, maxFractions: BigNumberish = 8): string {
|
|
return this.toFraction(decimals, 10).toString(BigNumber.from(maxFractions).toNumber())
|
|
}
|
|
|
|
String.prototype.toBigNumber = function (decimals: BigNumberish): BigNumber {
|
|
try {
|
|
return parseUnits(this as string, decimals)
|
|
} catch (error) {
|
|
}
|
|
return BigNumber.from(0)
|
|
}
|