@uswap/toolboxes 4.3.6

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 +34 -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 +5 -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 +208 -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 +62 -0
  76. package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
  77. package/dist/types/cosmos/toolbox/index.d.ts +15 -0
  78. package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
  79. package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -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 +74 -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 +83 -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 +76 -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 +75 -0
  106. package/dist/types/index.d.ts.map +1 -0
  107. package/dist/types/near/helpers/core.d.ts +15 -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 +32 -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 +14 -0
  126. package/dist/types/radix/index.d.ts.map +1 -0
  127. package/dist/types/ripple/index.d.ts +46 -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 +51 -0
  132. package/dist/types/solana/toolbox.d.ts.map +1 -0
  133. package/dist/types/substrate/balance.d.ts +17 -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 +148 -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 +19 -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 +14 -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 +8 -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 +26 -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 +4 -0
  166. package/dist/types/utils.d.ts.map +1 -0
  167. package/dist/types/utxo/helpers/api.d.ts +101 -0
  168. package/dist/types/utxo/helpers/api.d.ts.map +1 -0
  169. package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
  170. package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
  171. package/dist/types/utxo/helpers/coinselect.d.ts +17 -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 +21 -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 +93 -0
  180. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
  181. package/dist/types/utxo/toolbox/index.d.ts +28 -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 +103 -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 +72 -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 +168 -0
  199. package/src/cardano/types.ts +10 -0
  200. package/src/cosmos/__tests__/toolbox.test.ts +91 -0
  201. package/src/cosmos/index.ts +4 -0
  202. package/src/cosmos/thorchainUtils/addressFormat.ts +22 -0
  203. package/src/cosmos/thorchainUtils/index.ts +4 -0
  204. package/src/cosmos/thorchainUtils/messages.ts +212 -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 +345 -0
  210. package/src/cosmos/toolbox/index.ts +35 -0
  211. package/src/cosmos/toolbox/thorchain.ts +249 -0
  212. package/src/cosmos/types.ts +48 -0
  213. package/src/cosmos/util.ts +214 -0
  214. package/src/evm/__tests__/address-validation.test.ts +84 -0
  215. package/src/evm/__tests__/ethereum.test.ts +137 -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 +73 -0
  221. package/src/evm/index.ts +4 -0
  222. package/src/evm/toolbox/baseEVMToolbox.ts +695 -0
  223. package/src/evm/toolbox/evm.ts +67 -0
  224. package/src/evm/toolbox/index.ts +44 -0
  225. package/src/evm/toolbox/op.ts +156 -0
  226. package/src/evm/types.ts +146 -0
  227. package/src/index.ts +260 -0
  228. package/src/near/__tests__/core.test.ts +70 -0
  229. package/src/near/helpers/core.ts +85 -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 +421 -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 +132 -0
  239. package/src/ripple/index.ts +179 -0
  240. package/src/solana/index.ts +36 -0
  241. package/src/solana/toolbox.ts +415 -0
  242. package/src/substrate/balance.ts +88 -0
  243. package/src/substrate/index.ts +2 -0
  244. package/src/substrate/substrate.ts +281 -0
  245. package/src/substrate/types.ts +115 -0
  246. package/src/sui/__tests__/toolbox.test.ts +82 -0
  247. package/src/sui/index.ts +2 -0
  248. package/src/sui/toolbox.ts +165 -0
  249. package/src/sui/types.ts +11 -0
  250. package/src/ton/__tests__/toolbox.test.ts +63 -0
  251. package/src/ton/index.ts +2 -0
  252. package/src/ton/toolbox.ts +136 -0
  253. package/src/ton/types.ts +13 -0
  254. package/src/tron/__tests__/toolbox.test.ts +221 -0
  255. package/src/tron/helpers/trc20.abi.ts +107 -0
  256. package/src/tron/helpers/trongrid.ts +53 -0
  257. package/src/tron/index.ts +21 -0
  258. package/src/tron/toolbox.ts +585 -0
  259. package/src/tron/types.ts +83 -0
  260. package/src/types.ts +28 -0
  261. package/src/utils.ts +27 -0
  262. package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
  263. package/src/utxo/helpers/api.ts +471 -0
  264. package/src/utxo/helpers/bchaddrjs.ts +166 -0
  265. package/src/utxo/helpers/coinselect.ts +92 -0
  266. package/src/utxo/helpers/index.ts +4 -0
  267. package/src/utxo/helpers/txSize.ts +137 -0
  268. package/src/utxo/index.ts +6 -0
  269. package/src/utxo/toolbox/bitcoinCash.ts +243 -0
  270. package/src/utxo/toolbox/index.ts +59 -0
  271. package/src/utxo/toolbox/params.ts +18 -0
  272. package/src/utxo/toolbox/utxo.ts +439 -0
  273. package/src/utxo/toolbox/validators.ts +36 -0
  274. package/src/utxo/toolbox/zcash.ts +245 -0
  275. package/src/utxo/types.ts +39 -0
