@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
133 lines (132 loc) • 5.15 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.SolanaTokenWallet = void 0;
const js_core_1 = require("@broxus/js-core");
const js_utils_1 = require("@broxus/js-utils");
const mobx_1 = require("mobx");
const SolanaToken_1 = require("../../models/solana-token/SolanaToken");
const utils_1 = require("../../utils");
class SolanaTokenWallet extends js_core_1.AbstractStore {
_connection;
params;
constructor(_connection, params) {
super();
this._connection = _connection;
this.params = params;
this.setData(() => ({
ownerAddress: (0, utils_1.resolveSolanaAddress)(params.ownerAddress),
tokenAddress: (0, utils_1.resolveSolanaAddress)(params.tokenAddress),
}));
(0, mobx_1.makeObservable)(this);
}
static async create(connection, params) {
const { sync = true, watch, ...restParams } = params;
const wallet = new SolanaTokenWallet(connection, restParams);
await Promise.allSettled([sync && wallet.sync(), watch && wallet.watch()]);
return wallet;
}
async sync(options) {
if (!options?.force && this.isSyncing) {
return;
}
try {
this.setState('isSyncing', !options?.silent);
const balance = await SolanaToken_1.SolanaToken.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress);
this.setData('balance', balance);
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
(0, js_utils_1.groupCollapsed)(`%c${this.constructor.name}%c Sync failed with an error`, js_core_1.errorLabelStyle, js_core_1.inheritTextStyle);
(0, js_utils_1.debug)(`Code: ${e.code}`);
(0, js_utils_1.debug)(`Message: ${e.message}`);
(0, js_utils_1.groupEnd)();
}
}
finally {
this.setState('isSyncing', false);
}
}
async watch() {
try {
clearInterval(this.balanceCheckInterval);
this.balanceCheckInterval = setInterval(async () => {
const balance = await SolanaToken_1.SolanaToken.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress).catch(() => undefined);
this.setData('balance', balance);
}, this.params.watchDebounceDelay ?? 15000);
}
catch (e) {
(0, js_utils_1.error)(e);
await this.unwatch();
}
}
async unwatch() {
try {
clearInterval(this.balanceCheckInterval);
this.balanceCheckInterval = undefined;
}
catch (e) {
(0, js_utils_1.error)(e);
}
}
get balance() {
return this._data.balance;
}
get ownerAddress() {
return this._data.ownerAddress;
}
get tokenAddress() {
return this._data.tokenAddress;
}
get isSyncing() {
return this._state.isSyncing;
}
balanceCheckInterval;
}
exports.SolanaTokenWallet = SolanaTokenWallet;
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "sync", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "watch", null);
__decorate([
mobx_1.action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "unwatch", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "balance", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "ownerAddress", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "tokenAddress", null);
__decorate([
mobx_1.computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "isSyncing", null);