UNPKG

@provenanceio/wallet-utils

Version:

Typescript Utilities for Provenance Blockchain Wallet

47 lines (38 loc) 1.7 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import { Coin } from '../../proto/cosmos/base/v1beta1/coin_pb'; /** * This function accepts a list of fees of any denom and * returns a list of Coins * @param coinsList: List of coins * @returns List of coins of type Coin[]. */ export var getCoinList = function getCoinList(coinsList) { return coinsList.reduce(function (agg, curr) { // Find if the same coin is already in the aggregated list var sameCoin = agg.find(function (i) { return i.denom === curr.denom; }); if (sameCoin) { // if it is find the index of it var sameCoinInd = agg.findIndex(function (i) { return i.denom === sameCoin.denom; }); // create a new array from the aggregate so we don't mutate it var result = _toConsumableArray(agg); // change the item in place to add the current amount to whatever it currently is result[sameCoinInd] = { amount: +sameCoin.amount + +curr.amount, denom: curr.denom }; // return the resulting array return result; } // if the coin wasn't already in the aggregate just add it to the aggregate here return [].concat(_toConsumableArray(agg), [curr]); }, []) // sort by denom name in ascending order (assumes all denoms are lowercase) .sort(function (a, b) { return a.denom > b.denom ? 1 : -1; }).map(function (feeItem) { // map each feeItem and create a coin out of it var feeCoin = new Coin(); feeCoin.setDenom(feeItem.denom); // since the amount can be a string or number we convert it to a string here feeCoin.setAmount(feeItem.amount.toString()); return feeCoin; }); };