@tcswap/toolboxes 4.3.15

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 (275) hide show
  1. package/dist/src/cardano/index.cjs +4 -0
  2. package/dist/src/cardano/index.cjs.map +11 -0
  3. package/dist/src/cardano/index.js +4 -0
  4. package/dist/src/cardano/index.js.map +11 -0
  5. package/dist/src/cosmos/index.cjs +4 -0
  6. package/dist/src/cosmos/index.cjs.map +20 -0
  7. package/dist/src/cosmos/index.js +4 -0
  8. package/dist/src/cosmos/index.js.map +20 -0
  9. package/dist/src/evm/index.cjs +4 -0
  10. package/dist/src/evm/index.cjs.map +20 -0
  11. package/dist/src/evm/index.js +4 -0
  12. package/dist/src/evm/index.js.map +20 -0
  13. package/dist/src/index.cjs +5 -0
  14. package/dist/src/index.cjs.map +67 -0
  15. package/dist/src/index.js +5 -0
  16. package/dist/src/index.js.map +67 -0
  17. package/dist/src/near/index.cjs +4 -0
  18. package/dist/src/near/index.cjs.map +16 -0
  19. package/dist/src/near/index.js +4 -0
  20. package/dist/src/near/index.js.map +16 -0
  21. package/dist/src/radix/index.cjs +4 -0
  22. package/dist/src/radix/index.cjs.map +10 -0
  23. package/dist/src/radix/index.js +4 -0
  24. package/dist/src/radix/index.js.map +10 -0
  25. package/dist/src/ripple/index.cjs +4 -0
  26. package/dist/src/ripple/index.cjs.map +10 -0
  27. package/dist/src/ripple/index.js +4 -0
  28. package/dist/src/ripple/index.js.map +10 -0
  29. package/dist/src/solana/index.cjs +4 -0
  30. package/dist/src/solana/index.cjs.map +11 -0
  31. package/dist/src/solana/index.js +4 -0
  32. package/dist/src/solana/index.js.map +11 -0
  33. package/dist/src/substrate/index.cjs +4 -0
  34. package/dist/src/substrate/index.cjs.map +13 -0
  35. package/dist/src/substrate/index.js +4 -0
  36. package/dist/src/substrate/index.js.map +13 -0
  37. package/dist/src/sui/index.cjs +4 -0
  38. package/dist/src/sui/index.cjs.map +11 -0
  39. package/dist/src/sui/index.js +4 -0
  40. package/dist/src/sui/index.js.map +11 -0
  41. package/dist/src/ton/index.cjs +4 -0
  42. package/dist/src/ton/index.cjs.map +11 -0
  43. package/dist/src/ton/index.js +4 -0
  44. package/dist/src/ton/index.js.map +11 -0
  45. package/dist/src/tron/index.cjs +4 -0
  46. package/dist/src/tron/index.cjs.map +13 -0
  47. package/dist/src/tron/index.js +4 -0
  48. package/dist/src/tron/index.js.map +13 -0
  49. package/dist/src/utxo/index.cjs +5 -0
  50. package/dist/src/utxo/index.cjs.map +21 -0
  51. package/dist/src/utxo/index.js +5 -0
  52. package/dist/src/utxo/index.js.map +21 -0
  53. package/dist/types/cardano/index.d.ts +3 -0
  54. package/dist/types/cardano/index.d.ts.map +1 -0
  55. package/dist/types/cardano/toolbox.d.ts +37 -0
  56. package/dist/types/cardano/toolbox.d.ts.map +1 -0
  57. package/dist/types/cardano/types.d.ts +11 -0
  58. package/dist/types/cardano/types.d.ts.map +1 -0
  59. package/dist/types/cosmos/index.d.ts +5 -0
  60. package/dist/types/cosmos/index.d.ts.map +1 -0
  61. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +8 -0
  62. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
  63. package/dist/types/cosmos/thorchainUtils/index.d.ts +5 -0
  64. package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
  65. package/dist/types/cosmos/thorchainUtils/messages.d.ts +211 -0
  66. package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
  67. package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
  68. package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
  69. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
  70. package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
  71. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +63 -0
  72. package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
  73. package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
  74. package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
  75. package/dist/types/cosmos/toolbox/cosmos.d.ts +65 -0
  76. package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
  77. package/dist/types/cosmos/toolbox/index.d.ts +18 -0
  78. package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
  79. package/dist/types/cosmos/toolbox/thorchain.d.ts +161 -0
  80. package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
  81. package/dist/types/cosmos/types.d.ts +49 -0
  82. package/dist/types/cosmos/types.d.ts.map +1 -0
  83. package/dist/types/cosmos/util.d.ts +77 -0
  84. package/dist/types/cosmos/util.d.ts.map +1 -0
  85. package/dist/types/evm/api.d.ts +8 -0
  86. package/dist/types/evm/api.d.ts.map +1 -0
  87. package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
  88. package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
  89. package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
  90. package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
  91. package/dist/types/evm/helpers.d.ts +6 -0
  92. package/dist/types/evm/helpers.d.ts.map +1 -0
  93. package/dist/types/evm/index.d.ts +5 -0
  94. package/dist/types/evm/index.d.ts.map +1 -0
  95. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +88 -0
  96. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
  97. package/dist/types/evm/toolbox/evm.d.ts +767 -0
  98. package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
  99. package/dist/types/evm/toolbox/index.d.ts +7 -0
  100. package/dist/types/evm/toolbox/index.d.ts.map +1 -0
  101. package/dist/types/evm/toolbox/op.d.ts +67 -0
  102. package/dist/types/evm/toolbox/op.d.ts.map +1 -0
  103. package/dist/types/evm/types.d.ts +108 -0
  104. package/dist/types/evm/types.d.ts.map +1 -0
  105. package/dist/types/index.d.ts +78 -0
  106. package/dist/types/index.d.ts.map +1 -0
  107. package/dist/types/near/helpers/core.d.ts +18 -0
  108. package/dist/types/near/helpers/core.d.ts.map +1 -0
  109. package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
  110. package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
  111. package/dist/types/near/helpers/nep141.d.ts +36 -0
  112. package/dist/types/near/helpers/nep141.d.ts.map +1 -0
  113. package/dist/types/near/index.d.ts +10 -0
  114. package/dist/types/near/index.d.ts.map +1 -0
  115. package/dist/types/near/toolbox.d.ts +35 -0
  116. package/dist/types/near/toolbox.d.ts.map +1 -0
  117. package/dist/types/near/types/contract.d.ts +38 -0
  118. package/dist/types/near/types/contract.d.ts.map +1 -0
  119. package/dist/types/near/types/nep141.d.ts +29 -0
  120. package/dist/types/near/types/nep141.d.ts.map +1 -0
  121. package/dist/types/near/types/toolbox.d.ts +51 -0
  122. package/dist/types/near/types/toolbox.d.ts.map +1 -0
  123. package/dist/types/near/types.d.ts +47 -0
  124. package/dist/types/near/types.d.ts.map +1 -0
  125. package/dist/types/radix/index.d.ts +17 -0
  126. package/dist/types/radix/index.d.ts.map +1 -0
  127. package/dist/types/ripple/index.d.ts +49 -0
  128. package/dist/types/ripple/index.d.ts.map +1 -0
  129. package/dist/types/solana/index.d.ts +23 -0
  130. package/dist/types/solana/index.d.ts.map +1 -0
  131. package/dist/types/solana/toolbox.d.ts +54 -0
  132. package/dist/types/solana/toolbox.d.ts.map +1 -0
  133. package/dist/types/substrate/balance.d.ts +20 -0
  134. package/dist/types/substrate/balance.d.ts.map +1 -0
  135. package/dist/types/substrate/index.d.ts +3 -0
  136. package/dist/types/substrate/index.d.ts.map +1 -0
  137. package/dist/types/substrate/substrate.d.ts +151 -0
  138. package/dist/types/substrate/substrate.d.ts.map +1 -0
  139. package/dist/types/substrate/types.d.ts +100 -0
  140. package/dist/types/substrate/types.d.ts.map +1 -0
  141. package/dist/types/sui/index.d.ts +3 -0
  142. package/dist/types/sui/index.d.ts.map +1 -0
  143. package/dist/types/sui/toolbox.d.ts +22 -0
  144. package/dist/types/sui/toolbox.d.ts.map +1 -0
  145. package/dist/types/sui/types.d.ts +16 -0
  146. package/dist/types/sui/types.d.ts.map +1 -0
  147. package/dist/types/ton/index.d.ts +3 -0
  148. package/dist/types/ton/index.d.ts.map +1 -0
  149. package/dist/types/ton/toolbox.d.ts +17 -0
  150. package/dist/types/ton/toolbox.d.ts.map +1 -0
  151. package/dist/types/ton/types.d.ts +22 -0
  152. package/dist/types/ton/types.d.ts.map +1 -0
  153. package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
  154. package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
  155. package/dist/types/tron/helpers/trongrid.d.ts +11 -0
  156. package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
  157. package/dist/types/tron/index.d.ts +6 -0
  158. package/dist/types/tron/index.d.ts.map +1 -0
  159. package/dist/types/tron/toolbox.d.ts +29 -0
  160. package/dist/types/tron/toolbox.d.ts.map +1 -0
  161. package/dist/types/tron/types.d.ts +103 -0
  162. package/dist/types/tron/types.d.ts.map +1 -0
  163. package/dist/types/types.d.ts +26 -0
  164. package/dist/types/types.d.ts.map +1 -0
  165. package/dist/types/utils.d.ts +7 -0
  166. package/dist/types/utils.d.ts.map +1 -0
  167. package/dist/types/utxo/helpers/api.d.ts +104 -0
  168. package/dist/types/utxo/helpers/api.d.ts.map +1 -0
  169. package/dist/types/utxo/helpers/bchaddrjs.d.ts +13 -0
  170. package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
  171. package/dist/types/utxo/helpers/coinselect.d.ts +20 -0
  172. package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
  173. package/dist/types/utxo/helpers/index.d.ts +5 -0
  174. package/dist/types/utxo/helpers/index.d.ts.map +1 -0
  175. package/dist/types/utxo/helpers/txSize.d.ts +24 -0
  176. package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
  177. package/dist/types/utxo/index.d.ts +7 -0
  178. package/dist/types/utxo/index.d.ts.map +1 -0
  179. package/dist/types/utxo/toolbox/bitcoinCash.d.ts +96 -0
  180. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
  181. package/dist/types/utxo/toolbox/index.d.ts +31 -0
  182. package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
  183. package/dist/types/utxo/toolbox/params.d.ts +32 -0
  184. package/dist/types/utxo/toolbox/params.d.ts.map +1 -0
  185. package/dist/types/utxo/toolbox/utxo.d.ts +106 -0
  186. package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
  187. package/dist/types/utxo/toolbox/validators.d.ts +4 -0
  188. package/dist/types/utxo/toolbox/validators.d.ts.map +1 -0
  189. package/dist/types/utxo/toolbox/zcash.d.ts +75 -0
  190. package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
  191. package/dist/types/utxo/types.d.ts +46 -0
  192. package/dist/types/utxo/types.d.ts.map +1 -0
  193. package/package.json +205 -0
  194. package/src/__tests__/address-validation-all-chains.test.ts +162 -0
  195. package/src/__tests__/addressValidator.test.ts +162 -0
  196. package/src/cardano/__tests__/toolbox.test.ts +48 -0
  197. package/src/cardano/index.ts +2 -0
  198. package/src/cardano/toolbox.ts +172 -0
  199. package/src/cardano/types.ts +10 -0
  200. package/src/cosmos/__tests__/toolbox.test.ts +95 -0
  201. package/src/cosmos/index.ts +4 -0
  202. package/src/cosmos/thorchainUtils/addressFormat.ts +26 -0
  203. package/src/cosmos/thorchainUtils/index.ts +4 -0
  204. package/src/cosmos/thorchainUtils/messages.ts +216 -0
  205. package/src/cosmos/thorchainUtils/registry.ts +43 -0
  206. package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +2800 -0
  207. package/src/cosmos/thorchainUtils/types/client-types.ts +54 -0
  208. package/src/cosmos/thorchainUtils/types/index.ts +1 -0
  209. package/src/cosmos/toolbox/cosmos.ts +354 -0
  210. package/src/cosmos/toolbox/index.ts +39 -0
  211. package/src/cosmos/toolbox/thorchain.ts +253 -0
  212. package/src/cosmos/types.ts +48 -0
  213. package/src/cosmos/util.ts +218 -0
  214. package/src/evm/__tests__/address-validation.test.ts +84 -0
  215. package/src/evm/__tests__/ethereum.test.ts +141 -0
  216. package/src/evm/__tests__/signMessage.test.ts +60 -0
  217. package/src/evm/api.ts +10 -0
  218. package/src/evm/contracts/eth/multicall.ts +165 -0
  219. package/src/evm/contracts/op/gasOracle.ts +145 -0
  220. package/src/evm/helpers.ts +71 -0
  221. package/src/evm/index.ts +4 -0
  222. package/src/evm/toolbox/baseEVMToolbox.ts +701 -0
  223. package/src/evm/toolbox/evm.ts +66 -0
  224. package/src/evm/toolbox/index.ts +44 -0
  225. package/src/evm/toolbox/op.ts +147 -0
  226. package/src/evm/types.ts +146 -0
  227. package/src/index.ts +264 -0
  228. package/src/near/__tests__/core.test.ts +70 -0
  229. package/src/near/helpers/core.ts +89 -0
  230. package/src/near/helpers/gasEstimation.ts +96 -0
  231. package/src/near/helpers/nep141.ts +50 -0
  232. package/src/near/index.ts +21 -0
  233. package/src/near/toolbox.ts +425 -0
  234. package/src/near/types/contract.ts +32 -0
  235. package/src/near/types/nep141.ts +34 -0
  236. package/src/near/types/toolbox.ts +55 -0
  237. package/src/near/types.ts +44 -0
  238. package/src/radix/index.ts +142 -0
  239. package/src/ripple/index.ts +180 -0
  240. package/src/solana/index.ts +36 -0
  241. package/src/solana/toolbox.ts +413 -0
  242. package/src/substrate/balance.ts +92 -0
  243. package/src/substrate/index.ts +2 -0
  244. package/src/substrate/substrate.ts +285 -0
  245. package/src/substrate/types.ts +115 -0
  246. package/src/sui/__tests__/toolbox.test.ts +86 -0
  247. package/src/sui/index.ts +2 -0
  248. package/src/sui/toolbox.ts +170 -0
  249. package/src/sui/types.ts +11 -0
  250. package/src/ton/__tests__/toolbox.test.ts +67 -0
  251. package/src/ton/index.ts +2 -0
  252. package/src/ton/toolbox.ts +140 -0
  253. package/src/ton/types.ts +13 -0
  254. package/src/tron/__tests__/toolbox.test.ts +225 -0
  255. package/src/tron/helpers/trc20.abi.ts +107 -0
  256. package/src/tron/helpers/trongrid.ts +57 -0
  257. package/src/tron/index.ts +21 -0
  258. package/src/tron/toolbox.ts +589 -0
  259. package/src/tron/types.ts +83 -0
  260. package/src/types.ts +28 -0
  261. package/src/utils.ts +38 -0
  262. package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
  263. package/src/utxo/helpers/api.ts +475 -0
  264. package/src/utxo/helpers/bchaddrjs.ts +170 -0
  265. package/src/utxo/helpers/coinselect.ts +96 -0
  266. package/src/utxo/helpers/index.ts +4 -0
  267. package/src/utxo/helpers/txSize.ts +141 -0
  268. package/src/utxo/index.ts +6 -0
  269. package/src/utxo/toolbox/bitcoinCash.ts +247 -0
  270. package/src/utxo/toolbox/index.ts +63 -0
  271. package/src/utxo/toolbox/params.ts +18 -0
  272. package/src/utxo/toolbox/utxo.ts +443 -0
  273. package/src/utxo/toolbox/validators.ts +36 -0
  274. package/src/utxo/toolbox/zcash.ts +246 -0
  275. package/src/utxo/types.ts +39 -0
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Modifications © 2025 Horizontal Systems.
3
+ */
4
+
5
+ import type {
6
+ FungibleResourcesCollectionItem,
7
+ GatewayApiClient,
8
+ StateEntityDetailsVaultResponseItem,
9
+ StateEntityFungiblesPageRequest,
10
+ StateEntityFungiblesPageResponse,
11
+ } from "@radixdlt/babylon-gateway-api-sdk";
12
+ import { AssetValue, Chain, USwapConfig, type USwapConfigIntegrations, USwapError } from "@tcswap/helpers";
13
+
14
+ export type RadixWallet = Awaited<ReturnType<typeof RadixToolbox>>;
15
+
16
+ type RadixGetBalanceParams = { address: string; networkApi: GatewayApiClient };
17
+ // Could not find anything sync in SDK, ask Radix team
18
+ export function radixValidateAddress(address: string) {
19
+ return address.startsWith("account_rdx1") && address.length === 66;
20
+ }
21
+
22
+ function getBalance({ networkApi }: { networkApi: GatewayApiClient }) {
23
+ return async function getBalance(address: string) {
24
+ const fungibleResources = await fetchFungibleResources({ address, networkApi });
25
+ const fungibleBalances = await convertResourcesToBalances({ networkApi, resources: fungibleResources });
26
+
27
+ const hasNativeAsset = fungibleBalances.some((asset) => asset.isGasAsset);
28
+ if (!hasNativeAsset) {
29
+ return [AssetValue.from({ chain: Chain.Radix }), ...fungibleBalances];
30
+ }
31
+
32
+ return fungibleBalances;
33
+ };
34
+ }
35
+
36
+ async function fetchFungibleResources({
37
+ address,
38
+ networkApi,
39
+ }: RadixGetBalanceParams): Promise<FungibleResourcesCollectionItem[]> {
40
+ let hasNextPage = true;
41
+ let nextCursor: string | undefined;
42
+ let fungibleResources: FungibleResourcesCollectionItem[] = [];
43
+ const stateVersion = await currentStateVersion(networkApi);
44
+ while (hasNextPage) {
45
+ const stateEntityFungiblesPageRequest: StateEntityFungiblesPageRequest = {
46
+ address: address,
47
+ at_ledger_state: { state_version: stateVersion },
48
+ cursor: nextCursor,
49
+ limit_per_page: 100,
50
+ };
51
+
52
+ const stateEntityFungiblesPageResponse: StateEntityFungiblesPageResponse =
53
+ await networkApi.state.innerClient.entityFungiblesPage({
54
+ stateEntityFungiblesPageRequest: stateEntityFungiblesPageRequest,
55
+ });
56
+
57
+ fungibleResources = fungibleResources.concat(stateEntityFungiblesPageResponse.items);
58
+ if (stateEntityFungiblesPageResponse.next_cursor) {
59
+ nextCursor = stateEntityFungiblesPageResponse.next_cursor;
60
+ } else {
61
+ hasNextPage = false;
62
+ }
63
+ }
64
+ return fungibleResources;
65
+ }
66
+
67
+ async function convertResourcesToBalances({
68
+ resources,
69
+ networkApi,
70
+ }: {
71
+ resources: FungibleResourcesCollectionItem[]; //| NonFungibleResourcesCollectionItem[];
72
+ networkApi: GatewayApiClient;
73
+ }): Promise<AssetValue[]> {
74
+ const balances: AssetValue[] = [];
75
+ const BATCH_SIZE = 50;
76
+
77
+ // Split resources into batches of up to 50 items
78
+ const resourceBatches: FungibleResourcesCollectionItem[][] = [];
79
+ for (let i = 0; i < resources.length; i += BATCH_SIZE) {
80
+ resourceBatches.push(resources.slice(i, i + BATCH_SIZE));
81
+ }
82
+
83
+ for (const batch of resourceBatches) {
84
+ const addresses = batch.map((item) => item.resource_address);
85
+ const response: StateEntityDetailsVaultResponseItem[] =
86
+ await networkApi.state.getEntityDetailsVaultAggregated(addresses);
87
+
88
+ const divisibilities = new Map<string, { decimals: number; symbol: string }>();
89
+
90
+ for (const result of response) {
91
+ if (result.details !== undefined) {
92
+ const metaDataSymbol = result.metadata?.items.find((item) => item.key === "symbol");
93
+ const symbol = metaDataSymbol?.value.typed.type === "String" ? metaDataSymbol.value.typed.value : "?";
94
+
95
+ if (result.details.type === "FungibleResource") {
96
+ divisibilities.set(result.address, { decimals: result.details.divisibility, symbol });
97
+ }
98
+ }
99
+ }
100
+
101
+ for (const item of batch) {
102
+ if (item.aggregation_level === "Global") {
103
+ const assetInfo = divisibilities.get(item.resource_address) || { decimals: 0, symbol: "?" };
104
+
105
+ const balance = AssetValue.from({
106
+ asset:
107
+ assetInfo.symbol !== Chain.Radix
108
+ ? `${Chain.Radix}.${assetInfo.symbol}-${item.resource_address}`
109
+ : "XRD.XRD",
110
+ value: item.amount,
111
+ });
112
+ balances.push(balance);
113
+ }
114
+ }
115
+ }
116
+
117
+ return balances;
118
+ }
119
+
120
+ function currentStateVersion(networkApi: GatewayApiClient) {
121
+ return networkApi.status.getCurrent().then((status) => status.ledger_state.state_version);
122
+ }
123
+
124
+ export const RadixToolbox = async ({ dappConfig }: { dappConfig?: USwapConfigIntegrations["radix"] } = {}) => {
125
+ const { RadixDappToolkit } = await import("@radixdlt/radix-dapp-toolkit");
126
+ const { GatewayApiClient } = await import("@radixdlt/babylon-gateway-api-sdk");
127
+ const config = dappConfig || USwapConfig.get("integrations").radix;
128
+
129
+ const radixToolkit = RadixDappToolkit({ ...config, networkId: config.network?.networkId || 1 });
130
+
131
+ const networkApi = GatewayApiClient.initialize(radixToolkit.gatewayApi.clientConfig);
132
+
133
+ return {
134
+ getAddress: () => "",
135
+ getBalance: getBalance({ networkApi }),
136
+ networkApi,
137
+ signAndBroadcast: (() => {
138
+ throw new USwapError("toolbox_radix_method_not_supported", { method: "signAndBroadcast" });
139
+ }) as (params: any) => Promise<string>,
140
+ validateAddress: radixValidateAddress,
141
+ };
142
+ };
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Modifications © 2025 Horizontal Systems.
3
+ */
4
+
5
+ import {
6
+ AssetValue,
7
+ Chain,
8
+ type ChainSigner,
9
+ type GenericTransferParams,
10
+ getChainConfig,
11
+ getRPCUrl,
12
+ USwapError,
13
+ USwapNumber,
14
+ } from "@tcswap/helpers";
15
+ import type { Transaction } from "xrpl";
16
+ import { Client, isValidAddress, type Payment, Wallet, xrpToDrops } from "xrpl";
17
+
18
+ export type RippleWallet = Awaited<ReturnType<typeof getRippleToolbox>>;
19
+
20
+ export { hashes, type Transaction } from "xrpl";
21
+
22
+ const RIPPLE_ERROR_CODES = { ACCOUNT_NOT_FOUND: 19 } as const;
23
+
24
+ // Note: Ripple seeds generate a single address, no derivation path/index support.
25
+ function createSigner(phrase: string): ChainSigner<Transaction, { tx_blob: string; hash: string }> {
26
+ const wallet = Wallet.fromMnemonic(phrase);
27
+ return {
28
+ // publicKey: wallet.publicKey,
29
+ // Address is sync, but interface requires async
30
+ getAddress: () => Promise.resolve(wallet.address),
31
+ // Signing is sync, but interface requires async
32
+ signTransaction: (tx: Transaction) => Promise.resolve(wallet.sign(tx as Transaction)), // Cast needed as Wallet.sign expects Transaction
33
+ };
34
+ }
35
+
36
+ export function rippleValidateAddress(address: string) {
37
+ return isValidAddress(address);
38
+ }
39
+
40
+ type RippleToolboxParams =
41
+ | { phrase?: string }
42
+ | { signer?: ChainSigner<Transaction, { tx_blob: string; hash: string }> };
43
+
44
+ export const getRippleToolbox = async (params: RippleToolboxParams = {}) => {
45
+ const signer =
46
+ "signer" in params && params.signer
47
+ ? params.signer
48
+ : "phrase" in params && params.phrase
49
+ ? createSigner(params.phrase)
50
+ : undefined;
51
+
52
+ const rpcUrl = await getRPCUrl(Chain.Ripple);
53
+ if (!rpcUrl) {
54
+ throw new USwapError({ errorKey: "toolbox_ripple_rpc_not_configured", info: { chain: Chain.Ripple } });
55
+ }
56
+
57
+ const client = new Client(rpcUrl);
58
+ await client.connect();
59
+
60
+ const getAddress = () => {
61
+ if (!signer) {
62
+ throw new USwapError({ errorKey: "toolbox_ripple_signer_not_found" });
63
+ }
64
+ return signer.getAddress();
65
+ };
66
+
67
+ const getBalance = async (address?: string) => {
68
+ const addr = address || (await getAddress());
69
+ const { baseDecimal } = getChainConfig(Chain.Ripple);
70
+
71
+ try {
72
+ const accountInfo = await client.request({ account: addr, command: "account_info" });
73
+
74
+ const balance = accountInfo.result.account_data.Balance;
75
+
76
+ return [AssetValue.from({ chain: Chain.Ripple, fromBaseDecimal: baseDecimal, value: balance })];
77
+ } catch (error) {
78
+ // empty account
79
+ if ((error as any).data.error_code === RIPPLE_ERROR_CODES.ACCOUNT_NOT_FOUND) {
80
+ return [AssetValue.from({ chain: Chain.Ripple, value: 0 })];
81
+ }
82
+ throw new USwapError("toolbox_ripple_get_balance_error", { info: { address: addr, error } });
83
+ }
84
+ };
85
+
86
+ const estimateTransactionFee = async () => {
87
+ const feeResponse = await client.request({ command: "fee" });
88
+ const feeDrops = feeResponse.result.drops.open_ledger_fee; // Fee in drops
89
+
90
+ const { baseDecimal } = getChainConfig(Chain.Ripple);
91
+
92
+ return AssetValue.from({
93
+ chain: Chain.Ripple,
94
+ fromBaseDecimal: baseDecimal,
95
+ value: USwapNumber.fromBigInt(BigInt(feeDrops), baseDecimal),
96
+ });
97
+ };
98
+
99
+ const createTransaction = async ({
100
+ assetValue,
101
+ recipient,
102
+ memo,
103
+ sender,
104
+ }: {
105
+ assetValue: AssetValue;
106
+ recipient: string;
107
+ sender?: string;
108
+ memo?: string;
109
+ }) => {
110
+ if (!rippleValidateAddress(recipient)) {
111
+ throw new USwapError({ errorKey: "core_transaction_invalid_recipient_address" });
112
+ }
113
+
114
+ const senderAddress = sender || (await getAddress());
115
+
116
+ if (!assetValue.isGasAsset || assetValue.chain !== Chain.Ripple) {
117
+ throw new USwapError({ errorKey: "toolbox_ripple_asset_not_supported", info: { asset: assetValue.toString() } });
118
+ }
119
+
120
+ const transaction: Payment = {
121
+ Account: senderAddress,
122
+ Amount: xrpToDrops(assetValue.getValue("string")),
123
+ Destination: recipient,
124
+ TransactionType: "Payment",
125
+ };
126
+
127
+ if (memo) {
128
+ transaction.Memos = [{ Memo: { MemoData: Buffer.from(memo).toString("hex") } }];
129
+ }
130
+
131
+ const preparedTx = await client.autofill(transaction);
132
+ return preparedTx;
133
+ };
134
+
135
+ const signTransaction = (tx: Transaction) => {
136
+ if (!signer) {
137
+ throw new USwapError({ errorKey: "toolbox_ripple_signer_not_found" });
138
+ }
139
+ return signer.signTransaction(tx);
140
+ };
141
+
142
+ const broadcastTransaction = async (signedTxHex: string) => {
143
+ const submitResult = await client.submitAndWait(signedTxHex);
144
+ const result = submitResult.result;
145
+
146
+ if (result.validated) {
147
+ return result.hash;
148
+ }
149
+
150
+ throw new USwapError({ errorKey: "toolbox_ripple_broadcast_error", info: { chain: Chain.Ripple } });
151
+ };
152
+
153
+ const transfer = async (params: GenericTransferParams) => {
154
+ if (!signer) {
155
+ throw new USwapError({ errorKey: "toolbox_ripple_signer_not_found" });
156
+ }
157
+ const sender = await signer.getAddress();
158
+ const tx = await createTransaction({ ...params, sender });
159
+ const signedTx = await signTransaction(tx);
160
+ return broadcastTransaction(signedTx.tx_blob);
161
+ };
162
+
163
+ const disconnect = () => client.disconnect();
164
+
165
+ return {
166
+ broadcastTransaction,
167
+ createSigner, // Expose the helper
168
+ createTransaction,
169
+ disconnect,
170
+ estimateTransactionFee,
171
+ // Core methods
172
+ getAddress,
173
+ getBalance,
174
+ // Signer related
175
+ signer, // Expose the signer instance if created/provided
176
+ signTransaction,
177
+ transfer,
178
+ validateAddress: rippleValidateAddress,
179
+ };
180
+ };
@@ -0,0 +1,36 @@
1
+ import type { PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
2
+ import type { GenericCreateTransactionParams, GenericTransferParams } from "@tcswap/helpers";
3
+ import type { getSolanaToolbox } from "./toolbox";
4
+
5
+ // type DisplayEncoding = "utf8" | "hex";
6
+
7
+ // type PhantomRequestMethod =
8
+ // | "connect"
9
+ // | "disconnect"
10
+ // | "signAndSendTransaction"
11
+ // | "signAndSendTransactionV0"
12
+ // | "signAndSendTransactionV0WithLookupTable"
13
+ // | "signTransaction"
14
+ // | "signAllTransactions"
15
+ // | "signMessage";
16
+
17
+ interface ConnectOpts {
18
+ onlyIfTrusted: boolean;
19
+ }
20
+
21
+ export * from "./toolbox";
22
+
23
+ export type SolanaWallet = Awaited<ReturnType<typeof getSolanaToolbox>>;
24
+
25
+ export interface SolanaProvider {
26
+ connect: (opts?: Partial<ConnectOpts>) => Promise<{ publicKey: PublicKey }>;
27
+ disconnect: () => Promise<void>;
28
+ publicKey: PublicKey | null;
29
+ signTransaction: <T extends Transaction | VersionedTransaction = Transaction>(transaction: T) => Promise<T>;
30
+ }
31
+
32
+ export type SolanaCreateTransactionParams = Omit<GenericCreateTransactionParams, "feeRate"> & {
33
+ isProgramDerivedAddress?: boolean;
34
+ };
35
+
36
+ export type SolanaTransferParams = Omit<GenericTransferParams, "feeRate"> & { isProgramDerivedAddress?: boolean };