@broxus/js-core
Version:
MobX-based JavaScript Core library
251 lines (250 loc) • 10.5 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);
};
import { debounce, debug, throwException } from '@broxus/js-utils';
import { action, computed, makeObservable } from 'mobx';
import { inheritTextStyle, successLabelStyle, successTextStyle, warningLabelStyle } from '../../console';
import { SmartContractModel } from '../../core';
import { VoteEscrowDaoRootUtils, } from '../../models/vote-escrow-dao-root/VoteEscrowDaoRootUtils';
import { ProviderNotDefinedError, contractStateChangeDebugMessage, getRandomInt, isAddressesEquals, subscribeDebugMessage, syncErrorMessage, unsubscribeDebugMessage, unsubscribeErrorMessage, } from '../../utils';
export class VoteEscrowDaoRoot extends SmartContractModel {
_connection;
options;
_provider;
static Utils = VoteEscrowDaoRootUtils;
constructor(
/** Standalone RPC client that doesn't require connection to the TVM wallet provider */
_connection,
/** VoteEscrowDaoRoot root address */
address,
/** VoteEscrowDaoRoot Smart Contract Model options */
options,
/** RPC provider that require connection to the TVM wallet */
_provider) {
super(_connection, address);
this._connection = _connection;
this.options = options;
this._provider = _provider;
makeObservable(this);
}
static async create(connection, address, options, provider) {
const { sync = true, watch, watchCallback, ...restOptions } = { ...options };
const dao = new VoteEscrowDaoRoot(connection, address, restOptions, provider);
if (sync) {
await dao.sync({ force: false });
}
if (watch) {
await dao.watch(watchCallback);
}
return dao;
}
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('VoteEscrowDaoRoot is not deployed');
}
const [proposalConfiguration, voteEscrowRoot] = await Promise.all([
VoteEscrowDaoRoot.Utils.proposalConfiguration(this._connection, this.address, state),
VoteEscrowDaoRoot.Utils.getVoteEscrowRoot(this._connection, this.address, state),
]);
this.setData({ proposalConfiguration, voteEscrowRoot });
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
syncErrorMessage(this.constructor.name, this.address, e);
}
}
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') {
contractStateChangeDebugMessage(this.constructor.name, this.address, event);
}
if (isAddressesEquals(event.address, this.address)) {
await this.sync({ force: !this.isSyncing, silent: true });
callback?.();
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') {
unsubscribeDebugMessage(this.constructor.name, this.address);
}
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
unsubscribeErrorMessage(this.constructor.name, this.address, e);
}
}
}
async propose(params, args) {
if (!this._provider) {
throw new ProviderNotDefinedError(this.constructor.name);
}
const callId = params.callId ?? getRandomInt();
const subscriber = new this._connection.Subscriber();
let transaction;
try {
const message = await VoteEscrowDaoRoot.Utils.propose(this._provider, this.address, {
description: params.description,
evmActions: params.evmActions ?? [],
tvmActions: params.tvmActions ?? [],
}, args);
await params.onSend?.(message, { callId });
transaction = await message.transaction;
await params.onTransactionSent?.({ callId, transaction });
const stream = await subscriber
.trace(transaction)
.filterMap(async (tx) => {
if (!isAddressesEquals(tx.account, this.address)) {
return undefined;
}
const events = await this.decodeTransactionEvents(tx);
if (events.length === 0) {
return undefined;
}
const event = events.find(e => e.event === 'ProposalCreated');
if (!event) {
return undefined;
}
if (process.env.NODE_ENV !== 'production') {
debug(`%c${this.constructor.name}%c %cProposalCreated%c event was captured`, successLabelStyle, inheritTextStyle, successTextStyle, inheritTextStyle, event);
}
await params.onTransactionSuccess?.({
callId,
input: event.data,
transaction: tx,
});
return event;
})
.delayed(s => s.first());
await stream();
return transaction;
}
catch (e) {
params.onTransactionFailure?.({
callId,
error: e,
transaction,
});
throw e;
}
finally {
if (process.env.NODE_ENV !== 'production') {
debug(`%c${this.constructor.name}%c Unsubscribed from the proposal creating stream`, warningLabelStyle, inheritTextStyle);
}
await subscriber?.unsubscribe();
}
}
get proposalConfiguration() {
return this._data.proposalConfiguration;
}
get voteEscrowRoot() {
return this._data.voteEscrowRoot;
}
async calcTonActionsValue(args) {
return VoteEscrowDaoRoot.Utils.calcTvmActionsValue(this._connection, this.address, args, this.contractState);
}
async expectedProposalAddress(proposalId) {
return VoteEscrowDaoRoot.Utils.expectedProposalAddress(this._connection, this.address, proposalId, this.contractState);
}
decodeTransaction(args) {
return VoteEscrowDaoRoot.Utils.decodeTransaction(this._connection, this.address, args);
}
decodeTransactionEvents(transaction) {
return VoteEscrowDaoRoot.Utils.decodeTransactionEvents(this._connection, this.address, transaction);
}
}
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "sync", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "watch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "unwatch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "propose", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], VoteEscrowDaoRoot.prototype, "proposalConfiguration", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], VoteEscrowDaoRoot.prototype, "voteEscrowRoot", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "calcTonActionsValue", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "expectedProposalAddress", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "decodeTransaction", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VoteEscrowDaoRoot.prototype, "decodeTransactionEvents", null);