UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

298 lines (297 loc) 11 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 { debounce, throwException } from '@broxus/js-utils'; import { action, computed, makeObservable } from 'mobx'; import { SmartContractModel } from '../../core'; import { GaugeAccountUtils, } from '../../models/gauge-account/GaugeAccountUtils'; import { contractStateChangeDebugMessage, isAddressesEquals, subscribeDebugMessage, syncErrorMessage, unsubscribeDebugMessage, unsubscribeErrorMessage, } from '../../utils'; export class GaugeAccount extends SmartContractModel { _connection; options; _provider; static Utils = GaugeAccountUtils; /** * @param {ProviderRpcClient} _connection Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address GaugeAccount root address * @param {Readonly<GaugeAccountCtorOptions>} [options] (optional) GaugeAccount Smart Contract Model options * @param {ProviderRpcClient} [_provider] (optional) RPC provider that require connection to the TVM wallet */ constructor(_connection, address, options, _provider) { super(_connection, address); this._connection = _connection; this.options = options; this._provider = _provider; makeObservable(this); } /** * @param {ProviderRpcClient} connection Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address GaugeAccount root address * @param {Readonly<GaugeAccountCreateOptions>} [options] (optional) GaugeAccount Smart Contract Model options * @param {ProviderRpcClient} [provider] (optional) RPC provider that require connection to the TVM wallet */ static async create(connection, address, options, provider) { const { sync = true, watch, ...restOptions } = { ...options }; const gaugeAccount = new GaugeAccount(connection, address, restOptions, provider); if (sync) { await gaugeAccount.sync({ force: false }); } if (watch) { await gaugeAccount.watch(); } return gaugeAccount; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); const state = await this.syncContractState({ force: options?.force || !this.contractState }); await this.syncComputedStorageData(); if (!this.isDeployed) { throwException('GaugeAccount is not deployed'); } const [averages, details, rewardDetails] = await Promise.all([ GaugeAccount.Utils.getAverages(this._connection, this.address, state), GaugeAccount.Utils.getDetails(this._connection, this.address, state), GaugeAccount.Utils.getRewardDetails(this._connection, this.address, state), ]); this.setData({ ...averages, ...details, ...rewardDetails }); } catch (e) { if (process.env.NODE_ENV !== 'production') { syncErrorMessage(this.constructor.name, this.address, e); } } finally { this.setState('isSyncing', false); } } async watch(callback) { try { this.contractSubscriber = new this._connection.Subscriber(); await this.contractSubscriber.states(this.address).delayed(stream => { if (process.env.NODE_ENV !== 'production') { subscribeDebugMessage(this.constructor.name, this.address); } return stream.on(debounce(async (event) => { if (process.env.NODE_ENV !== 'production') { contractStateChangeDebugMessage(this.constructor.name, this.address, event); } if (isAddressesEquals(event.address, this.address)) { await this.sync({ force: !this.isSyncing, silent: true }); callback?.(); return; } await this.unwatch(); }, this.options?.watchDebounceDelay ?? 3000)); }); return this.contractSubscriber; } catch (e) { await this.unwatch(); throw e; } } async unwatch() { try { await this.contractSubscriber?.unsubscribe(); this.contractSubscriber = undefined; if (process.env.NODE_ENV !== 'production') { unsubscribeDebugMessage(this.constructor.name, this.address); } } catch (e) { if (process.env.NODE_ENV !== 'production') { unsubscribeErrorMessage(this.constructor.name, this.address, e); } } } async calculateMinGas() { return GaugeAccount.Utils.calculateMinGas(this._connection, this.address, this.contractState); } async pendingReward(params) { return GaugeAccount.Utils.pendingReward(this._connection, this.address, params, this.contractState); } get balance() { return this._data.balance; } get curAverageState() { return this._data.curAverageState; } get currentVersion() { return this._data.currentVersion; } get extraReward() { return this._data.extraReward; } get extraVesting() { return this._data.extraVesting; } get gauge() { return this._data.gauge; } get lastAverageState() { return this._data.lastAverageState; } get lastUpdateTime() { return this._data.lastUpdateTime; } get lockedBalance() { return this._data.lockedBalance; } get lockBoostedBalance() { return this._data.lockBoostedBalance; } get lockedDepositsNum() { return this._data.lockedDepositsNum; } get qubeReward() { return this._data.qubeReward; } get qubeVesting() { return this._data.qubeVesting; } get totalBoostedBalance() { return this._data.totalBoostedBalance; } get user() { return this._data.user; } get veAccount() { return this._data.veAccount; } get veBoostedBalance() { return this._data.veBoostedBalance; } get voteEscrow() { return this._data.voteEscrow; } } __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], GaugeAccount.prototype, "sync", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], GaugeAccount.prototype, "watch", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], GaugeAccount.prototype, "unwatch", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], GaugeAccount.prototype, "calculateMinGas", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], GaugeAccount.prototype, "pendingReward", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "balance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "curAverageState", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "currentVersion", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "extraReward", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "extraVesting", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "gauge", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "lastAverageState", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "lastUpdateTime", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "lockedBalance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "lockBoostedBalance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "lockedDepositsNum", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "qubeReward", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "qubeVesting", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "totalBoostedBalance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "user", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "veAccount", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "veBoostedBalance", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], GaugeAccount.prototype, "voteEscrow", null);