@swapkit/toolboxes 4.0.0-beta.50 → 4.0.0-beta.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/dist/src/cosmos/index.cjs +2 -2
  2. package/dist/src/cosmos/index.cjs.map +10 -6
  3. package/dist/src/cosmos/index.js +2 -2
  4. package/dist/src/cosmos/index.js.map +10 -6
  5. package/dist/src/evm/index.cjs +2 -2
  6. package/dist/src/evm/index.cjs.map +10 -8
  7. package/dist/src/evm/index.js +2 -2
  8. package/dist/src/evm/index.js.map +10 -8
  9. package/dist/src/index.cjs +4 -2
  10. package/dist/src/index.cjs.map +54 -4
  11. package/dist/src/index.js +4 -2
  12. package/dist/src/index.js.map +54 -4
  13. package/dist/src/near/index.cjs +2 -2
  14. package/dist/src/near/index.cjs.map +7 -5
  15. package/dist/src/near/index.js +2 -2
  16. package/dist/src/near/index.js.map +7 -5
  17. package/dist/src/radix/index.cjs +2 -2
  18. package/dist/src/radix/index.cjs.map +2 -2
  19. package/dist/src/radix/index.js +2 -2
  20. package/dist/src/radix/index.js.map +2 -2
  21. package/dist/src/ripple/index.cjs +2 -2
  22. package/dist/src/ripple/index.cjs.map +2 -2
  23. package/dist/src/ripple/index.js +2 -2
  24. package/dist/src/ripple/index.js.map +2 -2
  25. package/dist/src/solana/index.cjs +2 -2
  26. package/dist/src/solana/index.cjs.map +5 -4
  27. package/dist/src/solana/index.js +2 -2
  28. package/dist/src/solana/index.js.map +5 -4
  29. package/dist/src/substrate/index.cjs +2 -2
  30. package/dist/src/substrate/index.cjs.map +6 -5
  31. package/dist/src/substrate/index.js +2 -2
  32. package/dist/src/substrate/index.js.map +6 -5
  33. package/dist/src/tron/index.cjs +2 -2
  34. package/dist/src/tron/index.cjs.map +6 -5
  35. package/dist/src/tron/index.js +2 -2
  36. package/dist/src/tron/index.js.map +6 -5
  37. package/dist/src/utxo/index.cjs +4 -4
  38. package/dist/src/utxo/index.cjs.map +10 -7
  39. package/dist/src/utxo/index.js +4 -4
  40. package/dist/src/utxo/index.js.map +10 -7
  41. package/{src/cosmos/index.ts → dist/types/cosmos/index.d.ts} +1 -0
  42. package/dist/types/cosmos/index.d.ts.map +1 -0
  43. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
  44. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
  45. package/{src/cosmos/thorchainUtils/index.ts → dist/types/cosmos/thorchainUtils/index.d.ts} +1 -0
  46. package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
  47. package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
  48. package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
  49. package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
  50. package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
  51. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
  52. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
  53. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +66 -0
  54. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
  55. package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
  56. package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
  57. package/dist/types/cosmos/toolbox/cosmos.d.ts +93 -0
  58. package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
  59. package/dist/types/cosmos/toolbox/index.d.ts +14 -0
  60. package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
  61. package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
  62. package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
  63. package/{src/cosmos/types.ts → dist/types/cosmos/types.d.ts} +24 -29
  64. package/dist/types/cosmos/types.d.ts.map +1 -0
  65. package/dist/types/cosmos/util.d.ts +68 -0
  66. package/dist/types/cosmos/util.d.ts.map +1 -0
  67. package/dist/types/evm/api.d.ts +8 -0
  68. package/dist/types/evm/api.d.ts.map +1 -0
  69. package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
  70. package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
  71. package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
  72. package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
  73. package/dist/types/evm/helpers.d.ts +20 -0
  74. package/dist/types/evm/helpers.d.ts.map +1 -0
  75. package/{src/evm/index.ts → dist/types/evm/index.d.ts} +1 -0
  76. package/dist/types/evm/index.d.ts.map +1 -0
  77. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +82 -0
  78. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
  79. package/dist/types/evm/toolbox/evm.d.ts +367 -0
  80. package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
  81. package/dist/types/evm/toolbox/index.d.ts +89 -0
  82. package/dist/types/evm/toolbox/index.d.ts.map +1 -0
  83. package/dist/types/evm/toolbox/op.d.ts +62 -0
  84. package/dist/types/evm/toolbox/op.d.ts.map +1 -0
  85. package/dist/types/evm/types.d.ts +98 -0
  86. package/dist/types/evm/types.d.ts.map +1 -0
  87. package/dist/types/index.d.ts +63 -0
  88. package/dist/types/index.d.ts.map +1 -0
  89. package/dist/types/near/helpers/core.d.ts +15 -0
  90. package/dist/types/near/helpers/core.d.ts.map +1 -0
  91. package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
  92. package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
  93. package/dist/types/near/helpers/nep141.d.ts +72 -0
  94. package/dist/types/near/helpers/nep141.d.ts.map +1 -0
  95. package/dist/types/near/index.d.ts +10 -0
  96. package/dist/types/near/index.d.ts.map +1 -0
  97. package/dist/types/near/toolbox.d.ts +4 -0
  98. package/dist/types/near/toolbox.d.ts.map +1 -0
  99. package/dist/types/near/types/contract.d.ts +38 -0
  100. package/dist/types/near/types/contract.d.ts.map +1 -0
  101. package/dist/types/near/types/nep141.d.ts +56 -0
  102. package/dist/types/near/types/nep141.d.ts.map +1 -0
  103. package/dist/types/near/types/toolbox.d.ts +57 -0
  104. package/dist/types/near/types/toolbox.d.ts.map +1 -0
  105. package/dist/types/near/types.d.ts +44 -0
  106. package/dist/types/near/types.d.ts.map +1 -0
  107. package/dist/types/radix/index.d.ts +14 -0
  108. package/dist/types/radix/index.d.ts.map +1 -0
  109. package/dist/types/ripple/index.d.ts +43 -0
  110. package/dist/types/ripple/index.d.ts.map +1 -0
  111. package/dist/types/solana/index.d.ts +37 -0
  112. package/dist/types/solana/index.d.ts.map +1 -0
  113. package/dist/types/solana/toolbox.d.ts +41 -0
  114. package/dist/types/solana/toolbox.d.ts.map +1 -0
  115. package/dist/types/substrate/balance.d.ts +17 -0
  116. package/dist/types/substrate/balance.d.ts.map +1 -0
  117. package/{src/substrate/index.ts → dist/types/substrate/index.d.ts} +1 -0
  118. package/dist/types/substrate/index.d.ts.map +1 -0
  119. package/dist/types/substrate/substrate.d.ts +148 -0
  120. package/dist/types/substrate/substrate.d.ts.map +1 -0
  121. package/dist/types/substrate/types.d.ts +100 -0
  122. package/dist/types/substrate/types.d.ts.map +1 -0
  123. package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
  124. package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
  125. package/dist/types/tron/helpers/trongrid.d.ts +8 -0
  126. package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
  127. package/dist/types/tron/index.d.ts +6 -0
  128. package/dist/types/tron/index.d.ts.map +1 -0
  129. package/dist/types/tron/toolbox.d.ts +26 -0
  130. package/dist/types/tron/toolbox.d.ts.map +1 -0
  131. package/dist/types/tron/types.d.ts +101 -0
  132. package/dist/types/tron/types.d.ts.map +1 -0
  133. package/dist/types/types.d.ts +18 -0
  134. package/dist/types/types.d.ts.map +1 -0
  135. package/dist/types/utils.d.ts +4 -0
  136. package/dist/types/utils.d.ts.map +1 -0
  137. package/dist/types/utxo/helpers/api.d.ts +133 -0
  138. package/dist/types/utxo/helpers/api.d.ts.map +1 -0
  139. package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
  140. package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
  141. package/dist/types/utxo/helpers/coinselect.d.ts +16 -0
  142. package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
  143. package/{src/utxo/helpers/index.ts → dist/types/utxo/helpers/index.d.ts} +1 -0
  144. package/dist/types/utxo/helpers/index.d.ts.map +1 -0
  145. package/dist/types/utxo/helpers/txSize.d.ts +21 -0
  146. package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
  147. package/{src/utxo/index.ts → dist/types/utxo/index.d.ts} +1 -0
  148. package/dist/types/utxo/index.d.ts.map +1 -0
  149. package/dist/types/utxo/toolbox/bitcoinCash.d.ts +104 -0
  150. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
  151. package/dist/types/utxo/toolbox/index.d.ts +50 -0
  152. package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
  153. package/dist/types/utxo/toolbox/utxo.d.ts +102 -0
  154. package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
  155. package/dist/types/utxo/toolbox/zcash.d.ts +83 -0
  156. package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
  157. package/dist/types/utxo/types.d.ts +46 -0
  158. package/dist/types/utxo/types.d.ts.map +1 -0
  159. package/package.json +15 -18
  160. package/dist/chunk-0h4xdrwz.js +0 -5
  161. package/dist/chunk-0h4xdrwz.js.map +0 -10
  162. package/dist/chunk-4yap1fvd.js +0 -4
  163. package/dist/chunk-4yap1fvd.js.map +0 -10
  164. package/dist/chunk-9bqegm61.js +0 -4
  165. package/dist/chunk-9bqegm61.js.map +0 -10
  166. package/dist/chunk-fazw0jvt.js +0 -4
  167. package/dist/chunk-fazw0jvt.js.map +0 -9
  168. package/dist/chunk-fjfxga2v.js +0 -4
  169. package/dist/chunk-fjfxga2v.js.map +0 -10
  170. package/dist/chunk-s47y8512.js +0 -5
  171. package/dist/chunk-s47y8512.js.map +0 -9
  172. package/dist/chunk-vtd17cje.js +0 -4
  173. package/dist/chunk-vtd17cje.js.map +0 -10
  174. package/dist/chunk-zcdeg6h9.js +0 -5
  175. package/dist/chunk-zcdeg6h9.js.map +0 -10
  176. package/src/cosmos/thorchainUtils/addressFormat.ts +0 -26
  177. package/src/cosmos/thorchainUtils/messages.ts +0 -262
  178. package/src/cosmos/thorchainUtils/registry.ts +0 -44
  179. package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +0 -2800
  180. package/src/cosmos/thorchainUtils/types/client-types.ts +0 -73
  181. package/src/cosmos/thorchainUtils/types/index.ts +0 -1
  182. package/src/cosmos/toolbox/cosmos.ts +0 -375
  183. package/src/cosmos/toolbox/index.ts +0 -33
  184. package/src/cosmos/toolbox/thorchain.ts +0 -313
  185. package/src/cosmos/util.ts +0 -266
  186. package/src/evm/__tests__/address-validation.test.ts +0 -86
  187. package/src/evm/__tests__/ethereum.test.ts +0 -141
  188. package/src/evm/api.ts +0 -21
  189. package/src/evm/contracts/eth/multicall.ts +0 -165
  190. package/src/evm/contracts/op/gasOracle.ts +0 -151
  191. package/src/evm/helpers.ts +0 -194
  192. package/src/evm/toolbox/baseEVMToolbox.ts +0 -762
  193. package/src/evm/toolbox/evm.ts +0 -66
  194. package/src/evm/toolbox/index.ts +0 -52
  195. package/src/evm/toolbox/op.ts +0 -131
  196. package/src/evm/types.ts +0 -146
  197. package/src/index.ts +0 -263
  198. package/src/near/__tests__/core.test.ts +0 -80
  199. package/src/near/helpers/contractFactory.ts +0 -22
  200. package/src/near/helpers/core.ts +0 -91
  201. package/src/near/helpers/gasEstimation.ts +0 -110
  202. package/src/near/helpers/index.ts +0 -5
  203. package/src/near/helpers/nep141.ts +0 -110
  204. package/src/near/index.ts +0 -24
  205. package/src/near/toolbox.ts +0 -509
  206. package/src/near/types/contract.ts +0 -48
  207. package/src/near/types/nep141.ts +0 -66
  208. package/src/near/types.ts +0 -57
  209. package/src/radix/index.ts +0 -156
  210. package/src/ripple/index.ts +0 -192
  211. package/src/solana/index.ts +0 -55
  212. package/src/solana/toolbox.ts +0 -433
  213. package/src/substrate/balance.ts +0 -92
  214. package/src/substrate/substrate.ts +0 -320
  215. package/src/substrate/types.ts +0 -120
  216. package/src/tron/__tests__/toolbox.test.ts +0 -147
  217. package/src/tron/helpers/trc20.abi.ts +0 -107
  218. package/src/tron/helpers/trongrid.ts +0 -54
  219. package/src/tron/index.ts +0 -17
  220. package/src/tron/toolbox.ts +0 -650
  221. package/src/tron/types.ts +0 -120
  222. package/src/utils.ts +0 -27
  223. package/src/utxo/__tests__/zcash-integration.test.ts +0 -114
  224. package/src/utxo/helpers/api.ts +0 -560
  225. package/src/utxo/helpers/bchaddrjs.ts +0 -183
  226. package/src/utxo/helpers/coinselect.ts +0 -98
  227. package/src/utxo/helpers/txSize.ts +0 -104
  228. package/src/utxo/toolbox/bitcoinCash.ts +0 -320
  229. package/src/utxo/toolbox/index.ts +0 -90
  230. package/src/utxo/toolbox/utxo.ts +0 -525
  231. package/src/utxo/toolbox/zcash.ts +0 -208
  232. package/src/utxo/types.ts +0 -57
