UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

109 lines (108 loc) 4.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 { action, computed, makeObservable, toJS } from 'mobx'; import { AbstractStore } from '../AbstractStore'; import { getFullContractState, resolveTvmAddress } from '../utils'; export class SmartContractModel extends AbstractStore { _connection; _address; contractSubscriber; /** * @param {ProviderRpcClient} _connection Standalone RPC client that doesn't require connection to the TVM wallet provider * @param {Address | string} address Contract address * @protected */ constructor(_connection, address) { super(); this._connection = _connection; this.contractSubscriber = undefined; this._address = resolveTvmAddress(address); makeObservable(this); } get address() { return this._address; } get computedStorageData() { return toJS(this._data.computedStorageData); } get contractState() { return toJS(this._data.contractState); } get isSyncing() { return this._state.isSyncing; } get syncedAt() { return this._state.syncedAt; } get isDeployed() { return this.contractState?.isDeployed; } async syncContractState(options) { try { const state = await getFullContractState(this._connection, this._address, { force: options?.force, ttl: options?.ttl }); this.setData('contractState', state); this.setState('syncedAt', Date.now()); return state; } catch (e) { this.setData('contractState', undefined); return undefined; } } async syncComputedStorageData() { const state = await this.syncContractState({ force: !this.contractState }); if (state) { const computedStorageData = await this._connection.computeStorageFee({ state }); this.setData('computedStorageData', computedStorageData); } } } __decorate([ computed, __metadata("design:type", Function), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "address", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "computedStorageData", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "contractState", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "isSyncing", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "syncedAt", null); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], SmartContractModel.prototype, "isDeployed", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], SmartContractModel.prototype, "syncContractState", null); __decorate([ action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], SmartContractModel.prototype, "syncComputedStorageData", null);