UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

193 lines (192 loc) 8.82 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mainnet = exports.Network = exports.uniqueTokens = void 0; const uuid_1 = require("uuid"); const dab_js_1 = require("@funded-labs/dab-js"); const errors_1 = require("../../../errors"); const utils_1 = require("../../utils"); const constants_1 = require("../../../utils/dfx/constants"); const tokens_1 = require("../../../constants/tokens"); const object_1 = require("../../../utils/object"); const dfx_1 = require("../../../utils/dfx"); const uniqueTokens = (tokens) => { const uniqueTokens = tokens.filter((token, index) => { return tokens.findIndex(t => t.canisterId === token.canisterId) === index; }); return uniqueTokens; }; exports.uniqueTokens = uniqueTokens; class Network { constructor(networkParams, fetch, blsVerify) { this.getTokenInfo = ({ canisterId, standard, defaultIdentity }) => __awaiter(this, void 0, void 0, function* () { if (!(0, utils_1.validateCanisterId)(canisterId)) { throw new Error(errors_1.ERRORS.INVALID_CANISTER_ID); } const agent = this.createAgent({ defaultIdentity }); const tokenActor = yield (0, dab_js_1.getTokenActor)({ canisterId, standard, agent, blsVerify: this.blsVerify, }); const metadata = yield tokenActor.getMetadata(); if (!('fungible' in metadata)) { throw new Error(errors_1.ERRORS.NON_FUNGIBLE_TOKEN_NOT_SUPPORTED); } const token = Object.assign(Object.assign({}, metadata.fungible), { canisterId, standard, registeredBy: [] }); this.registeredTokens = (0, exports.uniqueTokens)([ ...this.registeredTokens, token, ]); return token; }); this.getNftInfo = ({ canisterId, identity, standard }) => __awaiter(this, void 0, void 0, function* () { if (!(0, utils_1.validateCanisterId)(canisterId)) { throw new Error(errors_1.ERRORS.INVALID_CANISTER_ID); } try { const agent = this.createAgent({ defaultIdentity: identity }); const nftActor = (0, dab_js_1.getNFTActor)({ canisterId, agent, standard, blsVerify: this.blsVerify, }); const metadata = yield nftActor.getMetadata(); const nft = Object.assign(Object.assign({}, metadata), { registeredBy: [] }); return nft; } catch (e) { throw new Error(errors_1.ERRORS.CANISTER_INTERFACE_ERROR); } }); this.registerNFT = ({ canisterId, standard, walletId, identity }) => __awaiter(this, void 0, void 0, function* () { var _a; const nft = this.registeredNFTS.find(({ canisterId: id }) => id === canisterId); if (nft) { throw new Error(errors_1.ERRORS.NFT_ALREADY_REGISTERED); } const nftInfo = yield this.getNftInfo({ canisterId, identity, standard }); this.registeredNFTS = (0, exports.uniqueTokens)([ ...this.registeredNFTS, nftInfo, ]); this.registeredNFTS = this.registeredNFTS.map(n => n.canisterId === canisterId ? Object.assign(Object.assign({}, n), { registeredBy: [...n === null || n === void 0 ? void 0 : n.registeredBy, walletId] }) : n); yield ((_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this)); return this.registeredNFTS; }); this.registerToken = ({ canisterId, standard, walletId, defaultIdentity, logo, }) => __awaiter(this, void 0, void 0, function* () { var _b; const token = this.registeredTokens.find(({ canisterId: id }) => id === canisterId); const defaultToken = this.defaultTokens.find(({ canisterId: id }) => id === canisterId); if (defaultToken) { return this.defaultTokens; } if (!token) { yield this.getTokenInfo({ canisterId, standard, defaultIdentity }); } this.registeredTokens = this.registeredTokens.map(t => t.canisterId === canisterId ? Object.assign(Object.assign({}, t), { logo, registeredBy: [...t === null || t === void 0 ? void 0 : t.registeredBy, walletId] }) : t); yield ((_b = this.onChange) === null || _b === void 0 ? void 0 : _b.call(this)); return this.registeredTokens; }); this.removeToken = ({ canisterId, }) => __awaiter(this, void 0, void 0, function* () { var _c; if (!this.registeredTokens.map(t => t.canisterId).includes(canisterId)) { return this.registeredTokens; } const newTokens = this.registeredTokens.filter(t => t.canisterId !== canisterId); this.registeredTokens = newTokens; yield ((_c = this.onChange) === null || _c === void 0 ? void 0 : _c.call(this)); return newTokens; }); this.getTokens = walletId => { return [ ...this.defaultTokens, ...this.registeredTokens.filter(t => { var _a; return (_a = t === null || t === void 0 ? void 0 : t.registeredBy) === null || _a === void 0 ? void 0 : _a.includes(walletId); }), ]; }; this.name = networkParams.name; this.host = networkParams.host; this.onChange = networkParams.onChange; this.id = (networkParams === null || networkParams === void 0 ? void 0 : networkParams.id) || (0, uuid_1.v4)(); this.isCustom = true; this.ledgerCanisterId = networkParams.ledgerCanisterId || ''; this.defaultTokens = [ { name: 'ICP', symbol: 'ICP', canisterId: networkParams.ledgerCanisterId || '', standard: dab_js_1.standards.TOKEN.icp, decimals: 8, }, ]; this.registeredTokens = [...(networkParams.registeredTokens || [])]; this.registeredNFTS = [...(networkParams.registeredNFTS || [])]; this.fetch = fetch; this.blsVerify = blsVerify; } get tokens() { return [...this.defaultTokens, ...this.registeredTokens]; } tokenByCanisterId(canisterId) { return this.tokens.find(token => token.canisterId === canisterId); } edit({ name, host, ledgerCanisterId }) { var _a; this.name = name || this.name; this.host = host || this.host; this.ledgerCanisterId = ledgerCanisterId || this.ledgerCanisterId; (_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this); } createAgent({ defaultIdentity }) { const agent = (0, dfx_1.createAgent)({ defaultIdentity, host: this.host, fetch: this.fetch, wrapped: !this.isCustom, }); return agent; } toJSON() { var _a; return { name: this.name, host: this.host, ledgerCanisterId: this.ledgerCanisterId, registeredTokens: (_a = this.registeredTokens) === null || _a === void 0 ? void 0 : _a.map(object_1.recursiveParseBigint), id: this.id, }; } } exports.Network = Network; class Mainnet extends Network { constructor({ registeredTokens, onChange, }, fetch, blsVerify) { super({ onChange, registeredTokens, name: 'Mainnet', host: `https://${constants_1.IC_URL_HOST}`, ledgerCanisterId: tokens_1.TOKENS.ICP.canisterId, }, fetch); this.id = 'mainnet'; this.isCustom = false; this.defaultTokens = tokens_1.DEFAULT_MAINNET_TOKENS; this.registeredTokens = registeredTokens || []; } edit() { return; } } exports.Mainnet = Mainnet;