@@ -1,560 +0,0 @@
1
- import {
2
- Chain,
3
- RequestClient,
4
- SKConfig,
5
- SwapKitError,
6
- type UTXOChain,
7
- warnOnce,
8
- } from "@swapkit/helpers";
9
- import { networks } from "bitcoinjs-lib";
10
- import { uniqid } from "../../utils";
11
-
12
- // @ts-ignore
13
- import coininfo from "coininfo";
14
-
15
- type BlockchairParams<T> = T & { chain: Chain; apiKey?: string };
16
- type BlockchairFetchUnspentUtxoParams = BlockchairParams<{
17
- offset?: number;
18
- limit?: number;
19
- address: string;
20
- targetValue?: number;
21
- accumulativeValue?: number;
22
- }>;
23
-
24
- async function broadcastUTXOTx({ chain, txHash }: { chain: Chain; txHash: string }) {
25
- const rpcUrl = SKConfig.get("rpcUrls")[chain];
26
- const body = JSON.stringify({
27
- jsonrpc: "2.0",
28
- method: "sendrawtransaction",
29
- params: [txHash],
30
- id: uniqid(),
31
- });
32
-
33
- const response = await RequestClient.post<{
34
- id: string;
35
- result: string;
36
- error: { message: string; code?: number } | null;
37
- }>(rpcUrl, { headers: { "Content-Type": "application/json" }, body });
38
-
39
- if (response.error) {
40
- throw new SwapKitError("toolbox_utxo_broadcast_failed", {
41
- error: response.error?.message,
42
- });
43
- }
44
-
45
- if (response.result.includes('"code":-26')) {
46
- throw new SwapKitError("toolbox_utxo_invalid_transaction", {
47
- error: "Transaction amount was too low",
48
- });
49
- }
50
-
51
- return response.result;
52
- }
53
-
54
- function baseUrl(chain: Chain) {
55
- return `https://api.blockchair.com/${mapChainToBlockchairChain(chain)}`;
56
- }
57
-
58
- function getDefaultTxFeeByChain(chain: Chain) {
59
- switch (chain) {
60
- case Chain.Bitcoin:
61
- return 5;
62
- case Chain.Dogecoin:
63
- return 10000;
64
- case Chain.Litecoin:
65
- return 1;
66
- case Chain.Zcash:
67
- return 1;
68
- default:
69
- return 2;
70
- }
71
- }
72
-
73
- function mapChainToBlockchairChain(chain: Chain) {
74
- switch (chain) {
75
- case Chain.BitcoinCash:
76
- return "bitcoin-cash";
77
- case Chain.Litecoin:
78
- return "litecoin";
79
- case Chain.Dash:
80
- return "dash";
81
- case Chain.Dogecoin:
82
- return "dogecoin";
83
- case Chain.Zcash:
84
- return "zcash";
85
- case Chain.Polkadot:
86
- return "polkadot";
87
- default:
88
- return "bitcoin";
89
- }
90
- }
91
-
92
- async function getSuggestedTxFee(chain: Chain) {
93
- try {
94
- //Use Bitgo API for fee estimation
95
- //Refer: https://app.bitgo.com/docs/#operation/v2.tx.getfeeestimate
96
- const { feePerKb } = await RequestClient.get<{
97
- feePerKb: number;
98
- cpfpFeePerKb: number;
99
- numBlocks: number;
100
- feeByBlockTarget: { 1: number; 3: number };
101
- }>(`https://app.bitgo.com/api/v2/${chain.toLowerCase()}/tx/fee`);
102
- const suggestedFee = feePerKb / 1000;
103
-
104
- return Math.max(suggestedFee, getDefaultTxFeeByChain(chain));
105
- } catch (_error) {
106
- return getDefaultTxFeeByChain(chain);
107
- }
108
- }
109
-
110
- async function blockchairRequest<T>(url: string, apiKey?: string): Promise<T> {
111
- const response = await RequestClient.get<BlockchairResponse<T>>(
112
- `${url}${apiKey ? `${url.includes("?") ? "&" : "?"}key=${apiKey}` : ""}`,
113
- );
114
-
115
- if (!response || response.context.code !== 200)
116
- throw new SwapKitError("toolbox_utxo_api_error", {
117
- error: `Failed to query ${url}`,
118
- });
119
-
120
- return response.data as T;
121
- }
122
-
123
- async function getAddressData({ address, chain, apiKey }: BlockchairParams<{ address?: string }>) {
124
- if (!address)
125
- throw new SwapKitError("toolbox_utxo_invalid_params", {
126
- error: "Address is required",
127
- });
128
-
129
- try {
130
- const response = await blockchairRequest<BlockchairAddressResponse>(
131
- `${baseUrl(chain)}/dashboards/address/${address}?transaction_details=true`,
132
- apiKey,
133
- );
134
-
135
- return response[address];
136
- } catch (_error) {
137
- return { utxo: [], address: { balance: 0, transaction_count: 0 } };
138
- }
139
- }
140
-
141
- async function getUnconfirmedBalance({
142
- address,
143
- chain,
144
- apiKey,
145
- }: BlockchairParams<{ address?: string }>) {
146
- const response = await getAddressData({ address, chain, apiKey });
147
-
148
- return response?.address.balance || 0;
149
- }
150
-
151
- async function getRawTx({ chain, apiKey, txHash }: BlockchairParams<{ txHash?: string }>) {
152
- if (!txHash)
153
- throw new SwapKitError("toolbox_utxo_invalid_params", {
154
- error: "TxHash is required",
155
- });
156
-
157
- try {
158
- const rawTxResponse = await blockchairRequest<BlockchairRawTransactionResponse>(
159
- `${baseUrl(chain)}/raw/transaction/${txHash}`,
160
- apiKey,
161
- );
162
- return rawTxResponse?.[txHash]?.raw_transaction || "";
163
- } catch (error) {
164
- console.error("Failed to fetch raw transaction:", error);
165
- return "";
166
- }
167
- }
168
-
169
- async function fetchUtxosBatch({
170
- chain,
171
- address,
172
- apiKey,
173
- offset = 0,
174
- limit = 30,
175
- }: BlockchairFetchUnspentUtxoParams) {
176
- // Only fetch the fields we need to reduce payload size
177
- const fields = "is_spent,transaction_hash,index,value,script_hex,block_id,spending_signature_hex";
178
-
179
- const response = await blockchairRequest<BlockchairOutputsResponse[]>(
180
- // TODO - remove max value limit once we updated bitcoinjs-lib to support larger values
181
- `${baseUrl(chain)}/outputs?q=recipient(${address}),is_spent(false),value(..2000000000000000)&s=value(desc)&fields=${fields}&limit=${limit}&offset=${offset}`,
182
- apiKey,
183
- );
184
-
185
- const txs = response.map(
186
- ({
187
- is_spent,
188
- script_hex,
189
- block_id,
190
- transaction_hash,
191
- index,
192
- value,
193
- spending_signature_hex,
194
- }) => ({
195
- hash: transaction_hash,
196
- index,
197
- value,
198
- txHex: spending_signature_hex,
199
- script_hex,
200
- is_confirmed: block_id !== -1,
201
- is_spent,
202
- }),
203
- );
204
-
205
- return txs;
206
- }
207
-
208
- function getTxsValue(txs: Awaited<ReturnType<typeof fetchUtxosBatch>>) {
209
- return txs.reduce((total, tx) => total + tx.value, 0);
210
- }
211
-
212
- function pickMostValuableTxs(
213
- txs: Awaited<ReturnType<typeof fetchUtxosBatch>>,
214
- targetValue?: number,
215
- ): Awaited<ReturnType<typeof fetchUtxosBatch>> {
216
- const sortedTxs = [...txs].sort((a, b) => b.value - a.value);
217
-
218
- if (targetValue) {
219
- const result = [];
220
- let accumulated = 0;
221
-
222
- for (const utxo of sortedTxs) {
223
- result.push(utxo);
224
- accumulated += utxo.value;
225
- if (accumulated >= targetValue) break;
226
- }
227
-
228
- return result;
229
- }
230
-
231
- return sortedTxs;
232
- }
233
-
234
- async function getUnspentUtxos({
235
- chain,
236
- address,
237
- apiKey,
238
- targetValue,
239
- accumulativeValue = 0,
240
- offset = 0,
241
- limit = 30,
242
- }: BlockchairFetchUnspentUtxoParams): Promise<Awaited<ReturnType<typeof fetchUtxosBatch>>> {
243
- if (!address)
244
- throw new SwapKitError("toolbox_utxo_invalid_params", {
245
- error: "Address is required",
246
- });
247
-
248
- try {
249
- const utxos = await fetchUtxosBatch({
250
- targetValue,
251
- chain,
252
- address,
253
- apiKey,
254
- offset,
255
- limit,
256
- });
257
- const utxosCount = utxos.length;
258
- const isComplete = utxosCount < limit;
259
-
260
- const unspentUtxos = utxos.filter(({ is_spent }) => !is_spent);
261
-
262
- const unspentUtxosValue = getTxsValue(unspentUtxos);
263
- const totalCurrentValue = accumulativeValue + unspentUtxosValue;
264
-
265
- const limitReached = targetValue && totalCurrentValue >= targetValue;
266
-
267
- if (isComplete || limitReached) {
268
- return pickMostValuableTxs(unspentUtxos, targetValue);
269
- }
270
-
271
- const nextBatch = await getUnspentUtxos({
272
- chain,
273
- address,
274
- apiKey,
275
- offset: offset + limit,
276
- limit,
277
- accumulativeValue: totalCurrentValue,
278
- targetValue,
279
- });
280
-
281
- const allUtxos = [...unspentUtxos, ...nextBatch];
282
-
283
- return pickMostValuableTxs(allUtxos, targetValue);
284
- } catch (error) {
285
- console.error("Failed to fetch unspent UTXOs:", error);
286
- return [];
287
- }
288
- }
289
-
290
- async function getUtxos({
291
- address,
292
- chain,
293
- apiKey,
294
- fetchTxHex = true,
295
- targetValue,
296
- }: BlockchairParams<{
297
- address: string;
298
- fetchTxHex?: boolean;
299
- targetValue?: number;
300
- }>) {
301
- const utxos = await getUnspentUtxos({ chain, address, apiKey, targetValue });
302
-
303
- const results = [];
304
-
305
- for (const { hash, index, script_hex, value } of utxos) {
306
- let txHex: string | undefined;
307
- if (fetchTxHex) {
308
- txHex = await getRawTx({ txHash: hash, chain, apiKey });
309
- }
310
- results.push({
311
- address,
312
- hash,
313
- index,
314
- txHex,
315
- value,
316
- witnessUtxo: { value, script: Buffer.from(script_hex, "hex") },
317
- });
318
- }
319
- return results;
320
- }
321
-
322
- function utxoApi(chain: UTXOChain) {
323
- const apiKey = SKConfig.get("apiKeys").blockchair || "";
324
-
325
- warnOnce(!apiKey, "No Blockchair API key found. Functionality will be limited.");
326
-
327
- return {
328
- broadcastTx: (txHash: string) => broadcastUTXOTx({ txHash, chain }),
329
- getRawTx: (txHash: string) => getRawTx({ txHash, chain, apiKey }),
330
- getSuggestedTxFee: () => getSuggestedTxFee(chain),
331
- getBalance: (address: string) => getUnconfirmedBalance({ address, chain, apiKey }),
332
- getAddressData: (address: string) => getAddressData({ address, chain, apiKey }),
333
- getUtxos: (params: {
334
- address: string;
335
- fetchTxHex?: boolean;
336
- targetValue?: number;
337
- }) => getUtxos({ ...params, chain, apiKey }),
338
- };
339
- }
340
-
341
- /**
342
- * "Factory" to ensure typing for custom UTXO APIs
343
- */
344
- export function createCustomUtxoApi(methods: ReturnType<typeof utxoApi>) {
345
- return methods;
346
- }
347
-
348
- export function getUtxoApi(chain: UTXOChain) {
349
- const customUtxoApi = SKConfig.get("apis")[chain];
350
-
351
- if (customUtxoApi) {
352
- warnOnce(true, "Using custom UTXO API. Be sure to implement all methods to avoid issues.");
353
- return customUtxoApi as ReturnType<typeof utxoApi>;
354
- }
355
-
356
- return utxoApi(chain);
357
- }
358
-
359
- // Define Zcash network objects that match ECPair's expected interface
360
- const ZCASH_MAINNET = {
361
- messagePrefix: "\x19Zcash Signed Message:\n",
362
- bech32: "zc",
363
- bip32: {
364
- public: 0x0488b21e,
365
- private: 0x0488ade4,
366
- },
367
- pubKeyHash: 0x1c, // 28 in decimal - correct for Zcash mainnet
368
- scriptHash: 0x1c, // 28 in decimal
369
- wif: 0x80, // 128 in decimal
370
- };
371
-
372
- const ZCASH_TESTNET = {
373
- messagePrefix: "\x19Zcash Signed Message:\n",
374
- bech32: "ztestsapling",
375
- bip32: {
376
- public: 0x043587cf,
377
- private: 0x04358394,
378
- },
379
- pubKeyHash: 0x1d, // 29 in decimal - correct for Zcash testnet
380
- scriptHash: 0x1c, // 28 in decimal
381
- wif: 0xef, // 239 in decimal
382
- };
383
-
384
- export function getUtxoNetwork() {
385
- return function getNetwork(chain: Chain) {
386
- switch (chain) {
387
- case Chain.Bitcoin:
388
- return networks.bitcoin;
389
- case Chain.BitcoinCash:
390
- return coininfo.bitcoincash.main.toBitcoinJS();
391
- case Chain.Dash:
392
- return coininfo.dash.main.toBitcoinJS();
393
- case Chain.Litecoin:
394
- return coininfo.litecoin.main.toBitcoinJS();
395
-
396
- case Chain.Dogecoin: {
397
- const bip32 = { private: 0x04358394, public: 0x043587cf };
398
- const test = coininfo.dogecoin.test;
399
- test.versions.bip32 = bip32;
400
- return coininfo.dogecoin.main.toBitcoinJS();
401
- }
402
-
403
- case Chain.Zcash: {
404
- // Get Zcash network configuration using our custom objects
405
- const { isStagenet } = SKConfig.get("envs");
406
- return isStagenet ? ZCASH_TESTNET : ZCASH_MAINNET;
407
- }
408
-
409
- default:
410
- throw new SwapKitError("toolbox_utxo_not_supported", { chain });
411
- }
412
- };
413
- }
414
-
415
- interface BlockchairVin {
416
- txid: string;
417
- vout: number;
418
- scriptSig: {
419
- asm: string;
420
- hex: string;
421
- };
422
- sequence: number;
423
- }
424
-
425
- interface BlockchairVout {
426
- value: number;
427
- n: number;
428
- scriptPubKey: {
429
- asm: string;
430
- hex: string;
431
- address: string;
432
- type: string;
433
- addresses: string[];
434
- reqSigs: number;
435
- };
436
- }
437
-
438
- interface BlockchairTransaction {
439
- block_id: number;
440
- hash: string;
441
- time: string;
442
- balance_change: number;
443
- }
444
-
445
- interface BlockchairUtxo {
446
- block_id: number;
447
- transaction_hash: string;
448
- index: number;
449
- value: number;
450
- }
451
-
452
- interface BlockchairAddressCoreData {
453
- type: string;
454
- script_hex: string;
455
- balance: number;
456
- balance_usd: number;
457
- received: number;
458
- received_usd: number;
459
- spent: number;
460
- spent_usd: number;
461
- output_count: number;
462
- unspent_output_count: number;
463
- first_seen_receiving: string;
464
- last_seen_receiving: string;
465
- first_seen_spending: null | string;
466
- last_seen_spending: null | string;
467
- transaction_count: number;
468
- scripthash_type: null | string;
469
- }
470
-
471
- interface BlockchairInputOutputCommonData {
472
- block_id: number;
473
- transaction_id: number;
474
- index: number;
475
- transaction_hash: string;
476
- date: string;
477
- time: string;
478
- value: number;
479
- value_usd: number;
480
- recipient: string;
481
- type: string;
482
- script_hex: string;
483
- is_from_coinbase: boolean;
484
- is_spendable: boolean | null;
485
- is_spent: boolean;
486
- lifespan: number | null;
487
- cdd: number | null;
488
- }
489
-
490
- interface BlockchairSpendingBlockData {
491
- spending_block_id: number | null;
492
- spending_transaction_id: number | null;
493
- spending_index: number | null;
494
- spending_transaction_hash: string | null;
495
- spending_date: string | null;
496
- spending_time: string | null;
497
- spending_value_usd: number | null;
498
- spending_sequence: number | null;
499
- spending_signature_hex: string | null;
500
- spending_witness: string | null;
501
- }
502
-
503
- interface BlockchairAddressResponse {
504
- [key: string]: {
505
- address: BlockchairAddressCoreData;
506
- transactions: BlockchairTransaction[];
507
- utxo: BlockchairUtxo[];
508
- };
509
- }
510
-
511
- interface BlockchairOutputsResponse
512
- extends BlockchairSpendingBlockData,
513
- BlockchairInputOutputCommonData {}
514
-
515
- interface BlockchairRawTransactionResponse {
516
- [key: string]: {
517
- raw_transaction: string;
518
- decoded_raw_transaction: {
519
- txid: string;
520
- hash: string;
521
- version: number;
522
- size: number;
523
- vsize: number;
524
- weight: number;
525
- locktime: number;
526
- vin: BlockchairVin[];
527
- vout: BlockchairVout[];
528
- };
529
- };
530
- }
531
-
532
- interface BlockchairResponse<T> {
533
- data: T;
534
- context: {
535
- code: number;
536
- source: string;
537
- results: number;
538
- state: number;
539
- market_price_usd: number;
540
- cache: {
541
- live: boolean;
542
- duration: number;
543
- since: string;
544
- until: string;
545
- time: any;
546
- };
547
- api: {
548
- version: string;
549
- last_major_update: string;
550
- next_major_update: null | string;
551
- documentation: string;
552
- notice: string;
553
- };
554
- servers: string;
555
- time: number;
556
- render_time: number;
557
- full_time: number;
558
- request_cost: number;
559
- };
560
- }