@broxus/js-core
Version:
MobX-based JavaScript Core library
145 lines (144 loc) • 5.34 kB
JavaScript
;
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.SmartContractModel = void 0;
const mobx_1 = require("mobx");
const AbstractStore_1 = require("../AbstractStore");
const utils_1 = require("../utils");
class SmartContractModel extends AbstractStore_1.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 = (0, utils_1.resolveTvmAddress)(address);
(0, mobx_1.makeObservable)(this);
}
get address() {
return this._address;
}
get computedStorageData() {
return (0, mobx_1.toJS)(this._data.computedStorageData);
}
/**
* **Get the full contract state.**
*
* This computed property returns the full contract state after synchronizing the contract state.
* @returns {SmartContractModelData["contractState"]}
*/
get contractState() {
return (0, mobx_1.toJS)(this._data.contractState);
}
get isSyncing() {
return this._state.isSyncing;
}
get syncedAt() {
return this._state.syncedAt;
}
/**
* **Check if the contract is deployed.**
*
* This computed property returns whether the contract is deployed on the blockchain.
* It checks the `isDeployed` property of the full contract state after synchronizing the contract state.
*
* @returns {boolean | undefined}
*/
get isDeployed() {
return this.contractState?.isDeployed;
}
/**
* **Synchronize full contract state.**
*
* This method fetches the full contract state from the blockchain.
*
* @param {SyncContractStateOptions} options
* @returns {Promise<FullContractState | undefined>}
*/
async syncContractState(options) {
try {
const state = await (0, utils_1.getFullContractState)(this._connection, this._address, { force: options?.force, ttl: options?.ttl });
this.setData('contractState', state);
this.setState('syncedAt', Date.now());
return state;
}
catch {
this.setData('contractState', undefined);
return undefined;
}
}
/**
* **Synchronize computed storage data.**
*
* This method fetches the computed storage data for the contract.
*
* @returns {Promise<void>}
* @protected
*/
async syncComputedStorageData() {
const state = await this.syncContractState({ force: !this.contractState });
if (state) {
const computedStorageData = await this._connection.computeStorageFee({ state });
this.setData('computedStorageData', computedStorageData);
}
}
}
exports.SmartContractModel = SmartContractModel;
__decorate([
mobx_1.computed,
__metadata("design:type", Function),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "address", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "computedStorageData", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "contractState", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "isSyncing", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "syncedAt", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SmartContractModel.prototype, "isDeployed", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], SmartContractModel.prototype, "syncContractState", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SmartContractModel.prototype, "syncComputedStorageData", null);