@broxus/js-core
Version:
MobX-based JavaScript Core library
137 lines (136 loc) • 6.13 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TvmTokenFactory = void 0;
const js_utils_1 = require("@broxus/js-utils");
const mobx_1 = require("mobx");
const console_1 = require("../../console");
const core_1 = require("../../core");
const TvmTokenFactoryUtils_1 = require("../../models/tvm-token-factory/TvmTokenFactoryUtils");
const utils_1 = require("../../utils");
class TvmTokenFactory extends core_1.SmartContractModel {
_connection;
options;
_provider;
static Utils = TvmTokenFactoryUtils_1.TvmTokenFactoryUtils;
/**
* @param {ProviderRpcClient} _connection
* Standalone RPC client that doesn't require connection to the TVM wallet provider
* @param {Address | string} address
* TvmTokenFactory root address
* @param {Readonly<TvmTokenFactoryCtorOptions>} [options]
* (optional) TvmTokenFactory 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);
}
async createToken(params, args) {
if (!this._provider) {
throw new utils_1.ProviderNotDefinedError(this.constructor.name);
}
const callId = params.callId ?? (0, utils_1.getRandomInt)();
const subscriber = new this._connection.Subscriber();
let transaction;
try {
const message = await TvmTokenFactory.Utils.createToken(this._provider, this.address, {
burnByRootDisabled: params.burnByRootDisabled,
burnPaused: params.burnPaused,
callId,
decimals: params.decimals,
deployWalletValue: params.deployWalletValue,
initialSupply: params.initialSupply,
initialSupplyTo: params.initialSupplyTo,
mintDisabled: params.mintDisabled,
name: params.name,
remainingGasTo: params.remainingGasTo,
symbol: params.symbol,
}, 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 (!(0, utils_1.areAddressesEqual)(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 === 'TokenCreated');
if (!event) {
return undefined;
}
if (process.env.NODE_ENV !== 'production') {
(0, js_utils_1.debug)(`%c${this.constructor.name}%c %cTokenCreated%c event was captured`, console_1.successLabelStyle, console_1.inheritTextStyle, console_1.successTextStyle, console_1.inheritTextStyle, event);
}
await params.onTransactionSuccess?.({
callId,
input: { tokenAddress: event.data.tokenRoot },
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') {
(0, js_utils_1.debug)(`%c${this.constructor.name}%c Unsubscribed from the token deploying stream`, console_1.warningLabelStyle, console_1.inheritTextStyle);
}
await subscriber?.unsubscribe();
}
}
decodeEvent(args) {
return TvmTokenFactory.Utils.decodeEvent(this._connection, this.address, args);
}
decodeTransaction(args) {
return TvmTokenFactory.Utils.decodeTransaction(this._connection, this.address, args);
}
decodeTransactionEvents(transaction) {
return TvmTokenFactory.Utils.decodeTransactionEvents(this._connection, this.address, transaction);
}
}
exports.TvmTokenFactory = TvmTokenFactory;
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTokenFactory.prototype, "decodeEvent", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTokenFactory.prototype, "decodeTransaction", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], TvmTokenFactory.prototype, "decodeTransactionEvents", null);