From 3b10f4e78333b6203429a5651d124b7d4eed0618 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 30 Jan 2020 08:52:26 +0100 Subject: [PATCH] Implement creditAmount without BN --- packages/faucet/src/cashflow.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/faucet/src/cashflow.ts b/packages/faucet/src/cashflow.ts index 987b34e1..73a1957c 100644 --- a/packages/faucet/src/cashflow.ts +++ b/packages/faucet/src/cashflow.ts @@ -1,7 +1,7 @@ import BN = require("bn.js"); import { Account, Amount, TokenTicker } from "@iov/bcp"; -import { Int53 } from "@iov/encoding"; +import { Uint53 } from "@iov/encoding"; /** Send `factor` times credit amount on refilling */ const defaultRefillFactor = 20; @@ -24,15 +24,14 @@ export function getFractionalDigits(): number { } /** The amount of tokens that will be sent to the user */ -export function creditAmount(token: TokenTicker, factor = 1): Amount { +export function creditAmount(token: TokenTicker, factor: Uint53 = new Uint53(1)): Amount { const amountFromEnv = process.env[`FAUCET_CREDIT_AMOUNT_${token}`]; - const wholeNumber = amountFromEnv ? Int53.fromString(amountFromEnv).toNumber() : 10; - const total = wholeNumber * factor; + const amount = amountFromEnv ? Uint53.fromString(amountFromEnv).toNumber() : 10; + const value = new Uint53(amount * factor.toNumber()); + const fractionalDigits = getFractionalDigits(); - // replace BN with BigInt with TypeScript 3.2 and node 11 - const quantity = new BN(total).imul(new BN(10).pow(new BN(fractionalDigits))).toString(); return { - quantity: quantity, + quantity: value.toString() + "0".repeat(fractionalDigits), fractionalDigits: fractionalDigits, tokenTicker: token, }; @@ -40,13 +39,13 @@ export function creditAmount(token: TokenTicker, factor = 1): Amount { export function refillAmount(token: TokenTicker): Amount { const factorFromEnv = Number.parseInt(process.env.FAUCET_REFILL_FACTOR || "0", 10) || undefined; - const factor = factorFromEnv || defaultRefillFactor; + const factor = new Uint53(factorFromEnv || defaultRefillFactor); return creditAmount(token, factor); } export function refillThreshold(token: TokenTicker): Amount { const factorFromEnv = Number.parseInt(process.env.FAUCET_REFILL_THRESHOLD || "0", 10) || undefined; - const factor = factorFromEnv || defaultRefillThresholdFactor; + const factor = new Uint53(factorFromEnv || defaultRefillThresholdFactor); return creditAmount(token, factor); }