UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

218 lines (217 loc) 7.63 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.TonToken = void 0; const js_core_1 = require("@broxus/js-core"); const js_utils_1 = require("@broxus/js-utils"); const ton_1 = require("@ton/ton"); const mobx_1 = require("mobx"); const TonTokenUtils_1 = require("../../models/ton-token/TonTokenUtils"); const ton_token_wallet_1 = require("../../models/ton-token-wallet"); const utils_1 = require("../../utils"); class TonToken extends js_core_1.AbstractStore { _connection; options; static Utils = TonTokenUtils_1.TonTokenUtils; static Wallet = ton_token_wallet_1.TonTokenWallet; constructor(_connection, data, options) { const isAddress = typeof data === 'string' || data instanceof ton_1.Address; if (!isAddress && !('address' in data)) { throw new Error('Address is not specified'); } const address = (0, utils_1.resolveTonAddress)(isAddress ? data : data.address); super(); this._connection = _connection; this.options = options; if (isAddress) { this.setData('address', address); } else { this.setData(() => ({ ...data, address, })); } (0, mobx_1.makeObservable)(this); } static async create(connection, data, options) { const { sync = true, ...restOptions } = { ...options }; const token = new TonToken(connection, data, restOptions); if (sync) { await token.sync({ force: false }); } return token; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); const state = await this._connection.getProviderState(); const details = await TonToken.Utils.getOffchainDetails(this.address.toString(), state.networkId); if (!details?.symbol || details.decimals == null) { (0, js_utils_1.throwException)('Token has invalid data and cannot be resolved'); } this.setData({ ...details }); } catch (e) { if (process.env.NODE_ENV !== 'production') { (0, js_core_1.syncErrorMessage)(this.constructor.name, this.address.toString(), e); } } finally { this.setState('isSyncing', false); } } async wallet(ownerAddress, options) { const address = await this.walletOf(ownerAddress); return TonToken.Wallet.create(this._connection, { address }, options); } async walletOf(ownerAddress) { const state = await (0, js_core_1.getFullContractState)(this._connection, this.address.toRawString()); return TonToken.Utils.walletOf(this._connection, { ownerAddress, tokenAddress: this.address, }, state); } async balance(ownerAddress) { const walletAddress = await this.walletOf(ownerAddress); return TonToken.Wallet.Utils.balance(this._connection, { walletAddress }); } get root() { return this._data.address.toString(); } 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 symbol() { return this._data.symbol; } get rootOwnerAddress() { return this._data.rootOwnerAddress; } get totalSupply() { return this._data.totalSupply; } get isSyncing() { return this._state.isSyncing; } get(key) { return this._data[key]; } /** * Returns copy of the current token */ clone() { return new TonToken(this._connection, this.toJSON()); } } exports.TonToken = TonToken; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "sync", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "wallet", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "walletOf", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "balance", null); __decorate([ mobx_1.computed, __metadata("design:type", String), __metadata("design:paramtypes", []) ], TonToken.prototype, "root", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "address", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "chainId", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "decimals", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "icon", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "name", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "symbol", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "rootOwnerAddress", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "totalSupply", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "isSyncing", 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) ], TonToken.prototype, "get", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", TonToken) ], TonToken.prototype, "clone", null);