Feature/add currency helpers (#531)
* Exporting Coin struct, needed for wallet * Adding currency helpers
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user