UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

129 lines (128 loc) 4.73 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.EvmTokenWallet = void 0; const js_core_1 = require("@broxus/js-core"); const js_utils_1 = require("@broxus/js-utils"); const mobx_1 = require("mobx"); const EvmToken_1 = require("../../models/evm-token/EvmToken"); class EvmTokenWallet extends js_core_1.AbstractStore { _connection; params; constructor(_connection, params) { super(); this._connection = _connection; this.params = params; this.setData(() => ({ ownerAddress: params.ownerAddress, tokenAddress: params.tokenAddress, })); (0, mobx_1.makeObservable)(this); } static async create(connection, params) { const { sync = true, watch, ...restParams } = params; const wallet = new EvmTokenWallet(connection, restParams); await Promise.allSettled([sync && wallet.sync(), watch && wallet.watch()]); return wallet; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); const balance = await EvmToken_1.EvmToken.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress); this.setData('balance', balance); } catch (e) { if (process.env.NODE_ENV !== 'production') { (0, js_core_1.syncErrorMessage)(this.constructor.name, '', e); } } finally { this.setState('isSyncing', false); } } async watch() { try { clearInterval(this.balanceCheckInterval); this.balanceCheckInterval = setInterval(async () => { const balance = await EvmToken_1.EvmToken.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress).catch(() => undefined); this.setData('balance', balance); }, this.params.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; } balanceCheckInterval; } exports.EvmTokenWallet = EvmTokenWallet; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], EvmTokenWallet.prototype, "sync", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], EvmTokenWallet.prototype, "watch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], EvmTokenWallet.prototype, "unwatch", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmTokenWallet.prototype, "balance", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmTokenWallet.prototype, "ownerAddress", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmTokenWallet.prototype, "tokenAddress", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmTokenWallet.prototype, "isSyncing", null);