@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
129 lines (128 loc) • 4.86 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 { AbstractStore, errorLabelStyle, inheritTextStyle, } from '@broxus/js-core';
import { debug, error, groupCollapsed, groupEnd } from '@broxus/js-utils';
import { action, computed, makeObservable } from 'mobx';
import { SolanaToken } from '../../models/solana-token/SolanaToken';
import { resolveSolanaAddress } from '../../utils';
export class SolanaTokenWallet extends AbstractStore {
_connection;
params;
constructor(_connection, params) {
super();
this._connection = _connection;
this.params = params;
this.setData(() => ({
ownerAddress: resolveSolanaAddress(params.ownerAddress),
tokenAddress: resolveSolanaAddress(params.tokenAddress),
}));
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.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress);
this.setData('balance', balance);
}
catch (e) {
if (process.env.NODE_ENV !== 'production') {
groupCollapsed(`%c${this.constructor.name}%c Sync failed with an error`, errorLabelStyle, inheritTextStyle);
debug(`Code: ${e.code}`);
debug(`Message: ${e.message}`);
groupEnd();
}
}
finally {
this.setState('isSyncing', false);
}
}
async watch() {
try {
clearInterval(this.balanceCheckInterval);
this.balanceCheckInterval = setInterval(async () => {
const balance = await SolanaToken.Utils.balance(this._connection, this.tokenAddress, this.ownerAddress).catch(() => undefined);
this.setData('balance', balance);
}, this.params.watchDebounceDelay ?? 15000);
}
catch (e) {
error(e);
await this.unwatch();
}
}
async unwatch() {
try {
clearInterval(this.balanceCheckInterval);
this.balanceCheckInterval = undefined;
}
catch (e) {
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;
}
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "sync", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "watch", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SolanaTokenWallet.prototype, "unwatch", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "balance", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "ownerAddress", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "tokenAddress", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaTokenWallet.prototype, "isSyncing", null);