@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
270 lines (269 loc) • 11.8 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 { ProviderNotDefinedError, SmartContractModel, contractStateChangeDebugMessage, isAddressesEquals, subscribeDebugMessage, unsubscribeDebugMessage, unsubscribeErrorMessage, } from '@broxus/js-core';
import { debounce } from '@broxus/js-utils';
import { action, makeObservable } from 'mobx';
import { AlienProxyV10Utils, } from '../../models/alien-proxy/AlienProxyV10Utils';
export class AlienProxyV10 extends SmartContractModel {
_connection;
options;
_provider;
static Utils = AlienProxyV10Utils;
/**
* @param {ProviderRpcClient} _connection Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address AlienProxy root address
* @param {Readonly<AlienProxyV10CtorOptions>} [options] (optional) AlienProxy Smart Contract Model 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 AlienProxy root address
* @param {Readonly<AlienProxyV10CreateOptions>} [options] (optional) AlienProxy Smart Contract Model 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 proxy = new AlienProxyV10(connection, address, restOptions, provider);
if (sync) {
await proxy.syncContractState({ force: false });
}
if (watch) {
await proxy.watch(watchCallback);
}
return proxy;
}
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.syncContractState({ force: !this.isSyncing });
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 deployTokenFee(params, args) {
if (!this._provider) {
throw new ProviderNotDefinedError(this.constructor.name);
}
return AlienProxyV10.Utils.deployTokenFee(this._provider, this.address, {
remainingGasTo: params.remainingGasTo,
tokenAddress: params.tokenAddress,
}, args);
}
deriveEvmAlienTokenRoot(params) {
return AlienProxyV10.Utils.deriveEvmAlienTokenRoot(this._connection, this.address, params, this.contractState);
}
deriveTvmAlienTokenRoot(params) {
return AlienProxyV10.Utils.deriveTvmAlienTokenRoot(this._connection, this.address, params, this.contractState);
}
deriveSolanaAlienTokenRoot(params) {
return AlienProxyV10.Utils.deriveSolanaAlienTokenRoot(this._connection, this.address, params, this.contractState);
}
getExpectedTokenFeeAddress(tokenAddress) {
return AlienProxyV10.Utils.getExpectedTokenFeeAddress(this._connection, this.address, tokenAddress, this.contractState);
}
getDailyLimits(tokenAddress) {
return AlienProxyV10.Utils.getDailyLimits(this._connection, this.address, tokenAddress, this.contractState);
}
getTvmDefaultFee() {
return AlienProxyV10.Utils.getTvmDefaultFee(this._connection, this.address, this.contractState);
}
getTvmFees() {
return AlienProxyV10.Utils.getTvmFees(this._connection, this.address, this.contractState);
}
getTvmTokenFee(tokenAddress) {
return AlienProxyV10.Utils.getTvmTokenFee(this._connection, this.address, tokenAddress, this.contractState);
}
getTvmEvmConfiguration() {
return AlienProxyV10.Utils.getTvmEvmConfiguration(this._connection, this.address, this.contractState);
}
getTvmSolConfiguration() {
return AlienProxyV10.Utils.getTvmSolConfiguration(this._connection, this.address, this.contractState);
}
getSolTvmConfiguration() {
return AlienProxyV10.Utils.getSolTvmConfiguration(this._connection, this.address, this.contractState);
}
getEvmTokenMergeDetails(tokenAddress, chainId) {
return AlienProxyV10.Utils.getEvmTokenMergeDetails(this._connection, this.address, tokenAddress, chainId, this.contractState);
}
getTvmTokenMergeDetails(tokenAddress) {
return AlienProxyV10.Utils.getTvmTokenMergeDetails(this._connection, this.address, tokenAddress, this.contractState);
}
getSolTokenMergeDetails(tokenAddress) {
return AlienProxyV10.Utils.getSolTokenMergeDetails(this._connection, this.address, tokenAddress, this.contractState);
}
decodeEvent(args) {
return AlienProxyV10.Utils.decodeEvent(this._connection, this.address, args);
}
decodeTransaction(args) {
return AlienProxyV10.Utils.decodeTransaction(this._connection, this.address, args);
}
decodeTransactionEvents(transaction) {
return AlienProxyV10.Utils.decodeTransactionEvents(this._connection, this.address, transaction);
}
}
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "watch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "unwatch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "deployTokenFee", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "deriveEvmAlienTokenRoot", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "deriveTvmAlienTokenRoot", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "deriveSolanaAlienTokenRoot", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getExpectedTokenFeeAddress", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getDailyLimits", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmDefaultFee", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmFees", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmTokenFee", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmEvmConfiguration", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmSolConfiguration", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getSolTvmConfiguration", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getEvmTokenMergeDetails", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getTvmTokenMergeDetails", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "getSolTokenMergeDetails", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "decodeEvent", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "decodeTransaction", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AlienProxyV10.prototype, "decodeTransactionEvents", null);