UNPKG

@broxus/js-bridge-essentials

Version:

Bridge JavaScript Essentials library

217 lines (216 loc) 6.76 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.EvmToken = void 0; const js_core_1 = require("@broxus/js-core"); const mobx_1 = require("mobx"); const EvmTokenUtils_1 = require("../../models/evm-token/EvmTokenUtils"); class EvmToken extends js_core_1.AbstractStore { _connection; _provider; static Utils = EvmTokenUtils_1.EvmTokenUtils; constructor(_connection, data, _provider) { super(); this._connection = _connection; this._provider = _provider; if (typeof data === 'string') { this.setData(() => ({ address: data, tags: new Set(), })); } else if (!('address' in data)) { throw new Error('Address is not specified'); } else if (data != null && typeof data === 'object' && !Array.isArray(data)) { this.setData(() => ({ ...data, tags: new Set(data.tags), })); } (0, mobx_1.makeObservable)(this); } static async create(connection, data, options, provider) { const { sync = true } = { ...options }; const token = new EvmToken(connection, data, provider); if (sync) { const details = await EvmToken.Utils.getDetails(connection, token.address); if (!details?.symbol || details.decimals == null) { throw new Error('Token cannot be resolved'); } token.setData({ ...details }); } return token; } async approve(params) { if (this._provider == null) { throw new Error('Provider is not specified'); } return EvmToken.Utils.approve(this._provider, { ...params, tokenAddress: this.address }); } async allowance(params) { return EvmToken.Utils.allowance(this._connection, { ...params, tokenAddress: this.address }); } /** @deprecated Use EvmToken.balance (this.balance) instead */ async getBalance(ownerAddress) { return this.balance(ownerAddress); } async balance(ownerAddress) { return EvmToken.Utils.balance(this._connection, this.address, ownerAddress); } /** * Returns token root address as instance of Address. * @returns {EvmTokenData['address']} */ 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 root() { return this._data.address.toString(); } /** * */ get symbol() { return this._data.symbol; } /** * */ get totalSupply() { return this._data.totalSupply; } /** * */ get vendor() { return this._data.vendor; } get(key) { return this._data[key]; } /** * Returns copy of the current token */ clone() { return new EvmToken(this._connection, this.toJSON()); } } exports.EvmToken = EvmToken; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], EvmToken.prototype, "approve", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], EvmToken.prototype, "allowance", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], EvmToken.prototype, "getBalance", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], EvmToken.prototype, "balance", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "address", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "chainId", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "decimals", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "icon", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "name", null); __decorate([ mobx_1.computed, __metadata("design:type", String), __metadata("design:paramtypes", []) ], EvmToken.prototype, "root", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "symbol", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "totalSupply", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], EvmToken.prototype, "vendor", 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) ], EvmToken.prototype, "get", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", EvmToken) ], EvmToken.prototype, "clone", null);