@provenanceio/wallet-utils
Version:
Typescript Utilities for Provenance Blockchain Wallet
67 lines (49 loc) • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCoinList = void 0;
var _coin_pb = require("../../proto/cosmos/base/v1beta1/coin_pb");
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* 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[].
*/
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_pb.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;
});
};
exports.getCoinList = getCoinList;