|
| 1 | +import { TxHash } from '@xchainjs/xchain-client' |
| 2 | +import { BaseAmount, baseAmount } from '@xchainjs/xchain-util' |
| 3 | +import axios from 'axios' |
| 4 | + |
| 5 | +import { |
| 6 | + GetAddressInfo, |
| 7 | + BalanceParams, |
| 8 | + BroadcastDTO, |
| 9 | + Transaction, |
| 10 | + TxHashParams, |
| 11 | + AddressUTXO, |
| 12 | +} from './nownodes-api-types' |
| 13 | + |
| 14 | +/** |
| 15 | + * Get transaction by hash. |
| 16 | + * |
| 17 | + * |
| 18 | + * @param {string} baseUrl The sochain node url. |
| 19 | + * @param {string} hash The transaction hash. |
| 20 | + * @returns {Transactions} |
| 21 | + */ |
| 22 | +export const getTx = async ({ apiKey, baseUrl, hash }: TxHashParams): Promise<Transaction> => { |
| 23 | + const url = `${baseUrl}/tx/${hash}` |
| 24 | + const response = await axios.get(url, { |
| 25 | + headers: { |
| 26 | + 'api-key': apiKey, |
| 27 | + } |
| 28 | + }) |
| 29 | + const tx: Transaction = response.data |
| 30 | + return tx |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Get transactions |
| 35 | + * |
| 36 | + * |
| 37 | + * @param {string} baseUrl The sochain node url. |
| 38 | + * @param {string} hash The transaction hash. |
| 39 | + * @returns {Transactions} |
| 40 | + */ |
| 41 | +export const getTxs = async ({ |
| 42 | + apiKey, |
| 43 | + address, |
| 44 | + baseUrl, |
| 45 | + limit, |
| 46 | +}: { |
| 47 | + apiKey?: string |
| 48 | + address: string |
| 49 | + baseUrl: string |
| 50 | + limit: number |
| 51 | +}): Promise<Transaction[]> => { |
| 52 | + const url = `${baseUrl}/address/${address}` |
| 53 | + const response = await axios.get(url, { |
| 54 | + params: { |
| 55 | + details: 'txs', |
| 56 | + pageSize: limit |
| 57 | + }, |
| 58 | + headers: { |
| 59 | + 'api-key': apiKey, |
| 60 | + } |
| 61 | + }) |
| 62 | + const txs: GetAddressInfo = response.data |
| 63 | + return txs.transactions as Transaction[] |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Get UTXOs |
| 68 | + * |
| 69 | + * @param {string} baseUrl The nownodes blockbook URL. |
| 70 | + * @param {string} address address. |
| 71 | + * @returns {Transactions} |
| 72 | + */ |
| 73 | + |
| 74 | +export const getUTXOs = async ({ |
| 75 | + apiKey, |
| 76 | + address, |
| 77 | + baseUrl, |
| 78 | + isConfirmed = true, |
| 79 | +}: { |
| 80 | + apiKey: string |
| 81 | + address: string |
| 82 | + baseUrl: string |
| 83 | + isConfirmed: boolean, |
| 84 | +}): Promise<AddressUTXO[]> => { |
| 85 | + const url = `${baseUrl}/utxo/${address}` |
| 86 | + const response = await axios.get(url, { |
| 87 | + params: { |
| 88 | + confirmed: isConfirmed |
| 89 | + }, |
| 90 | + headers: { |
| 91 | + 'api-key': apiKey, |
| 92 | + } |
| 93 | + }) |
| 94 | + const utxos: AddressUTXO[] = response.data |
| 95 | + return utxos |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Get address balance. |
| 100 | + * |
| 101 | + * |
| 102 | + * @param {string} baseUrl The sochain node url. |
| 103 | + * @param {string} address Address |
| 104 | + * @param {boolean} confirmedOnly Flag whether to get balances of confirmed txs only or for all |
| 105 | + * @returns {number} |
| 106 | + */ |
| 107 | +export const getBalance = async ({ |
| 108 | + apiKey, |
| 109 | + baseUrl, |
| 110 | + address, |
| 111 | + confirmedOnly = true, |
| 112 | + assetDecimals, |
| 113 | +}: BalanceParams): Promise<BaseAmount> => { |
| 114 | + const url = `${baseUrl}/address/${address}` |
| 115 | + const response = await axios.get(url, { |
| 116 | + headers: { |
| 117 | + 'api-key': apiKey, |
| 118 | + } |
| 119 | + }) |
| 120 | + const balanceResponse: GetAddressInfo = response.data |
| 121 | + const balance = confirmedOnly ? baseAmount(balanceResponse.balance, assetDecimals) : baseAmount(balanceResponse.balance).plus(balanceResponse.unconfirmedBalance, assetDecimals) |
| 122 | + return balance |
| 123 | +} |
| 124 | + |
| 125 | +export const broadcastTx = async ({ |
| 126 | + apiKey, |
| 127 | + baseUrl, |
| 128 | + txHex, |
| 129 | +}: { |
| 130 | + apiKey: string |
| 131 | + baseUrl: string |
| 132 | + txHex: string |
| 133 | +}): Promise<TxHash> => { |
| 134 | + const url = `${baseUrl}/sendtx/${txHex}` |
| 135 | + const response = await axios.get(url, { |
| 136 | + headers: { |
| 137 | + 'api-key': apiKey, |
| 138 | + } |
| 139 | + }) |
| 140 | + return (response.data as BroadcastDTO).result |
| 141 | +} |
0 commit comments