UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

171 lines (170 loc) 6.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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TonTokenWallet = void 0; const js_core_1 = require("@broxus/js-core"); const js_utils_1 = require("@broxus/js-utils"); const mobx_1 = require("mobx"); const TonTokenWalletUtils_1 = require("../../models/ton-token-wallet/TonTokenWalletUtils"); const TonTokenUtils_1 = require("../../models/ton-token/TonTokenUtils"); const utils_1 = require("../../utils"); class TonTokenWallet extends js_core_1.AbstractStore { _connection; options; static Utils = TonTokenWalletUtils_1.TonTokenWalletUtils; _address; constructor(_connection, address, options) { super(); this._connection = _connection; this.options = options; this._address = (0, utils_1.resolveTonAddress)(address); (0, mobx_1.makeObservable)(this); } static async create(connection, config, options) { const { sync = true, watch, watchCallback, ...restOptions } = { ...options }; let address; if ('address' in config) { address = config.address; } else { address = await TonTokenUtils_1.TonTokenUtils.walletOf(connection, { ownerAddress: config.ownerAddress, tokenAddress: config.tokenAddress, }); } const wallet = new TonTokenWallet(connection, address, restOptions); if ('ownerAddress' in config) { wallet.setData({ ownerAddress: (0, utils_1.resolveTonAddress)(config.ownerAddress) }); } if ('tokenAddress' in config) { wallet.setData({ tokenAddress: (0, utils_1.resolveTonAddress)(config.tokenAddress) }); } if (sync) { await wallet.sync({ force: false }); } if (watch) { await wallet.watch(watchCallback); } return wallet; } get address() { return this._address; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); const balance = await TonTokenWallet.Utils.balance(this._connection, { walletAddress: this.address }); this.setData({ balance }); } catch (e) { if (process.env.NODE_ENV !== 'production') { (0, js_utils_1.groupCollapsed)(`%c${this.constructor.name}%c Sync has been ended with error`, js_core_1.errorLabelStyle, js_core_1.inheritTextStyle); (0, js_utils_1.debug)(`Code: ${e.code}`); (0, js_utils_1.debug)(`Message: ${e.message}`); (0, js_utils_1.groupEnd)(); } } finally { this.setState('isSyncing', false); } } async watch(callback) { try { clearInterval(this.balanceCheckInterval); this.balanceCheckInterval = setInterval(async () => { const balance = await TonTokenWallet.Utils.balance(this._connection, { walletAddress: this.address }).catch(() => undefined); this.setData('balance', balance); callback?.(...this.toJSON(true)); }, this.options?.watchDebounceDelay ?? 15000); } catch (e) { (0, js_utils_1.error)(e); await this.unwatch(); } } async unwatch() { try { clearInterval(this.balanceCheckInterval); this.balanceCheckInterval = undefined; } catch (e) { (0, js_utils_1.error)(e); } } get balance() { return this._data.balance; } get ownerAddress() { return this._data.ownerAddress; } get tokenAddress() { return this._data.tokenAddress; } get isSyncing() { return this._state.isSyncing; } get syncedAt() { return this._state.syncedAt; } balanceCheckInterval; } exports.TonTokenWallet = TonTokenWallet; __decorate([ mobx_1.computed, __metadata("design:type", Function), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "address", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "sync", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "watch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "unwatch", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "balance", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "ownerAddress", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "tokenAddress", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "isSyncing", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "syncedAt", null);