|
| 1 | +import { LTSRadixEngineToolkit, NetworkId, PrivateKey, RadixEngineToolkit } from '@radixdlt/radix-engine-toolkit' |
| 2 | +import { |
| 3 | + AssetInfo, |
| 4 | + Balance, |
| 5 | + BaseXChainClient, |
| 6 | + Fees, |
| 7 | + Network, |
| 8 | + PreparedTx, |
| 9 | + Tx, |
| 10 | + TxHistoryParams, |
| 11 | + TxParams, |
| 12 | + TxsPage, |
| 13 | +} from '@xchainjs/xchain-client' |
| 14 | +import { getSeed } from '@xchainjs/xchain-crypto/lib' |
| 15 | +import { Address, Asset } from '@xchainjs/xchain-util' |
| 16 | + |
| 17 | +const axios = require('axios') |
| 18 | + |
| 19 | +class RadixClient extends BaseXChainClient { |
| 20 | + /** |
| 21 | + * Get transaction fees. |
| 22 | + * |
| 23 | + * @param {TxParams} params - The transaction parameters. |
| 24 | + * @returns {Fees} The average, fast, and fastest fees. |
| 25 | + * @throws {"Params need to be passed"} Thrown if parameters are not provided. |
| 26 | + */ |
| 27 | + getFees(): never |
| 28 | + getFees(params: TxParams): Promise<Fees> |
| 29 | + async getFees(params?: TxParams): Promise<Fees> { |
| 30 | + if (!params) throw new Error('Params need to be passed') |
| 31 | + const fees = await this.estimateFees(params) |
| 32 | + return fees |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the address for a given account. |
| 37 | + * @deprecated Use getAddressAsync instead. |
| 38 | + */ |
| 39 | + getAddress(): string { |
| 40 | + throw new Error('getAddress is synchronous and cannot retrieve addresses directly. Use getAddressAsync instead.') |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the current address asynchronously for a given account. |
| 45 | + * @returns {Address} A promise resolving to the current address. |
| 46 | + * @throws {Error} Thrown if the phrase has not been set before. |
| 47 | + * A phrase is needed to create a wallet and to derive an address from it. |
| 48 | + */ |
| 49 | + async getAddressAsync(): Promise<string> { |
| 50 | + if (!this.phrase) throw new Error('Phrase not set') |
| 51 | + const seed = getSeed(this.phrase) |
| 52 | + const hexString = seed.toString('hex') |
| 53 | + const privateKey = new PrivateKey.Ed25519(hexString) |
| 54 | + const publicKey = privateKey.publicKey() |
| 55 | + const network = this.getNetwork() |
| 56 | + const networkId = network === Network.Mainnet ? NetworkId.Mainnet : NetworkId.Stokenet |
| 57 | + const address = await LTSRadixEngineToolkit.Derive.virtualAccountAddress(publicKey, networkId) |
| 58 | + return address.toString() |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the explorer URL based on the network. |
| 63 | + * |
| 64 | + * @returns {string} The explorer URL based on the network. |
| 65 | + */ |
| 66 | + getExplorerUrl(): string { |
| 67 | + switch (this.getNetwork()) { |
| 68 | + case Network.Mainnet: |
| 69 | + return 'https://explorer.radixdlt.com' |
| 70 | + case Network.Testnet: |
| 71 | + return 'https://stokenet-dashboard.radixdlt.com/' |
| 72 | + default: |
| 73 | + throw new Error('Unsupported network') |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the explorer URL for a given account address based on the network. |
| 79 | + * @param {Address} address The address to generate the explorer URL for. |
| 80 | + * @returns {string} The explorer URL for the given address. |
| 81 | + */ |
| 82 | + getExplorerAddressUrl(address: Address): string { |
| 83 | + return `${this.getExplorerUrl()}/account/${address}` |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the explorer URL for a given transaction ID based on the network. |
| 88 | + * @param {string} txID The transaction ID to generate the explorer URL for. |
| 89 | + * @returns {string} The explorer URL for the given transaction ID. |
| 90 | + */ |
| 91 | + getExplorerTxUrl(txID: string): string { |
| 92 | + return `${this.getExplorerUrl()}/transactions/${txID}` |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Validate the given address. |
| 97 | + * @param {Address} address The address to validate. |
| 98 | + * @returns {boolean} `true` if the address is valid, `false` otherwise. |
| 99 | + */ |
| 100 | + validateAddress(address: string): boolean { |
| 101 | + try { |
| 102 | + RadixEngineToolkit.Address.decode(address) |
| 103 | + return true |
| 104 | + } catch (error) { |
| 105 | + return false |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Retrieves the balance of a given address. |
| 111 | + * @param {Address} address - The address to retrieve the balance for. |
| 112 | + * @param {Asset[]} assets - Assets to retrieve the balance for (optional). |
| 113 | + * @returns {Promise<Balance[]>} An array containing the balance of the address. |
| 114 | + * @throws {"Invalid asset"} Thrown when the provided asset is invalid. |
| 115 | + */ |
| 116 | + async getBalance(address: string, assets?: Asset[] | undefined): Promise<Balance[]> { |
| 117 | + console.log(address + assets) |
| 118 | + try { |
| 119 | + // Assuming walletIndex is used to construct the URL |
| 120 | + const url = `https://jsonplaceholder.typicode.com/posts/}` |
| 121 | + console.log('Making GET request to:', url) |
| 122 | + const response = await axios.get(url) |
| 123 | + console.log('Response data:', response.data) |
| 124 | + // Assuming the response contains an address field |
| 125 | + const address = response.data.address |
| 126 | + return address |
| 127 | + } catch (error) { |
| 128 | + console.error('Error fetching address:', error) |
| 129 | + throw error |
| 130 | + } |
| 131 | + } |
| 132 | + async getTransactions(params?: TxHistoryParams | undefined): Promise<TxsPage> { |
| 133 | + try { |
| 134 | + console.log(params) |
| 135 | + // Assuming walletIndex is used to construct the URL |
| 136 | + const url = `https://jsonplaceholder.typicode.com/posts/` |
| 137 | + console.log('Making GET request to:', url) |
| 138 | + const response = await axios.get(url) |
| 139 | + console.log('Response data:', response.data) |
| 140 | + // Assuming the response contains an address field |
| 141 | + const address = response.data.address |
| 142 | + return address |
| 143 | + } catch (error) { |
| 144 | + console.error('Error fetching address:', error) |
| 145 | + throw error |
| 146 | + } |
| 147 | + } |
| 148 | + async getTransactionData(txId: string, assetAddress?: string | undefined): Promise<Tx> { |
| 149 | + try { |
| 150 | + console.log(txId + assetAddress) |
| 151 | + // Assuming walletIndex is used to construct the URL |
| 152 | + const url = `https://jsonplaceholder.typicode.com/posts/` |
| 153 | + console.log('Making GET request to:', url) |
| 154 | + const response = await axios.get(url) |
| 155 | + console.log('Response data:', response.data) |
| 156 | + // Assuming the response contains an address field |
| 157 | + const address = response.data.address |
| 158 | + return address |
| 159 | + } catch (error) { |
| 160 | + console.error('Error fetching address:', error) |
| 161 | + throw error |
| 162 | + } |
| 163 | + } |
| 164 | + async transfer(params: TxParams): Promise<string> { |
| 165 | + try { |
| 166 | + console.log(params) |
| 167 | + // Assuming walletIndex is used to construct the URL |
| 168 | + const url = `https://jsonplaceholder.typicode.com/posts/` |
| 169 | + console.log('Making GET request to:', url) |
| 170 | + const response = await axios.get(url) |
| 171 | + console.log('Response data:', response.data) |
| 172 | + // Assuming the response contains an address field |
| 173 | + const address = response.data.address |
| 174 | + return address |
| 175 | + } catch (error) { |
| 176 | + console.error('Error fetching address:', error) |
| 177 | + throw error |
| 178 | + } |
| 179 | + } |
| 180 | + async broadcastTx(txHex: string): Promise<string> { |
| 181 | + try { |
| 182 | + console.log(txHex) |
| 183 | + // Assuming walletIndex is used to construct the URL |
| 184 | + const url = `https://jsonplaceholder.typicode.com/posts/` |
| 185 | + console.log('Making GET request to:', url) |
| 186 | + const response = await axios.get(url) |
| 187 | + console.log('Response data:', response.data) |
| 188 | + // Assuming the response contains an address field |
| 189 | + const address = response.data.address |
| 190 | + return address |
| 191 | + } catch (error) { |
| 192 | + console.error('Error fetching address:', error) |
| 193 | + throw error |
| 194 | + } |
| 195 | + } |
| 196 | + getAssetInfo(): AssetInfo { |
| 197 | + throw new Error('Method not implemented.') |
| 198 | + } |
| 199 | + async prepareTx(params: TxParams): Promise<PreparedTx> { |
| 200 | + try { |
| 201 | + console.log(params) |
| 202 | + // Assuming walletIndex is used to construct the URL |
| 203 | + const url = `https://jsonplaceholder.typicode.com/posts/` |
| 204 | + console.log('Making GET request to:', url) |
| 205 | + const response = await axios.get(url) |
| 206 | + console.log('Response data:', response.data) |
| 207 | + // Assuming the response contains an address field |
| 208 | + const address = response.data.address |
| 209 | + return address |
| 210 | + } catch (error) { |
| 211 | + console.error('Error fetching address:', error) |
| 212 | + throw error |
| 213 | + } |
| 214 | + } |
| 215 | + async estimateFees(params?: TxParams): Promise<Fees> { |
| 216 | + try { |
| 217 | + console.log(params) |
| 218 | + // check if we can estimate the fees using the transaction manifest somehow |
| 219 | + // if we can estimate them, do an http request and return the fees |
| 220 | + const url = `fees_estimation_url` |
| 221 | + console.log('Making GET request to:', url) |
| 222 | + const response = await axios.get(url) |
| 223 | + console.log('Response data:', response.data) |
| 224 | + const fees = response.data.fees |
| 225 | + return fees |
| 226 | + } catch (error) { |
| 227 | + console.error('Error fetching address:', error) |
| 228 | + throw error |
| 229 | + } |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +export { RadixClient } |
0 commit comments