UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

167 lines (166 loc) 5.37 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.SolanaToken = void 0; const js_core_1 = require("@broxus/js-core"); const web3_js_1 = require("@solana/web3.js"); const mobx_1 = require("mobx"); const SolanaTokenUtils_1 = require("../../models/solana-token/SolanaTokenUtils"); const utils_1 = require("../../utils"); class SolanaToken extends js_core_1.AbstractStore { _connection; static Utils = SolanaTokenUtils_1.SolanaTokenUtils; constructor(_connection, data) { super(); this._connection = _connection; const isAddress = typeof data === 'string' || data instanceof web3_js_1.PublicKey; if (!isAddress && !('address' in data)) { throw new Error('Address is not specified'); } const address = (0, utils_1.resolveSolanaAddress)(isAddress ? data : data.address); if (isAddress) { this.setData(() => ({ address, tags: new Set(), })); } else if (data != null && typeof data === 'object' && !Array.isArray(data)) { this.setData(() => ({ ...data, address, tags: new Set(data.tags), })); } (0, mobx_1.makeObservable)(this); } static async create(connection, data, options) { const { sync = true } = { ...options }; const token = new SolanaToken(connection, data); if (sync) { const details = await SolanaToken.Utils.getDetails(token.address); if (!details?.symbol || details.decimals == null) { throw new Error('Token cannot be resolved'); } token.setData({ ...details }); } return token; } async balance(ownerAddress) { return SolanaToken.Utils.balance(this._connection, this.address, ownerAddress); } /** * Returns token root address as instance of Address. * @returns {SolanaTokenData['address']} */ get address() { return this._data.address; } /** * */ get chainId() { return this._data.chainId; } /** * */ get decimals() { return this._data.decimals; } /** * */ get icon() { return this._data.logoURI; } /** * */ get name() { return this._data.name; } /** * */ get root() { return this._data.address?.toBase58(); } /** * */ get symbol() { return this._data.symbol; } get(key) { return this._data[key]; } /** * Returns copy of the current token */ clone() { return new SolanaToken(this._connection, this.toJSON()); } } exports.SolanaToken = SolanaToken; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], SolanaToken.prototype, "balance", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "address", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "chainId", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "decimals", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "icon", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "name", null); __decorate([ mobx_1.computed, __metadata("design:type", String), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "root", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SolanaToken.prototype, "symbol", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [typeof (_a = typeof K !== "undefined" && K) === "function" ? _a : Object]), __metadata("design:returntype", Object) ], SolanaToken.prototype, "get", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", SolanaToken) ], SolanaToken.prototype, "clone", null);