@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
337 lines (336 loc) • 13.7 kB
JavaScript
"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.TvmTvmEventNative = void 0;
const js_core_1 = require("@broxus/js-core");
const js_utils_1 = require("@broxus/js-utils");
const mobx_1 = require("mobx");
const TvmTvmEventNativeUtils_1 = require("../../models/tvm-tvm-event-native/TvmTvmEventNativeUtils");
class TvmTvmEventNative extends js_core_1.SmartContractModel {
_connection;
options;
_provider;
static Utils = TvmTvmEventNativeUtils_1.TvmTvmEventNativeUtils;
/**
* @param {ProviderRpcClient} _connection Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address Native Event root address
* @param {Readonly<TvmTvmEventNativeCtorOptions>} [options] (optional) Native 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;
(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 Native Event root address
* @param {Readonly<TvmTvmEventNativeCreateOptions>} [options] (optional) Native 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 TvmTvmEventNative(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) {
(0, js_utils_1.throwException)('Tvm Tvm Event Native is not deployed');
}
const [decodedData, details, eventInitialBalance, eventTokenWalletAddress, limitApproverAddress, bounty, recipient, sender,] = await Promise.all([
TvmTvmEventNative.Utils.getDecodedData(this._connection, this.address, state),
TvmTvmEventNative.Utils.getDetails(this._connection, this.address, state),
TvmTvmEventNative.Utils.eventInitialBalance(this._connection, this.address, state),
TvmTvmEventNative.Utils.eventTokenWallet(this._connection, this.address, state),
TvmTvmEventNative.Utils.limitApprover(this._connection, this.address, state),
TvmTvmEventNative.Utils.bounty(this._connection, this.address, state),
TvmTvmEventNative.Utils.recipient(this._connection, this.address, state),
TvmTvmEventNative.Utils.sender(this._connection, this.address, state),
]);
this.setData({
bounty,
decodedData,
...details,
eventInitialBalance,
eventTokenWalletAddress,
limitApproverAddress,
recipient,
sender,
});
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
(0, js_core_1.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') {
(0, js_core_1.subscribeDebugMessage)(this.constructor.name, this.address);
}
return stream.on((0, js_utils_1.debounce)(async (event) => {
if (process.env.NODE_ENV !== 'production') {
(0, js_core_1.contractStateChangeDebugMessage)(this.constructor.name, this.address, event);
}
if ((0, js_core_1.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() {
try {
await this.contractSubscriber?.unsubscribe();
this.contractSubscriber = undefined;
if (process.env.NODE_ENV !== 'production') {
(0, js_core_1.unsubscribeDebugMessage)(this.constructor.name, this.address);
}
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
(0, js_core_1.unsubscribeErrorMessage)(this.constructor.name, this.address, e);
}
}
}
approveLimit(args) {
if (!this._provider) {
throw new js_core_1.ProviderNotDefinedError(this.constructor.name);
}
return TvmTvmEventNative.Utils.approveLimit(this._provider, this.address, args);
}
cancel(params, args) {
if (!this._provider) {
throw new js_core_1.ProviderNotDefinedError(this.constructor.name);
}
return TvmTvmEventNative.Utils.cancel(this._provider, this.address, params, args);
}
rejectLimit(expectedGasReceiver, args) {
if (!this._provider) {
throw new js_core_1.ProviderNotDefinedError(this.constructor.name);
}
return TvmTvmEventNative.Utils.rejectLimit(this._provider, this.address, expectedGasReceiver, args);
}
retry(args) {
if (!this._provider) {
throw new js_core_1.ProviderNotDefinedError(this.constructor.name);
}
return TvmTvmEventNative.Utils.retry(this._provider, this.address, args);
}
setBounty(bounty, args) {
if (!this._provider) {
throw new js_core_1.ProviderNotDefinedError(this.constructor.name);
}
return TvmTvmEventNative.Utils.setBounty(this._provider, this.address, bounty, args);
}
async expectedGas() {
return TvmTvmEventNative.Utils.expectedGas(this._connection, this.address, this.contractState);
}
async nonce() {
return TvmTvmEventNative.Utils.nonce(this._connection, this.address, this.contractState);
}
get bounty() {
return this._data.bounty;
}
get decodedData() {
return this._data.decodedData;
}
get eventInitData() {
return this._data.eventInitData;
}
get eventTokenWalletAddress() {
return this._data.eventTokenWalletAddress;
}
get initializer() {
return this._data.initializer;
}
get limitApproverAddress() {
return this._data.limitApproverAddress;
}
get meta() {
return this._data.meta;
}
get recipient() {
return this._data.recipient;
}
get sender() {
return this._data.sender;
}
get status() {
return this._data.status;
}
decodeEvent(args) {
return TvmTvmEventNative.Utils.decodeEvent(this._connection, this.address, args);
}
decodeTransaction(args) {
return TvmTvmEventNative.Utils.decodeTransaction(this._connection, this.address, args);
}
decodeTransactionEvents(transaction) {
return TvmTvmEventNative.Utils.decodeTransactionEvents(this._connection, this.address, transaction);
}
}
exports.TvmTvmEventNative = TvmTvmEventNative;
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "sync", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "watch", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "unwatch", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "approveLimit", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "cancel", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "rejectLimit", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "retry", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "setBounty", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "expectedGas", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "nonce", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "bounty", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "decodedData", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "eventInitData", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "eventTokenWalletAddress", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "initializer", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "limitApproverAddress", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "meta", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "recipient", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "sender", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], TvmTvmEventNative.prototype, "status", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "decodeEvent", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "decodeTransaction", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTvmEventNative.prototype, "decodeTransactionEvents", null);