@yoroi/portfolio
Version:
The Portfolio package of Yoroi SDK
62 lines • 1.68 kB
JavaScript
import { freeze } from 'immer';
function negate(amounts) {
return freeze(Object.fromEntries(Object.entries(amounts).map(_ref => {
let [tokenId, amount] = _ref;
return [tokenId, {
...amount,
quantity: -amount.quantity
}];
})), true);
}
function drop(amounts, removeTokenIds) {
return freeze(Object.fromEntries(Object.entries(amounts).filter(_ref2 => {
let [tokenId] = _ref2;
return !removeTokenIds.includes(tokenId);
})), true);
}
function plus(amounts) {
const entries = amounts.map(amount => Object.entries(amount)).flat();
return freeze(entries.reduce((result, _ref3) => {
let [id, {
info,
quantity
}] = _ref3;
const tokenId = id;
result[tokenId] = {
info,
quantity: (result[tokenId]?.quantity ?? 0n) + quantity
};
return result;
}, {}), true);
}
function minus(amounts1, amounts2) {
const negatedAmounts2 = negate(amounts2);
return freeze(plus([amounts1, negatedAmounts2]), true);
}
export const AmountsCalcultor = function () {
let initialAmounts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
let amounts = {
...initialAmounts
};
const builder = {
negate: () => {
amounts = negate(amounts);
return builder;
},
drop: removeTokenIds => {
amounts = drop(amounts, removeTokenIds);
return builder;
},
plus: newAmounts => {
amounts = plus([amounts, newAmounts]);
return builder;
},
minus: newAmounts => {
amounts = minus(amounts, newAmounts);
return builder;
},
build: () => amounts
};
return builder;
};
//# sourceMappingURL=amounts-calculator.js.map