Round subscription price and payment amount

This commit is contained in:
silverpill 2022-06-26 11:33:23 +00:00
parent 251dea508b
commit b856f23eb1
3 changed files with 29 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import { DateTime } from "luxon"
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum" import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum"
import { roundBigNumber } from "@/utils/numbers"
import { http } from "./common" import { http } from "./common"
import { Contracts, getContract } from "./contracts" import { Contracts, getContract } from "./contracts"
@ -70,7 +71,7 @@ export class Subscription {
} }
get pricePerMonthInt(): BigNumber { get pricePerMonthInt(): BigNumber {
return this.price.mul(SECONDS_IN_MONTH) return roundBigNumber(this.price.mul(SECONDS_IN_MONTH), 4)
} }
get pricePerMonth(): FixedNumber { get pricePerMonth(): FixedNumber {

13
src/utils/numbers.ts Normal file
View file

@ -0,0 +1,13 @@
import { BigNumber } from "@ethersproject/bignumber"
export function roundBigNumber(value: BigNumber, precision: number): BigNumber {
const decimals = value.toString().length
const divisor = BigNumber.from(10).pow(decimals - precision)
const remainder = value.mod(divisor)
const midpoint = BigNumber.from(10).pow(Math.max(decimals - precision - 1, 0)).mul(5)
if (remainder.gte(midpoint)) {
return value.div(divisor).add(1).mul(divisor)
} else {
return value.div(divisor).mul(divisor)
}
}

View file

@ -0,0 +1,14 @@
import { expect } from "chai"
import { BigNumber } from "@ethersproject/bignumber"
import { roundBigNumber } from "@/utils/numbers"
describe("Numbers utils", () => {
it("Should round big number", () => {
const value = BigNumber.from(534985)
expect(roundBigNumber(value, 2).toNumber()).to.equal(530000)
expect(roundBigNumber(value, 3).toNumber()).to.equal(535000)
expect(roundBigNumber(value, 4).toNumber()).to.equal(535000)
expect(roundBigNumber(value, 5).toNumber()).to.equal(534990)
expect(roundBigNumber(value, 6).toNumber()).to.equal(534985)
})
})