@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
36 lines (35 loc) • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tokens_1 = require("../../../constants/tokens");
const Network_1 = require("../../../PlugKeyRing/modules/NetworkModule/Network");
exports.default = (storage) => {
var _a, _b, _c;
const defaultTokenIds = tokens_1.DEFAULT_MAINNET_TOKENS.map((t) => t.canisterId);
const wallets = storage.wallets;
// For each wallet, get its tokens and add the wallet id to the list of registered by
const nestedTokens = wallets === null || wallets === void 0 ? void 0 : wallets.map((wallet) => (Object.values(wallet === null || wallet === void 0 ? void 0 : wallet.assets) || []).map((asset) => {
var _a, _b;
return (Object.assign(Object.assign({}, ((asset === null || asset === void 0 ? void 0 : asset.token) || {})), { registeredBy: [wallet.walletNumber], logo: ((_a = asset === null || asset === void 0 ? void 0 : asset.token) === null || _a === void 0 ? void 0 : _a.logo) || ((_b = asset === null || asset === void 0 ? void 0 : asset.token) === null || _b === void 0 ? void 0 : _b.image) }));
}));
const tokens = ((_a = nestedTokens === null || nestedTokens === void 0 ? void 0 : nestedTokens.flat) === null || _a === void 0 ? void 0 : _a.call(nestedTokens)) || [];
// Remove default tokens and merge duplicates, concatting their registeredBy
const registeredTokens = tokens.reduce((acum, token) => {
// Remove default tokens
if (defaultTokenIds.includes(token.canisterId)) {
return acum;
}
// If we already had this token, only update the registeredby array
const registeredTokenIndex = acum.findIndex((t) => t.canisterId === token.canisterId);
if (registeredTokenIndex > -1) {
const registeredToken = acum[registeredTokenIndex];
const updatedToken = Object.assign(Object.assign({}, registeredToken), { registeredBy: [...registeredToken.registeredBy, ...token.registeredBy] });
const copy = [...acum];
copy[registeredTokenIndex] = updatedToken;
return copy;
}
// Else just add the token (first time we find it)
return [...acum, token];
}, []);
const newStorage = Object.assign(Object.assign({}, storage), { networkModule: Object.assign(Object.assign({}, (storage.networkModule || {})), { networks: Object.assign(Object.assign({}, (((_b = storage === null || storage === void 0 ? void 0 : storage.networkModule) === null || _b === void 0 ? void 0 : _b.networks) || {})), { mainnet: (new Network_1.Mainnet({ registeredTokens }, fetch)).toJSON() }), networkId: ((_c = storage === null || storage === void 0 ? void 0 : storage.networkModule) === null || _c === void 0 ? void 0 : _c.networkId) || 'mainnet' }) });
return newStorage;
};