UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

241 lines (240 loc) 10.5 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.DexGasValues = void 0; const js_utils_1 = require("@broxus/js-utils"); const bignumber_js_1 = require("bignumber.js"); const mobx_1 = require("mobx"); const core_1 = require("../../core"); const DexGasValuesUtils_1 = require("../../models/dex-gas-values/DexGasValuesUtils"); const utils_1 = require("../../utils"); /** * Calculate gas * @param {string} fixedValue * @param {string} dynamicGas * @param {number} [gasPrice] * @returns {string} */ function calcGas(fixedValue, dynamicGas, gasPrice = 1000) { return (0, utils_1.gasToValue)(fixedValue, (0, bignumber_js_1.BigNumber)(dynamicGas).plus(100_000).toFixed(), gasPrice); } class DexGasValues extends core_1.SmartContractModel { _connection; options; static Utils = DexGasValuesUtils_1.DexGasValuesUtils; /** * @param {ProviderRpcClient} _connection * Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address * DexGasValues root address * @param {Readonly<DexCtorOptions>} [options] * (optional) DexGasValues Smart Contract Model options */ constructor(_connection, address, options) { super(_connection, address); this._connection = _connection; this.options = options; (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 * DexGasValues root address * @param {Readonly<DexGasValuesCreateOptions>} [options] * (optional) DexGasValues Smart Contract Model options */ static async create(connection, address, options) { const { sync = true, watch, watchCallback, ...restOptions } = { ...options }; const dexGasValues = new DexGasValues(connection, address, restOptions); if (sync) { await dexGasValues.sync({ force: false }); } if (watch) { await dexGasValues.watch(watchCallback); } return dexGasValues; } async sync(options) { if (!options?.force && this.isSyncing) { return; } try { this.setState('isSyncing', !options?.silent); await this.syncContractState({ force: options?.force || !this.contractState }); if (!this.isDeployed) { (0, js_utils_1.throwException)('DexGasValues is not deployed'); } } 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 getDeployAccountGas() { const result = await DexGasValues.Utils.getDeployAccountGas(this._connection, this.address, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getDepositToAccountGas() { const result = await DexGasValues.Utils.getDepositToAccountGas(this._connection, this.address, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getAccountWithdrawGas(params) { const result = await DexGasValues.Utils.getAccountWithdrawGas(this._connection, this.address, params, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getAccountDepositGas(params) { const result = await DexGasValues.Utils.getAccountDepositGas(this._connection, this.address, params, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getAddPoolGas(params) { const result = await DexGasValues.Utils.getAddPoolGas(this._connection, this.address, params, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getDeployPoolGas(params) { const result = await DexGasValues.Utils.getDeployPoolGas(this._connection, this.address, params, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getPoolDirectNoFeeWithdrawGas(params) { const result = await DexGasValues.Utils.getPoolDirectNoFeeWithdrawGas(this._connection, this.address, params, this.contractState); const gasPrice = await this.getGasPrice(); return calcGas(result.fixedValue, result.dynamicGas, gasPrice); } async getGasPrice() { if (typeof this.options?.gasPrice === 'function') { return this.options.gasPrice(); } return this.options?.gasPrice ?? 1000; } } exports.DexGasValues = DexGasValues; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "sync", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "watch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "unwatch", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getDeployAccountGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getDepositToAccountGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getAccountWithdrawGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getAccountDepositGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getAddPoolGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getDeployPoolGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getPoolDirectNoFeeWithdrawGas", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], DexGasValues.prototype, "getGasPrice", null);