@@ -0,0 +1,48 @@
1
+ import type { OfflineAminoSigner } from "@cosmjs/amino";
2
+ import type { DirectSecp256k1HdWallet, OfflineDirectSigner } from "@cosmjs/proto-signing";
3
+ import type {
4
+ ChainId,
5
+ CosmosChain,
6
+ DerivationPathArray,
7
+ GenericCreateTransactionParams,
8
+ TCLikeChain,
9
+ } from "@uswap/helpers";
10
+ import type { buildAminoMsg } from "./thorchainUtils";
11
+ import type { createCosmosToolbox } from "./toolbox/cosmos";
12
+ import type { createThorchainToolbox } from "./toolbox/thorchain";
13
+ import type { getDefaultChainFee } from "./util";
14
+
15
+ export type CosmosSDKClientParams = { server: string; chainId: ChainId; prefix?: string; stagenet?: boolean };
16
+ export type CosmosCreateTransactionParams = GenericCreateTransactionParams & {
17
+ accountNumber?: number;
18
+ sequence?: number;
19
+ };
20
+
21
+ export type MultiSigSigner = { pubKey: string; signature: string };
22
+
23
+ export type MultisigTx = {
24
+ msgs: ReturnType<typeof buildAminoMsg>[];
25
+ accountNumber: number;
26
+ sequence: number;
27
+ chainId: ChainId;
28
+ fee: ReturnType<typeof getDefaultChainFee>;
29
+ memo: string;
30
+ };
31
+
32
+ export type CosmosSigner = DirectSecp256k1HdWallet | OfflineDirectSigner | OfflineAminoSigner;
33
+
34
+ export type CosmosToolboxParams<T = CosmosChain> = { chain: T } & (
35
+ | { signer?: CosmosSigner }
36
+ | { phrase?: string; derivationPath?: DerivationPathArray; index?: number }
37
+ );
38
+
39
+ export type BaseCosmosToolboxType = ReturnType<typeof createCosmosToolbox>;
40
+ export type BaseCosmosWallet = Awaited<ReturnType<typeof createCosmosToolbox>>;
41
+ export type CosmosWallets = {
42
+ [chain in Exclude<CosmosChain, TCLikeChain>]: BaseCosmosWallet;
43
+ };
44
+
45
+ export type ThorchainWallet = Awaited<Omit<ReturnType<typeof createThorchainToolbox>, "signMessage">>;
46
+ export type ThorchainWallets = {
47
+ [chain in TCLikeChain]: ThorchainWallet;
48
+ };
@@ -0,0 +1,214 @@
1
+ import type { OfflineSigner } from "@cosmjs/proto-signing";
2
+ import type { SigningStargateClientOptions } from "@cosmjs/stargate";
3
+ import { AssetValue, Chain, type CosmosChain, getChainConfig, getRPCUrl, SwapKitError } from "@uswap/helpers";
4
+ import type { CosmosCreateTransactionParams } from "./types";
5
+
6
+ export const USK_KUJIRA_FACTORY_DENOM =
7
+ "FACTORY/KUJIRA1QK00H5ATUTPSV900X202PXX42NPJR9THG58DNQPA72F2P7M2LUASE444A7/UUSK";
8
+
9
+ export const YUM_KUJIRA_FACTORY_DENOM = "FACTORY/KUJIRA1YGFXN0ER40KLCNCK8THLTUPRDXLCK6WVNPKF2K/UYUM";
10
+
11
+ export const DEFAULT_COSMOS_FEE_MAINNET = { amount: [{ amount: "500", denom: "uatom" }], gas: "200000" };
12
+
13
+ export const DEFAULT_KUJI_FEE_MAINNET = { amount: [{ amount: "1000", denom: "ukuji" }], gas: "200000" };
14
+
15
+ export const DEFAULT_NOBLE_FEE_MAINNET = { amount: [{ amount: "1000", denom: "uusdc" }], gas: "200000" };
16
+
17
+ export function getDefaultChainFee(chain: CosmosChain) {
18
+ switch (chain) {
19
+ case Chain.Maya:
20
+ return { amount: [], gas: "10000000000" };
21
+ case Chain.THORChain:
22
+ return { amount: [], gas: "500000000" };
23
+ case Chain.Kujira:
24
+ return DEFAULT_KUJI_FEE_MAINNET;
25
+ case Chain.Noble:
26
+ return DEFAULT_NOBLE_FEE_MAINNET;
27
+ default:
28
+ return DEFAULT_COSMOS_FEE_MAINNET;
29
+ }
30
+ }
31
+
32
+ export const getMsgSendDenom = (symbol: string, isThorchain = false) => {
33
+ if (isThorchain) {
34
+ return symbol.toLowerCase();
35
+ }
36
+
37
+ switch (symbol) {
38
+ case "uUSK":
39
+ case "USK":
40
+ return USK_KUJIRA_FACTORY_DENOM;
41
+ case "uYUM":
42
+ case "YUM":
43
+ return YUM_KUJIRA_FACTORY_DENOM;
44
+ case "uKUJI":
45
+ case "KUJI":
46
+ return "ukuji";
47
+ case "ATOM":
48
+ case "uATOM":
49
+ return "uatom";
50
+ case "uUSDC":
51
+ case "USDC":
52
+ return "uusdc";
53
+ default:
54
+ return symbol;
55
+ }
56
+ };
57
+
58
+ export const getDenomWithChain = ({ symbol, chain }: AssetValue) => {
59
+ if (chain === Chain.Maya) {
60
+ return (symbol.toUpperCase() !== "CACAO" ? symbol : `${Chain.Maya}.${symbol}`).toUpperCase();
61
+ }
62
+ if (chain === Chain.THORChain) {
63
+ return (
64
+ ["RUNE", "TCY", "RUJI"].includes(symbol.toUpperCase()) ? `${Chain.THORChain}.${symbol}` : symbol
65
+ ).toUpperCase();
66
+ }
67
+ return getMsgSendDenom(symbol, false);
68
+ };
69
+
70
+ export async function createStargateClient(url: string) {
71
+ const imported = await import("@cosmjs/stargate");
72
+
73
+ const StargateClient = imported.StargateClient ?? imported.default?.StargateClient;
74
+
75
+ const defaultRequestHeaders =
76
+ typeof window !== "undefined"
77
+ ? ({} as Record<string, string>)
78
+ : { referer: "https://sdk.swapkit.dev", referrer: "https://sdk.swapkit.dev" };
79
+
80
+ return StargateClient.connect({ headers: defaultRequestHeaders, url });
81
+ }
82
+
83
+ export async function createSigningStargateClient(
84
+ url: string,
85
+ signer: OfflineSigner,
86
+ optionsOrBaseGas: string | SigningStargateClientOptions = {},
87
+ ) {
88
+ const imported = await import("@cosmjs/stargate");
89
+ const SigningStargateClient = imported.SigningStargateClient ?? imported.default?.SigningStargateClient;
90
+ const GasPrice = imported.GasPrice ?? imported.default?.GasPrice;
91
+ const gasPrice = typeof optionsOrBaseGas === "string" ? optionsOrBaseGas : "0.0003uatom";
92
+ const options = typeof optionsOrBaseGas === "string" ? {} : optionsOrBaseGas;
93
+
94
+ return SigningStargateClient.connectWithSigner(url, signer, { gasPrice: GasPrice.fromString(gasPrice), ...options });
95
+ }
96
+
97
+ export async function createOfflineStargateClient(wallet: OfflineSigner, registry?: SigningStargateClientOptions) {
98
+ const imported = await import("@cosmjs/stargate");
99
+ const SigningStargateClient = imported.SigningStargateClient ?? imported.default?.SigningStargateClient;
100
+ return SigningStargateClient.offline(wallet, registry);
101
+ }
102
+
103
+ const getTransferMsgTypeByChain = (chain: CosmosChain) => {
104
+ switch (chain) {
105
+ case Chain.Maya:
106
+ case Chain.THORChain:
107
+ return "/types.MsgSend";
108
+ case Chain.Cosmos:
109
+ case Chain.Kujira:
110
+ case Chain.Noble:
111
+ return "/cosmos.bank.v1beta1.MsgSend";
112
+ default:
113
+ throw new SwapKitError("toolbox_cosmos_not_supported", { chain });
114
+ }
115
+ };
116
+
117
+ /**
118
+ * Used to build tx for Cosmos and Kujira
119
+ */
120
+ export async function cosmosCreateTransaction({
121
+ sender,
122
+ recipient,
123
+ assetValue,
124
+ memo = "",
125
+ feeRate,
126
+ sequence,
127
+ accountNumber,
128
+ }: CosmosCreateTransactionParams) {
129
+ const { chain, chainId } = assetValue;
130
+
131
+ const rpcUrl = await getRPCUrl(chain);
132
+ const client = await createStargateClient(rpcUrl);
133
+ const accountOnChain = await client.getAccount(sender);
134
+
135
+ if (!accountOnChain) {
136
+ throw new SwapKitError("toolbox_cosmos_account_not_found", { sender });
137
+ }
138
+
139
+ const gasAsset = AssetValue.from({ chain });
140
+ const feeAsset = getMsgSendDenom(gasAsset.symbol);
141
+ const defaultFee = getDefaultChainFee(chain as CosmosChain);
142
+
143
+ const txFee =
144
+ feeAsset && feeRate
145
+ ? { amount: [{ amount: feeRate.toString(), denom: feeAsset }], gas: defaultFee.gas }
146
+ : defaultFee;
147
+
148
+ const msgSend = {
149
+ amount: [{ amount: assetValue.getBaseValue("string"), denom: getMsgSendDenom(assetValue.symbol) }],
150
+ fromAddress: sender,
151
+ toAddress: recipient,
152
+ };
153
+
154
+ return {
155
+ accountNumber: accountNumber ?? accountOnChain.accountNumber,
156
+ chainId,
157
+ fee: txFee,
158
+ memo,
159
+ msgs: [{ typeUrl: getTransferMsgTypeByChain(chain as CosmosChain), value: msgSend }],
160
+ sequence: sequence ?? accountOnChain.sequence,
161
+ };
162
+ }
163
+
164
+ // Map of known denoms to their asset configurations
165
+ const DENOM_MAP = {
166
+ atom: { chain: Chain.Cosmos, decimals: getChainConfig(Chain.Cosmos).baseDecimal },
167
+
168
+ // Maya denoms
169
+ cacao: { chain: Chain.Maya, decimals: 10 }, // Maya uses 10 decimals for CACAO
170
+ kuji: { chain: Chain.Kujira, decimals: getChainConfig(Chain.Kujira).baseDecimal },
171
+ maya: { asset: `${Chain.Maya}.${Chain.Maya}`, decimals: 4 }, // MAYA token uses 4 decimals
172
+ // THORChain denoms
173
+ rune: { chain: Chain.THORChain, decimals: getChainConfig(Chain.THORChain).baseDecimal },
174
+ tcy: { asset: "THOR.TCY", decimals: getChainConfig(Chain.THORChain).baseDecimal },
175
+
176
+ // Cosmos denoms
177
+ uatom: { chain: Chain.Cosmos, decimals: getChainConfig(Chain.Cosmos).baseDecimal },
178
+
179
+ // Kujira denoms
180
+ ukuji: { chain: Chain.Kujira, decimals: getChainConfig(Chain.Kujira).baseDecimal },
181
+ usdc: { chain: Chain.Noble, decimals: getChainConfig(Chain.Noble).baseDecimal },
182
+
183
+ // Noble denoms
184
+ uusdc: { chain: Chain.Noble, decimals: getChainConfig(Chain.Noble).baseDecimal },
185
+ "x/kuji": { asset: "THOR.KUJI", decimals: getChainConfig(Chain.THORChain).baseDecimal },
186
+
187
+ // USK on Kujira (lowercase version of the factory denom)
188
+ [USK_KUJIRA_FACTORY_DENOM.toLowerCase()]: {
189
+ asset: `${Chain.Kujira}.USK`,
190
+ decimals: getChainConfig(Chain.Kujira).baseDecimal,
191
+ },
192
+ };
193
+
194
+ /**
195
+ * Converts a Cosmos denom and amount to an AssetValue with proper decimal handling
196
+ * @param denom - The denomination string
197
+ * @param amount - The amount in base units as a string
198
+ * @returns AssetValue with the correct decimal conversion
199
+ */
200
+ export const getAssetFromDenom = (denom: string, amount: string) => {
201
+ const config = DENOM_MAP[denom.toLowerCase()];
202
+
203
+ if (!config) {
204
+ // For unknown denoms, default to 8 decimals (common for many Cosmos chains)
205
+ // This preserves the original behavior while using fromBaseDecimal
206
+ return AssetValue.from({ asset: denom, fromBaseDecimal: 8, value: amount });
207
+ }
208
+
209
+ const { chain, asset, decimals } = config;
210
+
211
+ const assetOrChain = (chain ? { chain } : { asset }) as { asset: string } | { chain: CosmosChain };
212
+
213
+ return AssetValue.from({ ...assetOrChain, fromBaseDecimal: decimals, value: amount });
214
+ };
@@ -0,0 +1,84 @@
1
+ import { beforeAll, describe, expect, test } from "bun:test";
2
+ import { Chain } from "@uswap/helpers";
3
+ import { getEvmToolbox } from "../toolbox";
4
+
5
+ const context: { validateAddress: (address: string) => boolean } = {} as any;
6
+
7
+ beforeAll(async () => {
8
+ // Get EVM toolbox for address validation
9
+ const toolbox = await getEvmToolbox(Chain.Ethereum);
10
+ context.validateAddress = toolbox.validateAddress;
11
+ });
12
+
13
+ describe("EVM Address Validation", () => {
14
+ test("should validate valid EVM addresses", () => {
15
+ const validAddresses = [
16
+ "0xa052Ddf1c1739419B90FB7bf722843AD3e63114B", // User provided address
17
+ "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC contract
18
+ "0x6d6e022eE439C8aB8B7a7dBb0576f8090319CDc6", // Test address
19
+ "0xE29E61479420Dd1029A9946710Ac31A0d140e77F", // Another valid address
20
+ "0x0000000000000000000000000000000000000000", // Zero address
21
+ "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF", // Max address
22
+ "0x1234567890123456789012345678901234567890", // Mixed case
23
+ ];
24
+
25
+ for (const address of validAddresses) {
26
+ expect(context.validateAddress(address)).toBe(true);
27
+ }
28
+ });
29
+
30
+ test("should reject invalid EVM addresses", () => {
31
+ const invalidAddresses = [
32
+ "", // Empty string
33
+ "invalid", // Random string
34
+ "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", // TRON address
35
+ "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", // Bitcoin address
36
+ "cosmos1abc123", // Cosmos address
37
+ "0xa052Ddf1c1739419B90FB7bf722843AD3e63114", // Too short (missing 1 char)
38
+ "0xa052Ddf1c1739419B90FB7bf722843AD3e63114BB", // Too long (extra char)
39
+ "0XA052DDF1C1739419B90FB7BF722843AD3E63114B", // Uppercase 0X prefix
40
+ "0xG052Ddf1c1739419B90FB7bf722843AD3e63114B", // Invalid hex character
41
+ "0x", // Only prefix
42
+ ];
43
+
44
+ for (const address of invalidAddresses) {
45
+ expect(context.validateAddress(address)).toBe(false);
46
+ }
47
+ });
48
+
49
+ test("should handle case normalization properly", () => {
50
+ const address = "0xa052Ddf1c1739419B90FB7bf722843AD3e63114B"; // Proper checksum
51
+ const lowerCase = address.toLowerCase();
52
+
53
+ // Valid case variations should be accepted
54
+ expect(context.validateAddress(address)).toBe(true);
55
+ expect(context.validateAddress(lowerCase)).toBe(true);
56
+ });
57
+
58
+ test("should reject invalid checksum addresses", () => {
59
+ const invalidChecksumAddress = "0xA052dDF1C1739419b90fb7BF722843ad3E63114b"; // Invalid checksum
60
+
61
+ // Should reject mixed case with invalid checksum
62
+ expect(context.validateAddress(invalidChecksumAddress)).toBe(false);
63
+ });
64
+
65
+ test("should handle edge cases", () => {
66
+ const edgeCases = [null, undefined, 123, {}, [], true, false];
67
+
68
+ for (const testCase of edgeCases) {
69
+ // Should not throw but return false for invalid inputs
70
+ expect(context.validateAddress(testCase as any)).toBe(false);
71
+ }
72
+ });
73
+
74
+ test("should validate checksummed addresses", () => {
75
+ const checksummedAddresses = [
76
+ "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC with proper checksum
77
+ "0x6d6e022eE439C8aB8B7a7dBb0576f8090319CDc6", // Another checksummed address
78
+ ];
79
+
80
+ for (const address of checksummedAddresses) {
81
+ expect(context.validateAddress(address)).toBe(true);
82
+ }
83
+ });
84
+ });
@@ -0,0 +1,137 @@
1
+ import { beforeEach, describe, expect, test } from "bun:test";
2
+ import { existsSync } from "node:fs";
3
+ import { resolve } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { AssetValue, Chain, SKConfig } from "@uswap/helpers";
6
+ import { erc20ABI } from "@uswap/helpers/contracts";
7
+ import type { JsonRpcProvider, Signer } from "ethers";
8
+ import { getEvmToolbox } from "../toolbox";
9
+
10
+ // Import plugins for type augmentation
11
+ import "@nomicfoundation/hardhat-ethers";
12
+ import "@nomicfoundation/hardhat-network-helpers";
13
+
14
+ // Set Hardhat config path to work from both root and package folder
15
+ const __dirname = fileURLToPath(new URL(".", import.meta.url));
16
+ const packageHardhatConfig = resolve(__dirname, "../../../hardhat.config.js");
17
+
18
+ // Only set HARDHAT_CONFIG if not already set and if the config exists
19
+ if (!process.env.HARDHAT_CONFIG && existsSync(packageHardhatConfig)) {
20
+ process.env.HARDHAT_CONFIG = packageHardhatConfig;
21
+ }
22
+
23
+ // Import hardhat after potentially setting the config path
24
+ const { network } = await import("hardhat");
25
+
26
+ const testAddress = "0x6d6e022eE439C8aB8B7a7dBb0576f8090319CDc6";
27
+ const emptyRecipient = "0xE29E61479420Dd1029A9946710Ac31A0d140e77F";
28
+ const USDCAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
29
+
30
+ const context: { provider: JsonRpcProvider; signer: Signer; toolbox: Awaited<ReturnType<typeof getEvmToolbox>> } =
31
+ {} as any;
32
+
33
+ beforeEach(async () => {
34
+ // Connect to Hardhat's in-process simulated network with forking (works in both local and CI)
35
+ // Must explicitly connect to 'hardhat' network to use the forking configuration
36
+ const connection = await network.connect("hardhat");
37
+
38
+ // Use Hardhat's simulated network provider directly
39
+ const provider = (connection as any).ethers.provider as JsonRpcProvider;
40
+ const signer = await (connection as any).ethers.getImpersonatedSigner(testAddress);
41
+
42
+ SKConfig.set({ apiKeys: { swapKit: process.env.TEST_API_KEY || Bun.env.TEST_API_KEY } });
43
+
44
+ context.provider = provider;
45
+ context.signer = signer as any;
46
+ context.toolbox = await getEvmToolbox(Chain.Ethereum, { provider, signer: signer as any });
47
+ });
48
+
49
+ describe("Ethereum toolkit", () => {
50
+ test.skip("Get Balances", async () => {
51
+ const balances = await context.toolbox.getBalance(testAddress);
52
+ expect(balances.find((balance) => balance.symbol === "ETH")?.getBaseValue("string")).toBe("20526000000000000");
53
+ expect(
54
+ balances
55
+ .find(
56
+ (balance) => balance.symbol.toLowerCase() === "USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".toLowerCase(),
57
+ )
58
+ ?.getBaseValue("string"),
59
+ ).toBe("6656178");
60
+ }, 10000);
61
+
62
+ test("Send ETH", async () => {
63
+ expect((await context.provider.getBalance(emptyRecipient)).toString()).toBe("0");
64
+ await context.toolbox.transfer({
65
+ assetValue: AssetValue.from({ chain: Chain.Ethereum, value: "0.010526" }),
66
+ recipient: emptyRecipient,
67
+ sender: testAddress,
68
+ });
69
+ expect((await context.provider.getBalance(emptyRecipient)).toString()).toBe("10526000000000000");
70
+ }, 10000);
71
+
72
+ test(
73
+ "Send Token",
74
+ async () => {
75
+ const USDC = context.toolbox.createContract(USDCAddress, erc20ABI);
76
+ const balance = await USDC.balanceOf?.(emptyRecipient);
77
+ expect(balance.toString()).toBe("0");
78
+
79
+ await AssetValue.loadStaticAssets(["thorchain"]);
80
+
81
+ const assetValue = AssetValue.from({ asset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", value: "1" });
82
+
83
+ await context.toolbox.transfer({ assetValue, recipient: emptyRecipient, sender: testAddress });
84
+ // biome-ignore lint/correctness/noUnsafeOptionalChaining: Tests
85
+ expect((await USDC.balanceOf?.(emptyRecipient)).toString()).toBe("1000000");
86
+ },
87
+ { retry: 3, timeout: 10000 },
88
+ );
89
+
90
+ test(
91
+ "Approve Token and validate approved amount",
92
+ async () => {
93
+ expect(
94
+ await context.toolbox.isApproved({
95
+ amount: "1000000",
96
+ assetAddress: USDCAddress,
97
+ from: testAddress,
98
+ spenderAddress: emptyRecipient,
99
+ }),
100
+ ).toBe(false);
101
+
102
+ await context.toolbox.approve({ amount: "1000000", assetAddress: USDCAddress, spenderAddress: emptyRecipient });
103
+
104
+ expect(
105
+ await context.toolbox.isApproved({
106
+ amount: "1000000",
107
+ assetAddress: USDCAddress,
108
+ from: testAddress,
109
+ spenderAddress: emptyRecipient,
110
+ }),
111
+ ).toBe(true);
112
+ },
113
+ { retry: 3, timeout: 10000 },
114
+ );
115
+
116
+ test(
117
+ "Create contract tx object and sendTransaction",
118
+ async () => {
119
+ const USDC = context.toolbox.createContract(USDCAddress, erc20ABI);
120
+ const balance = await USDC.balanceOf?.(emptyRecipient);
121
+ expect(balance.toString()).toBe("0");
122
+
123
+ const txObject = await context.toolbox.createContractTxObject({
124
+ abi: erc20ABI,
125
+ contractAddress: USDCAddress,
126
+ funcName: "transfer",
127
+ funcParams: [emptyRecipient, BigInt("2222222")],
128
+ txOverrides: { from: testAddress },
129
+ });
130
+
131
+ await context.toolbox.sendTransaction(txObject);
132
+ // biome-ignore lint/correctness/noUnsafeOptionalChaining: Tests
133
+ expect((await USDC?.balanceOf?.(emptyRecipient)).toString()).toBe("2222222");
134
+ },
135
+ { retry: 3, timeout: 10000 },
136
+ );
137
+ });
@@ -0,0 +1,60 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { Chain } from "@uswap/helpers";
3
+ import { getEvmToolbox } from "../toolbox";
4
+
5
+ const TEST_PHRASE = "test test test test test test test test test test test junk";
6
+ const TEST_MESSAGE = "Hello, this is a test message for SIWE";
7
+ const EXPECTED_ADDRESS = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
8
+
9
+ describe("EVM signMessage", () => {
10
+ test("should sign message with phrase-based signer", async () => {
11
+ const toolbox = await getEvmToolbox(Chain.Ethereum, { phrase: TEST_PHRASE });
12
+
13
+ const address = await toolbox.getAddress();
14
+ expect(address).toBe(EXPECTED_ADDRESS);
15
+
16
+ const signature = await toolbox.signMessage?.(TEST_MESSAGE);
17
+
18
+ expect(signature).toBeDefined();
19
+ expect(typeof signature).toBe("string");
20
+ });
21
+
22
+ test("should sign Uint8Array message with phrase-based signer", async () => {
23
+ const toolbox = await getEvmToolbox(Chain.Ethereum, { phrase: TEST_PHRASE });
24
+
25
+ const messageBytes = new TextEncoder().encode(TEST_MESSAGE);
26
+ const signature = await toolbox.signMessage?.(messageBytes);
27
+
28
+ expect(signature).toBeDefined();
29
+ expect(typeof signature).toBe("string");
30
+ });
31
+
32
+ test("should produce consistent signatures for same message", async () => {
33
+ const toolbox = await getEvmToolbox(Chain.Ethereum, { phrase: TEST_PHRASE });
34
+
35
+ const signature1 = await toolbox.signMessage?.(TEST_MESSAGE);
36
+ const signature2 = await toolbox.signMessage?.(TEST_MESSAGE);
37
+
38
+ expect(signature1).toBeDefined();
39
+ expect(signature2).toBeDefined();
40
+ expect(signature1).toBe(signature2 as string);
41
+ });
42
+
43
+ test("should handle signMessage when no signer is present", async () => {
44
+ const toolbox = await getEvmToolbox(Chain.Ethereum);
45
+
46
+ expect(toolbox.signMessage).toBeUndefined();
47
+ });
48
+
49
+ test("should work with different EVM chains", async () => {
50
+ const chains = [Chain.Ethereum, Chain.Arbitrum, Chain.Polygon, Chain.BinanceSmartChain] as const;
51
+
52
+ for (const chain of chains) {
53
+ const toolbox = await getEvmToolbox(chain, { phrase: TEST_PHRASE });
54
+ const signature = await toolbox.signMessage?.(TEST_MESSAGE);
55
+
56
+ expect(signature).toBeDefined();
57
+ expect(typeof signature).toBe("string");
58
+ }
59
+ });
60
+ });
package/src/evm/api.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { EVMChain } from "@uswap/helpers";
2
+ import { getBalance } from "../utils";
3
+
4
+ export function getEvmApi(chain: EVMChain) {
5
+ return { getBalance: getBalance(chain) };
6
+ }
7
+
8
+ export function createCustomEvmApi(methods: ReturnType<typeof getEvmApi>) {
9
+ return methods;
10
+ }