@tetherto/wdk-wallet-ton-gasless
Version:
A simple package to manage BIP-32 wallets for the TON blockchain, which implement the gasless functionality
98 lines (81 loc) • 3.21 kB
JavaScript
// Copyright 2024 Tether Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import WalletManager from '@tetherto/wdk-wallet'
import WalletAccountTonGasless from './wallet-account-ton-gasless.js'
/** @typedef {import('@tetherto/wdk-wallet-ton').FeeRates} FeeRates */
/** @typedef {import('./wallet-account-ton-gasless.js').TonGaslessWalletConfig} TonGaslessWalletConfig */
const TON_API_URL = 'https://tonapi.io/v2'
export default class WalletManagerTonGasless extends WalletManager {
/**
* Creates a new wallet manager for the ton blockchain that implements gasless features.
*
* @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
* @param {TonGaslessWalletConfig} config - The configuration object.
*/
constructor (seed, config) {
super(seed, config)
/**
* The ton gasless wallet configuration.
*
* @protected
* @type {TonGaslessWalletConfig}
*/
this._config = config
}
/**
* Returns the wallet account at a specific index (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
*
* @example
* // Returns the account with derivation path m/44'/607'/0'/0/1
* const account = await wallet.getAccount(1);
* @param {number} [index] - The index of the account to get (default: 0).
* @returns {Promise<WalletAccountTonGasless>} The account.
*/
async getAccount (index = 0) {
return await this.getAccountByPath(`0'/0/${index}`)
}
/**
* Returns the wallet account at a specific BIP-44 derivation path.
*
* @example
* // Returns the account with derivation path m/44'/607'/0'/0/1
* const account = await wallet.getAccountByPath("0'/0/1");
* @param {string} path - The derivation path (e.g. "0'/0/0").
* @returns {Promise<WalletAccountTonGasless>} The account.
*/
async getAccountByPath (path) {
if (!this._accounts[path]) {
const account = new WalletAccountTonGasless(this.seed, path, this._config)
this._accounts[path] = account
}
return this._accounts[path]
}
/**
* Returns the current fee rates.
*
* @returns {Promise<FeeRates>} The fee rates (in nanotons).
*/
async getFeeRates () {
/* eslint-disable camelcase */
const response = await fetch(`${TON_API_URL}/blockchain/config/raw`)
const { config: { config_param21 } } = await response.json()
const gasPrice = config_param21.gas_limits_prices.gas_flat_pfx.other.gas_prices_ext.gas_price
const feeRate = Math.round(gasPrice / 65_536)
return {
normal: BigInt(feeRate),
fast: BigInt(feeRate)
}
}
}