UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

361 lines (360 loc) 13.2 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 { GaugeUtils, } from '../../models/gauge/GaugeUtils'; import { areAddressesEqual, contractStateChangeDebugMessage, subscribeDebugMessage, syncErrorMessage, unsubscribeDebugMessage, unsubscribeErrorMessage, } from '../../utils'; export class Gauge extends SmartContractModel { _connection; options; _provider; static Utils = GaugeUtils; /** * @param {ProviderRpcClient} _connection * Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address * Gauge root address * @param {Readonly<GaugeCtorOptions>} [options] * (optional) Gauge 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 * Gauge root address * @param {Readonly<GaugeCreateOptions>} [options] * (optional) Gauge 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, watchCallback, ...restOptions } = { ...options }; const gauge = new Gauge(connection, address, restOptions, provider); if (sync) { await gauge.sync({ force: false }); } if (watch) { await gauge.watch(watchCallback); } return gauge; } 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 }); if (!this.isDeployed) { throwException('Gauge is not deployed'); } const [details, rewardDetails, tokenDetails] = await Promise.all([ Gauge.Utils.getDetails(this._connection, this.address, state), Gauge.Utils.getRewardDetails(this._connection, this.address, state), Gauge.Utils.getTokenDetails(this._connection, this.address, state), ]); this.setData({ ...details, ...rewardDetails, ...tokenDetails }); } catch (e) { if (process.env.NODE_ENV !== 'production') { const state = await this._connection.getProviderState(); syncErrorMessage(this.constructor.name, this.address, e, state.networkId.toString()); } } 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') { const state = await this._connection.getProviderState(); contractStateChangeDebugMessage(this.constructor.name, this.address, event, state.networkId.toString()); } if (areAddressesEqual(event.address, this.address)) { await this.sync({ force: !this.isSyncing, silent: true }); callback?.(...this.toJSON(true)); 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') { const state = await this._connection.getProviderState(); unsubscribeErrorMessage(this.constructor.name, this.address, e, state.networkId.toString()); } } } // todo: move to sync async calcSyncData() { return Gauge.Utils.calcSyncData(this._connection, this.address, this.contractState); } // todo: move to sync async calculateRewardData() { return Gauge.Utils.calculateRewardData(this._connection, this.address, this.contractState); } // todo: move to sync async calculateSupplyAverage() { return Gauge.Utils.calculateSupplyAverage(this._connection, this.address, this.contractState); } async calculateBoostedAmount(params) { return Gauge.Utils.calculateBoostedAmount(this._connection, this.address, params, this.contractState); } async getGaugeAccountAddress(userAddress) { return Gauge.Utils.getGaugeAccountAddress(this._connection, this.address, userAddress, this.contractState); } async getVoteEscrowAccountAddress(userAddress) { return Gauge.Utils.getVoteEscrowAccountAddress(this._connection, this.address, userAddress, this.contractState); } get depositTokenData() { return this._data.depositTokenData; } get extraRewardEnded() { return this._data.extraRewardEnded; } get extraRewardRounds() { return this._data.extraRewardRounds; } get extraTokenData() { return this._data.extraTokenData; } get extraVestingPeriods() { return this._data.extraVestingPeriods; } get extraVestingRatios() { return this._data.extraVestingRatios; } get initialized() { return this._data.initialized; } get lastAverageUpdateTime() { return this._data.lastAverageUpdateTime; } get lastExtraRewardRoundIdx() { return this._data.lastExtraRewardRoundIdx; } get lastQubeRewardRoundIdx() { return this._data.lastQubeRewardRoundIdx; } get lastRewardTime() { return this._data.lastRewardTime; } get lockBoostedSupply() { return this._data.lockBoostedSupply; } get maxBoost() { return this._data.maxBoost; } get maxLockTime() { return this._data.maxLockTime; } get owner() { return this._data.owner; } get qubeRewardRounds() { return this._data.qubeRewardRounds; } get qubeTokenData() { return this._data.qubeTokenData; } get qubeVestingPeriod() { return this._data.qubeVestingPeriod; } get qubeVestingRatio() { return this._data.qubeVestingRatio; } get totalBoostedSupply() { return this._data.totalBoostedSupply; } get voteEscrow() { return this._data.voteEscrow; } get withdrawAllLockPeriod() { return this._data.withdrawAllLockPeriod; } } __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], Gauge.prototype, "sync", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], Gauge.prototype, "watch", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], Gauge.prototype, "unwatch", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], Gauge.prototype, "calculateBoostedAmount", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], Gauge.prototype, "getGaugeAccountAddress", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], Gauge.prototype, "getVoteEscrowAccountAddress", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "depositTokenData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "extraRewardEnded", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "extraRewardRounds", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "extraTokenData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "extraVestingPeriods", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "extraVestingRatios", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "initialized", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "lastAverageUpdateTime", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "lastExtraRewardRoundIdx", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "lastQubeRewardRoundIdx", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "lastRewardTime", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "lockBoostedSupply", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "maxBoost", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "maxLockTime", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "owner", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "qubeRewardRounds", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "qubeTokenData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "qubeVestingPeriod", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "qubeVestingRatio", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "totalBoostedSupply", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "voteEscrow", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], Gauge.prototype, "withdrawAllLockPeriod", null);