From 75d099d76d52288174391739b0c7eb1dc5671d6c Mon Sep 17 00:00:00 2001 From: Facu Spagnuolo Date: Tue, 29 Oct 2024 10:41:11 -0300 Subject: [PATCH] Numbers: Add to FP helper (#1) * numbers: add to fp helper * numbers: improve tests --- src/numbers.ts | 16 ++++---- test/src/numbers.spec.ts | 80 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 test/src/numbers.spec.ts diff --git a/src/numbers.ts b/src/numbers.ts index d6ea033..7bd5f52 100644 --- a/src/numbers.ts +++ b/src/numbers.ts @@ -1,23 +1,23 @@ import { Decimal } from 'decimal.js' import { BigNumber } from 'ethers' -const SCALING_FACTOR = 1e18 +export type BigNumberish = string | number | BigNumber | Decimal -export type BigNumberish = string | number | BigNumber +export const decimal = (x: BigNumberish): Decimal => new Decimal(x.toString()) -export const decimal = (x: BigNumberish | Decimal): Decimal => new Decimal(x.toString()) +export const toFP = (x: BigNumberish, decimals: number): BigNumber => bn(decimal(x).mul(decimal(10).pow(decimals))) -export const fp = (x: number | string | Decimal): BigNumber => bn(decimal(x).mul(SCALING_FACTOR)) +export const fp = (x: BigNumberish): BigNumber => toFP(x, 18) -export const toWBTC = (x: number | string | Decimal): BigNumber => fp(x).div(1e10) +export const toWBTC = (x: BigNumberish): BigNumber => toFP(x, 8) -export const toUSDC = (x: number | string | Decimal): BigNumber => fp(x).div(1e12) +export const toUSDC = (x: BigNumberish): BigNumber => toFP(x, 6) export const toUSDT = toUSDC -export const pct = (x: BigNumber, p: number): BigNumber => x.mul(fp(p)).div(fp(1)) +export const pct = (x: BigNumberish, p: number): BigNumber => bn(x).mul(fp(p)).div(fp(1)) -export const bn = (x: BigNumberish | Decimal): BigNumber => { +export const bn = (x: BigNumberish): BigNumber => { if (BigNumber.isBigNumber(x)) return x const stringified = parseScientific(x.toString()) const integer = stringified.split('.')[0] diff --git a/test/src/numbers.spec.ts b/test/src/numbers.spec.ts new file mode 100644 index 0000000..6336b24 --- /dev/null +++ b/test/src/numbers.spec.ts @@ -0,0 +1,80 @@ +import { expect } from 'chai' + +import { bn, decimal, fp, toFP, toUSDC, toUSDT, toWBTC } from '../../src/numbers' + +describe('numbers', () => { + describe('fp', () => { + it('converts to fp with the expected decimals', () => { + expect(fp(0.1).toString()).to.be.equal('100000000000000000') + expect(fp(1).toString()).to.be.equal('1000000000000000000') + expect(fp(1.1).toString()).to.be.equal('1100000000000000000') + }) + + it('supports any bignumberish', () => { + expect(fp('1').toString()).to.be.equal('1000000000000000000') + expect(fp(bn(1)).toString()).to.be.equal('1000000000000000000') + expect(fp(decimal(1)).toString()).to.be.equal('1000000000000000000') + }) + }) + + describe('toFP', () => { + it('converts to fp with the expected decimals', () => { + expect(toFP(1, 6).toString()).to.be.equal('1000000') + expect(toFP(1.1, 6).toString()).to.be.equal('1100000') + + expect(toFP(1, 18).toString()).to.be.equal('1000000000000000000') + expect(toFP(1.1, 18).toString()).to.be.equal('1100000000000000000') + + expect(toFP(1, 20).toString()).to.be.equal('100000000000000000000') + expect(toFP(1.1, 20).toString()).to.be.equal('110000000000000000000') + }) + + it('supports any bignumberish', () => { + expect(toFP('1', 18).toString()).to.be.equal('1000000000000000000') + expect(toFP(bn(1), 18).toString()).to.be.equal('1000000000000000000') + expect(toFP(decimal(1), 18).toString()).to.be.equal('1000000000000000000') + }) + }) + + describe('toWBTC', () => { + it('converts to fp with the expected decimals', () => { + expect(toWBTC(0.1).toString()).to.be.equal('10000000') + expect(toWBTC(1).toString()).to.be.equal('100000000') + expect(toWBTC(1.1).toString()).to.be.equal('110000000') + }) + + it('supports any bignumberish', () => { + expect(toWBTC('1').toString()).to.be.equal('100000000') + expect(toWBTC(bn(1)).toString()).to.be.equal('100000000') + expect(toWBTC(decimal(1)).toString()).to.be.equal('100000000') + }) + }) + + describe('toUSDC', () => { + it('converts to fp with the expected decimals', () => { + expect(toUSDC(0.1).toString()).to.be.equal('100000') + expect(toUSDC(1).toString()).to.be.equal('1000000') + expect(toUSDC(1.1).toString()).to.be.equal('1100000') + }) + + it('supports any bignumberish', () => { + expect(toUSDC('1').toString()).to.be.equal('1000000') + expect(toUSDC(bn(1)).toString()).to.be.equal('1000000') + expect(toUSDC(decimal(1)).toString()).to.be.equal('1000000') + }) + }) + + describe('toUSDT', () => { + it('converts to fp with the expected decimals', () => { + expect(toUSDT(0.1).toString()).to.be.equal('100000') + expect(toUSDT(1).toString()).to.be.equal('1000000') + expect(toUSDT(1.1).toString()).to.be.equal('1100000') + }) + + it('supports any bignumberish', () => { + expect(toUSDT('1').toString()).to.be.equal('1000000') + expect(toUSDT(bn(1)).toString()).to.be.equal('1000000') + expect(toUSDT(decimal(1)).toString()).to.be.equal('1000000') + }) + }) +})