UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

167 lines (166 loc) 6.04 kB
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); }; import { AbstractStore, errorLabelStyle, inheritTextStyle, } from '@broxus/js-core'; import { debug, error, groupCollapsed, groupEnd } from '@broxus/js-utils'; import { action, computed, makeObservable } from 'mobx'; import { TonTokenWalletUtils } from '../../models/ton-token-wallet/TonTokenWalletUtils'; import { TonTokenUtils } from '../../models/ton-token/TonTokenUtils'; import { resolveTonAddress } from '../../utils'; export class TonTokenWallet extends AbstractStore { _connection; options; static Utils = TonTokenWalletUtils; _address; constructor(_connection, address, options) { super(); this._connection = _connection; this.options = options; this._address = resolveTonAddress(address); 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.walletOf(connection, { ownerAddress: config.ownerAddress, tokenAddress: config.tokenAddress, }); } const wallet = new TonTokenWallet(connection, address, restOptions); if ('ownerAddress' in config) { wallet.setData({ ownerAddress: resolveTonAddress(config.ownerAddress) }); } if ('tokenAddress' in config) { wallet.setData({ tokenAddress: 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') { groupCollapsed(`%c${this.constructor.name}%c Sync has been ended with error`, errorLabelStyle, inheritTextStyle); debug(`Code: ${e.code}`); debug(`Message: ${e.message}`); 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) { error(e); await this.unwatch(); } } async unwatch() { try { clearInterval(this.balanceCheckInterval); this.balanceCheckInterval = undefined; } catch (e) { 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; } __decorate([ computed, __metadata("design:type", Function), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "address", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "sync", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "watch", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TonTokenWallet.prototype, "unwatch", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "balance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "ownerAddress", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "tokenAddress", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "isSyncing", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonTokenWallet.prototype, "syncedAt", null);