@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
252 lines (251 loc) • 9.88 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 { SmartContractModel, contractStateChangeDebugMessage, errorLabelStyle, inheritTextStyle, isAddressesEquals, subscribeDebugMessage, unsubscribeDebugMessage, unsubscribeErrorMessage, } from '@broxus/js-core';
import { debounce, debug, groupCollapsed, groupEnd, throwException } from '@broxus/js-utils';
import { action, computed, makeObservable } from 'mobx';
import { TvmEvmBaseEventUtils, } from '../../models/tvm-evm-base-event/TvmEvmBaseEventUtils';
export class TvmEvmBaseEvent extends SmartContractModel {
_connection;
options;
_provider;
static Utils = TvmEvmBaseEventUtils;
/**
* @param {ProviderRpcClient} _connection Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address Transfer Event root address
* @param {Readonly<TvmEvmBaseEventCtorOptions>} [options] (optional) Transfer Event ABI Wrapper options
* @param {ProviderRpcClient} [_provider] (optional) RPC provider that require connection to the TVM wallet
*/
constructor(_connection, address, options, _provider) {
super(_connection, address);
this._connection = _connection;
this.options = options;
this._provider = _provider;
makeObservable(this);
}
/**
* @param {ProviderRpcClient} connection Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address Transfer Event root address
* @param {Readonly<TvmEvmBaseEventCreateOptions>} [options] (optional) Transfer Event ABI Wrapper options
* @param {ProviderRpcClient} [provider] (optional) RPC provider that require connection to the TVM wallet
*/
static async create(connection, address, options, provider) {
const { sync = true, watch, watchCallback, ...restOptions } = { ...options };
const event = new TvmEvmBaseEvent(connection, address, restOptions, provider);
if (sync) {
await event.sync({ force: false });
}
if (watch) {
await event.watch(watchCallback);
}
return event;
}
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('TokenTransferTvmEvmEvent is not deployed');
}
const [details] = await Promise.all([
TvmEvmBaseEvent.Utils.getDetails(this._connection, this.address, state),
]);
this.setData({ ...details });
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
groupCollapsed(`%c${this.constructor.name}%c Sync failed with an error`, errorLabelStyle, inheritTextStyle);
debug(`Code: ${e.code}`);
debug(`Message: ${e.message}`);
groupEnd();
}
}
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: true, silent: true });
callback?.();
return;
}
await this.unwatch();
}, this.options?.watchDebounceDelay ?? 3000));
});
return this.contractSubscriber;
}
catch (e) {
await this.unwatch();
throw e;
}
}
async unwatch() {
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 relayRound() {
return TvmEvmBaseEvent.Utils.relayRound(this._connection, this.address, this.contractState);
}
async roundNumber() {
return TvmEvmBaseEvent.Utils.roundNumber(this._connection, this.address, this.contractState);
}
get balance() {
return this._data.balance;
}
get confirms() {
return this._data.confirms;
}
get empty() {
return this._data.empty;
}
get eventInitData() {
return this._data.eventInitData;
}
get initializer() {
return this._data.initializer;
}
get meta() {
return this._data.meta;
}
get rejects() {
return this._data.rejects;
}
get requiredVotes() {
return this._data.requiredVotes;
}
get status() {
return this._data.status;
}
decodeEvent(args) {
return TvmEvmBaseEvent.Utils.decodeEvent(this._connection, this.address, args);
}
decodeTransaction(args) {
return TvmEvmBaseEvent.Utils.decodeTransaction(this._connection, this.address, args);
}
decodeTransactionEvents(transaction) {
return TvmEvmBaseEvent.Utils.decodeTransactionEvents(this._connection, this.address, transaction);
}
}
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "sync", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "watch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "unwatch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "relayRound", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "roundNumber", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "balance", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "confirms", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "empty", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "eventInitData", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "initializer", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "meta", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "rejects", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "requiredVotes", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmEvmBaseEvent.prototype, "status", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "decodeEvent", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "decodeTransaction", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmEvmBaseEvent.prototype, "decodeTransactionEvents", null);