@broxus/js-core
Version:
MobX-based JavaScript Core library
272 lines (271 loc) • 11.4 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.VaultTokenWallet = void 0;
const js_utils_1 = require("@broxus/js-utils");
const mobx_1 = require("mobx");
const core_1 = require("../../core");
const tvm_token_1 = require("../../models/tvm-token");
const VaultTokenWalletUtils_1 = require("../../models/vault-token-wallet/VaultTokenWalletUtils");
const utils_1 = require("../../utils");
class VaultTokenWallet extends core_1.SmartContractModel {
_connection;
options;
_provider;
static Utils = VaultTokenWalletUtils_1.VaultTokenWalletUtils;
/**
* @param {ProviderRpcClient} _connection
* Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address
* VaultTokenWallet root address
* @param {Readonly<VaultTokenWalletCtorOptions>} [options]
* (optional) VaultTokenWallet 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;
(0, mobx_1.makeObservable)(this);
}
/**
* @param {ProviderRpcClient} connection
* Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Readonly<VaultTokenWalletCreateConfig>} config
* VaultTokenWallet Smart Contract Model config
* @param {Readonly<VaultTokenWalletCreateOptions>} [options]
* (optional) VaultTokenWallet Smart Contract Model options
* @param {ProviderRpcClient} [provider]
* (optional) RPC provider that require connection to the TVM wallet
*/
static async create(connection, config, options, provider) {
const { sync = true, watch, watchCallback, ...restOptions } = { ...options };
let address;
if ('address' in config) {
address = config.address;
}
else {
address = await tvm_token_1.TvmToken.Utils.walletOf(connection, {
ownerAddress: config.ownerAddress,
tokenAddress: config.tokenAddress,
});
}
const wallet = new VaultTokenWallet(connection, address, restOptions, provider);
if (sync) {
await wallet.sync({ force: false });
}
if (watch) {
await wallet.watch(watchCallback);
}
return wallet;
}
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 });
await this.syncComputedStorageData();
if (!this.isDeployed) {
(0, js_utils_1.throwException)('Wallet is not deployed');
}
const [balance, ownerAddress, tokenAddress] = await Promise.all([
VaultTokenWallet.Utils.balance(this._connection, { walletAddress: this.address }, state),
VaultTokenWallet.Utils.owner(this._connection, this.address, state),
VaultTokenWallet.Utils.root(this._connection, this.address, state),
]);
this.setData({ balance, ownerAddress, tokenAddress });
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
const state = await this._connection.getProviderState();
(0, utils_1.syncErrorMessage)(this.constructor.name, this.address, e, state.networkId.toString());
}
}
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, utils_1.subscribeDebugMessage)(this.constructor.name, this.address);
}
return stream.on((0, js_utils_1.debounce)(async (event) => {
if (process.env.NODE_ENV !== 'production') {
const state = await this._connection.getProviderState();
(0, utils_1.contractStateChangeDebugMessage)(this.constructor.name, this.address, event, state.networkId.toString());
}
if ((0, utils_1.areAddressesEqual)(event.address, this.address)) {
await this.sync({ force: !this.isSyncing, silent: true });
callback?.(...this.toJSON(true));
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, utils_1.unsubscribeDebugMessage)(this.constructor.name, this.address);
}
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
const state = await this._connection.getProviderState();
(0, utils_1.unsubscribeErrorMessage)(this.constructor.name, this.address, e, state.networkId.toString());
}
}
}
/**
* Accept Native - makes the transaction via token wallet contract to send message
* @param {VaultTokenWalletAcceptNativeAbiParams} params
* @param {Partial<SendInternalParams>} [args]
*/
async acceptNative(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
return VaultTokenWallet.Utils.acceptNative(this._provider, this.address, params, args);
}
/**
* Transfer - makes the transaction via token wallet contract to send message
* @param {VaultTokenWalletTransferAbiParams} params
* @param {Partial<SendInternalParams>} [args]
*/
async transfer(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
return VaultTokenWallet.Utils.transfer(this._provider, this.address, params, args);
}
/**
* Transfer to wallet - makes the transaction via token wallet contract to send message to a
* token wallet
* @param {VaultTokenWalletTransferToWalletAbiParams} params
* @param {Partial<SendInternalParams>} [args]
*/
async transferToWallet(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
return VaultTokenWallet.Utils.transferToWallet(this._provider, this.address, params, args);
}
/**
* Burns assets in favor of the address specified in callbackTo
* @param {VaultTokenWalletBurnAbiParams} params
* @param {Partial<SendInternalParams>} [args]
*/
async burn(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
return VaultTokenWallet.Utils.burn(this._provider, this.address, params, args);
}
/**
* Burns assets in favor of the address specified in callbackTo
* @param {VaultTokenWalletDestroyAbiParams} params
* @param {Partial<SendInternalParams>} [args]
*/
async destroy(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
return VaultTokenWallet.Utils.destroy(this._provider, this.address, params, args);
}
get balance() {
return this._data.balance;
}
get ownerAddress() {
return this._data.ownerAddress;
}
get tokenAddress() {
return this._data.tokenAddress;
}
}
exports.VaultTokenWallet = VaultTokenWallet;
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "sync", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "watch", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "unwatch", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "acceptNative", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "transfer", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "transferToWallet", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "burn", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], VaultTokenWallet.prototype, "destroy", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], VaultTokenWallet.prototype, "balance", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], VaultTokenWallet.prototype, "ownerAddress", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], VaultTokenWallet.prototype, "tokenAddress", null);