UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

214 lines (213 loc) 7.35 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); }; var _a; import { AbstractStore, getFullContractState, syncErrorMessage, } from '@broxus/js-core'; import { throwException } from '@broxus/js-utils'; import { Address } from '@ton/ton'; import { action, computed, makeObservable } from 'mobx'; import { TonTokenUtils } from '../../models/ton-token/TonTokenUtils'; import { TonTokenWallet } from '../../models/ton-token-wallet'; import { resolveTonAddress } from '../../utils'; export class TonToken extends AbstractStore { _connection; options; static Utils = TonTokenUtils; static Wallet = TonTokenWallet; constructor(_connection, data, options) { const isAddress = typeof data === 'string' || data instanceof Address; if (!isAddress && !('address' in data)) { throw new Error('Address is not specified'); } const address = resolveTonAddress(isAddress ? data : data.address); super(); this._connection = _connection; this.options = options; if (isAddress) { this.setData('address', address); } else { this.setData(() => ({ ...data, address, })); } 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) { throwException('Token has invalid data and cannot be resolved'); } this.setData({ ...details }); } catch (e) { if (process.env.NODE_ENV !== 'production') { 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 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()); } } __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "sync", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "wallet", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "walletOf", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TonToken.prototype, "balance", null); __decorate([ computed, __metadata("design:type", String), __metadata("design:paramtypes", []) ], TonToken.prototype, "root", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "address", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "chainId", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "decimals", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "icon", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "name", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "symbol", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "rootOwnerAddress", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "totalSupply", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TonToken.prototype, "isSyncing", null); __decorate([ 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([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", TonToken) ], TonToken.prototype, "clone", null);