Implement creditAmount without BN

This commit is contained in:
Simon Warta 2020-01-30 08:52:26 +01:00
parent d52f69afff
commit 3b10f4e783

View File

@ -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);
}