@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
36 lines (35 loc) • 1.78 kB
JavaScript
import { getFullContractState } from '@broxus/js-core';
// @ts-expect-error S2307: Cannot find module or its corresponding type declarations.
import core from 'everscale-standalone-client/core';
import init, { getJettonWalletData } from 'nekoton-wasm';
import { TonTokenUtils } from '../../models/ton-token/TonTokenUtils';
import { TonChains } from '../../types';
import { resolveTonAddress } from '../../utils';
import { getTonProtobufTransport } from '../../utils/get-ton-protobuf-transport';
export class TonTokenWalletUtils {
static endpoints = {
[]: 'https://jrpc-ton.broxus.com',
[]: 'https://jrpc-ton-testnet.broxus.com',
};
static async balance(connection, params) {
let tokenWalletAddress;
if ('walletAddress' in params) {
tokenWalletAddress = params.walletAddress;
}
else if ('tokenAddress' in params && 'ownerAddress' in params) {
const state = await getFullContractState(connection, resolveTonAddress(params.tokenAddress).toRawString());
tokenWalletAddress = await TonTokenUtils.walletOf(connection, params, state);
}
if (!tokenWalletAddress) {
throw new Error('Token Wallet not specified');
}
const state = await connection.getProviderState();
const endpoint = TonTokenWalletUtils.endpoints[state.networkId]
?? TonTokenWalletUtils.endpoints[TonChains.Mainnet];
await init();
const transport = await getTonProtobufTransport({ endpoint });
const clock = new core.nekoton.ClockWithOffset();
const data = await getJettonWalletData(clock, transport, resolveTonAddress(tokenWalletAddress).toRawString());
return data?.balance;
}
}