@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
156 lines (155 loc) • 4.9 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);
};
var _a;
import { AbstractStore, } from '@broxus/js-core';
import { PublicKey } from '@solana/web3.js';
import { action, computed, makeObservable } from 'mobx';
import { SolanaTokenUtils } from '../../models/solana-token/SolanaTokenUtils';
import { resolveSolanaAddress } from '../../utils';
export class SolanaToken extends AbstractStore {
_connection;
static Utils = SolanaTokenUtils;
constructor(_connection, data) {
super();
this._connection = _connection;
const isAddress = typeof data === 'string' || data instanceof PublicKey;
if (!isAddress && !('address' in data)) {
throw new Error('Address is not specified');
}
const address = resolveSolanaAddress(isAddress ? data : data.address);
if (isAddress) {
this.setData('address', address);
}
else {
this.setData(() => data);
}
makeObservable(this);
}
static async create(connection, data, options) {
const { sync = true } = { ...options };
const token = new SolanaToken(connection, data);
if (sync) {
const details = await SolanaToken.Utils.getDetails(token.address);
if (!details?.symbol || details.decimals == null) {
throw new Error('Token cannot be resolved');
}
token.setData({ ...details });
}
return token;
}
async balance(ownerAddress) {
return SolanaToken.Utils.balance(this._connection, this.address, ownerAddress);
}
/**
* Returns token root address as instance of Address.
* @returns {SolanaTokenData['address']}
*/
get address() {
return this._data.address;
}
/**
*
*/
get chainId() {
return this._data.chainId;
}
/**
*
*/
get decimals() {
return this._data.decimals;
}
/**
*
*/
get icon() {
return this._data.logoURI;
}
/**
*
*/
get name() {
return this._data.name;
}
/**
*
*/
get root() {
return this._data.address?.toBase58();
}
/**
*
*/
get symbol() {
return this._data.symbol;
}
get(key) {
return this._data[key];
}
/**
* Returns copy of the current token
*/
clone() {
return new SolanaToken(this._connection, this.toJSON());
}
}
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], SolanaToken.prototype, "balance", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "address", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "chainId", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "decimals", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "icon", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "name", null);
__decorate([
computed,
__metadata("design:type", String),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "root", null);
__decorate([
computed,
__metadata("design:type", Object),
__metadata("design:paramtypes", [])
], SolanaToken.prototype, "symbol", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [typeof (_a = typeof K !== "undefined" && K) === "function" ? _a : Object]),
__metadata("design:returntype", Object)
], SolanaToken.prototype, "get", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", SolanaToken)
], SolanaToken.prototype, "clone", null);