From d3570df968e9a9bb57797c40bb3938ec8f14a765 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 16 Mar 2021 14:31:13 +0000 Subject: [PATCH] Feature/add currency helpers (#531) * Exporting Coin struct, needed for wallet * Adding currency helpers --- clients/validator/package.json | 5 +-- clients/validator/src/currency.ts | 59 +++++++++++++++++++++++++++++++ clients/validator/src/index.ts | 9 +++-- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 clients/validator/src/currency.ts diff --git a/clients/validator/package.json b/clients/validator/package.json index 2041449514..099fa3f2d9 100644 --- a/clients/validator/package.json +++ b/clients/validator/package.json @@ -34,6 +34,7 @@ "@cosmjs/cosmwasm-stargate": "^0.24.0-alpha.18", "@cosmjs/crypto": "^0.24.0-alpha.18", "@cosmjs/launchpad": "^0.24.0-alpha.18", - "@cosmjs/proto-signing": "^0.24.0-alpha.18" + "@cosmjs/proto-signing": "^0.24.0-alpha.18", + "@cosmjs/math": "^0.24.0-alpha.18" } -} +} \ No newline at end of file diff --git a/clients/validator/src/currency.ts b/clients/validator/src/currency.ts new file mode 100644 index 0000000000..608a5526f0 --- /dev/null +++ b/clients/validator/src/currency.ts @@ -0,0 +1,59 @@ +import { Decimal } from "@cosmjs/math"; +import { Coin } from "."; + +// NARROW NO-BREAK SPACE (U+202F) +const thinSpace = "\u202F"; + +export function printableCoin(coin?: Coin): string { + if (!coin) { + return "0"; + } + if (coin.denom.startsWith("u")) { + const ticker = coin.denom.slice(1).toUpperCase(); + return Decimal.fromAtomics(coin.amount, 6).toString() + thinSpace + ticker; + } else { + return coin.amount + thinSpace + coin.denom; + } +} + +export function printableBalance(balance?: readonly Coin[]): string { + if (!balance || balance.length === 0) return "–"; + return balance.map(printableCoin).join(", "); +} + +export interface MappedCoin { + readonly denom: string; + readonly fractionalDigits: number; +} + +export interface CoinMap { + readonly [key: string]: MappedCoin; +} + +export function nativeCoinToDisplay(coin: Coin, coinMap: CoinMap): Coin { + if (!coinMap) return coin; + + const coinToDisplay = coinMap[coin.denom]; + if (!coinToDisplay) return coin; + + const amountToDisplay = Decimal.fromAtomics(coin.amount, coinToDisplay.fractionalDigits).toString(); + + return { denom: coinToDisplay.denom, amount: amountToDisplay }; +} + +// display amount is eg "12.0346", return is in native tokens +// with 6 fractional digits, this would be eg. "12034600" +export function displayAmountToNative( + amountToDisplay: string, + coinMap: CoinMap, + nativeDenom: string, +): string { + const fractionalDigits = coinMap[nativeDenom]?.fractionalDigits; + if (fractionalDigits) { + // use https://github.com/CosmWasm/cosmjs/blob/v0.22.2/packages/math/src/decimal.ts + const decimalAmount = Decimal.fromUserInput(amountToDisplay, fractionalDigits); + return decimalAmount.atomics; + } + + return amountToDisplay; +} diff --git a/clients/validator/src/index.ts b/clients/validator/src/index.ts index 7a83690b1c..dcfd905963 100644 --- a/clients/validator/src/index.ts +++ b/clients/validator/src/index.ts @@ -1,15 +1,18 @@ import NetClient, { INetClient } from "./net-client"; -import { MixNode, MixNodeBond } from "./types"; +import { Gateway, GatewayBond, MixNode, MixNodeBond } from "./types"; // import * as fs from "fs"; import { Bip39, Random } from "@cosmjs/crypto"; import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; import MixnodesCache from "./caches/mixnodes"; -import { Coin, coins } from "@cosmjs/launchpad"; +import { coin, Coin, coins } from "@cosmjs/launchpad"; import { BroadcastTxResponse } from "@cosmjs/stargate/types" import { ExecuteResult, InstantiateOptions, InstantiateResult, UploadMeta, UploadResult } from "@cosmjs/cosmwasm"; +import { CoinMap, displayAmountToNative, MappedCoin, nativeCoinToDisplay, printableBalance, printableCoin } from "./currency"; +import GatewaysCache from "./caches/gateways"; export { coins }; export { Coin }; +export { displayAmountToNative, nativeCoinToDisplay, printableCoin, printableBalance, MappedCoin, CoinMap } export default class ValidatorClient { private readonly stakeDenom: string = "unym" @@ -124,7 +127,7 @@ export default class ValidatorClient { console.log(`account ${this.address} unbonded mixnode`); return result; } - + /** * Get or refresh the list of gateways in the network. *