UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

343 lines (342 loc) 13 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShardChainConfig = void 0; const js_utils_1 = require("@broxus/js-utils"); const mobx_1 = require("mobx"); const constants_1 = require("../../constants"); const core_1 = require("../../core"); const constants_2 = require("../../models/network-config/constants"); const utils_1 = require("../../utils"); class ShardChainConfig extends core_1.SmartContractModel { _connection; options; _contract; /** * @param {ProviderRpcClient} _connection * Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address * ShardChainConfig root address * @param {Readonly<ShardChainConfigCtorOptions>} [options] * (optional) ShardChainConfig Smart Contract Model options */ constructor(_connection, address, options) { super(_connection, address); this._connection = _connection; this.options = options; this._contract = new this._connection.Contract(constants_2.CONFIG_ABI, this.address); (0, mobx_1.makeObservable)(this); } /** * @param {ProviderRpcClient} connection * Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address * ShardChainConfig root address * @param {Readonly<ShardChainCreateOptions>} [options] * (optional) ShardChainConfig Smart Contract Model options */ static async create(connection, address, options) { const { sync = true, watch, watchCallback, ...restOptions } = { ...options }; const config = new ShardChainConfig(connection, address, restOptions); if (sync) { await config.sync({ force: false, ttl: options?.ttl }); } if (watch) { await config.watch(watchCallback); } return config; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); await this.syncContractState({ force: options?.force || !this.contractState, ttl: options?.ttl || constants_1.SECONDS_IN_HOUR * 1000, }); if (!this.isDeployed) { (0, js_utils_1.throwException)(`${this.constructor.name} is not deployed`); } await this.syncParams(); } catch (e) { if (process.env.NODE_ENV !== 'production') { const state = await this._connection.getProviderState(); (0, utils_1.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') { (0, utils_1.subscribeDebugMessage)(this.constructor.name, this.address); } return stream.on((0, js_utils_1.debounce)(async (event) => { if (process.env.NODE_ENV !== 'production') { const state = await this._connection.getProviderState(); (0, utils_1.contractStateChangeDebugMessage)(this.constructor.name, this.address, event, state.networkId.toString()); } if ((0, utils_1.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() { if (this.contractSubscriber === undefined) { return; } try { await this.contractSubscriber?.unsubscribe(); this.contractSubscriber = undefined; if (process.env.NODE_ENV !== 'production') { (0, utils_1.unsubscribeDebugMessage)(this.constructor.name, this.address); } } catch (e) { if (process.env.NODE_ENV !== 'production') { const state = await this._connection.getProviderState(); (0, utils_1.unsubscribeErrorMessage)(this.constructor.name, this.address, e, state.networkId.toString()); } } } async syncParams() { const { fields } = await this._contract.getFields({ allowPartial: true, cachedState: this.contractState }); if (fields == null) { throw new Error('Config contract state not found'); } const { boc: nonEmptyMap } = await this._connection.packIntoCell({ abiVersion: '2.2', data: { flag: true, root: fields.paramsRoot, }, structure: [ { name: 'flag', type: 'bool' }, { name: 'root', type: 'cell' }, ], }); const { data: { params: rawParams }, } = await this._connection.unpackFromCell({ abiVersion: '2.2', allowPartial: true, boc: nonEmptyMap, structure: [{ name: 'params', type: 'map(uint32,cell)' }], }); const params = new Map(); for (const [id, value] of rawParams) { params.set(parseInt(id, 10), value); } // Base workchain gas limits and prices. // https://github.com/broxus/tycho-types/blob/master/src/models/config/mod.rs#L1046 const param21 = params.get(21); // Message forwarding prices for base workchain. // https://github.com/broxus/tycho-types/blob/master/src/models/config/mod.rs#L1064 const param25 = params.get(25); if (!param21) { throw new Error('Param (21) state not found'); } if (!param25) { throw new Error('Param (25) state not found'); } const [data21, data25] = await Promise.all([ this._connection.unpackFromCell({ abiVersion: '2.2', allowPartial: true, boc: param21, structure: constants_2.PRICES_PARAM_ABI, }), this._connection.unpackFromCell({ abiVersion: '2.2', allowPartial: true, boc: param25, structure: constants_2.FORWARD_FEE_PRICES_PARAM_ABI, }), ]); this.setData({ bitPrice: data25.data.value.bitPrice, blockGasLimit: data21.data.value.blockGasLimit, cellPrice: data25.data.value.cellPrice, deleteDueLimit: data21.data.value.deleteDueLimit, firstFrac: data25.data.value.firstFrac, flatGasLimit: data21.data.value.flatGasLimit, flatGasPrice: data21.data.value.flatGasPrice, freezeDueLimit: data21.data.value.freezeDueLimit, gasCredit: data21.data.value.gasCredit, gasLimit: data21.data.value.gasLimit, gasPrice: data21.data.value.gasPrice, ihrPriceFactor: data25.data.value.ihrPriceFactor, lumpPrice: data25.data.value.lumpPrice, nextFrac: data25.data.value.nextFrac, specialGasLimit: data21.data.value.specialGasLimit, }); } get bitPrice() { return this._data.bitPrice; } get blockGasLimit() { return this._data.blockGasLimit; } get cellPrice() { return this._data.cellPrice; } get deleteDueLimit() { return this._data.deleteDueLimit; } get firstFrac() { return this._data.firstFrac; } get flatGasLimit() { return this._data.flatGasLimit; } get flatGasPrice() { return this._data.flatGasPrice; } get freezeDueLimit() { return this._data.freezeDueLimit; } get gasCredit() { return this._data.gasCredit; } get gasLimit() { return this._data.gasLimit; } get gasPrice() { return this._data.gasPrice; } get ihrPriceFactor() { return this._data.ihrPriceFactor; } get lumpPrice() { return this._data.lumpPrice; } get nextFrac() { return this._data.nextFrac; } get specialGasLimit() { return this._data.specialGasLimit; } } exports.ShardChainConfig = ShardChainConfig; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], ShardChainConfig.prototype, "sync", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], ShardChainConfig.prototype, "watch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ShardChainConfig.prototype, "unwatch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], ShardChainConfig.prototype, "syncParams", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "bitPrice", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "blockGasLimit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "cellPrice", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "deleteDueLimit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "firstFrac", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "flatGasLimit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "flatGasPrice", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "freezeDueLimit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "gasCredit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "gasLimit", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "gasPrice", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "ihrPriceFactor", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "lumpPrice", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "nextFrac", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ShardChainConfig.prototype, "specialGasLimit", null);