@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,73 +0,0 @@
1
- import type { EncodeObject } from "@cosmjs/proto-signing";
2
- import type {
3
- Asset,
4
- ChainId,
5
- GenericCreateTransactionParams,
6
- GenericTransferParams,
7
- } from "@swapkit/helpers";
8
-
9
- enum TxType {
10
- Transfer = "transfer",
11
- Unknown = "unknown",
12
- }
13
-
14
- type Tx = {
15
- asset: Asset; // asset
16
- from: { from: string }[]; // list of "from" txs. BNC will have one `TxFrom` only, `BTC` might have many transactions going "in" (based on UTXO)
17
- to: { to: string }[]; // list of "to" transactions. BNC will have one `TxTo` only, `BTC` might have many transactions going "out" (based on UTXO)
18
- date: Date; // timestamp of tx
19
- type: TxType; // type
20
- hash: string; // Tx hash
21
- };
22
-
23
- export type NodeUrl = {
24
- node: string;
25
- rpc: string;
26
- };
27
-
28
- export type TxData = Pick<Tx, "from" | "to" | "type">;
29
-
30
- /**
31
- * Response from `thorchain/constants` endpoint
32
- */
33
- export type ThorchainConstantsResponse = {
34
- int_64_values: {
35
- // We are in fee interested only - ignore all other values
36
- NativeTransactionFee: number;
37
- };
38
- };
39
-
40
- /**
41
- * Response of `/cosmos/base/tendermint/v1beta1/node_info`
42
- * Note: We are interested in `network` (aka chain id) only
43
- */
44
- export type NodeInfoResponse = {
45
- default_node_info: {
46
- network: string;
47
- };
48
- };
49
-
50
- export type TransferTransaction = {
51
- memo: string;
52
- accountNumber: number;
53
- sequence: number;
54
- chainId: ChainId;
55
- msgs: EncodeObject[];
56
- fee: { amount: { denom: string; amount: string }[]; gas: string };
57
- };
58
-
59
- export type CosmosCreateTransactionParams = GenericCreateTransactionParams & {
60
- accountNumber?: number;
61
- sequence?: number;
62
- };
63
-
64
- export type ThorchainCreateTransactionParams = Omit<
65
- CosmosCreateTransactionParams,
66
- "feeRate" | "recipient"
67
- > & {
68
- recipient?: string;
69
- asSignable?: boolean;
70
- asAminoMessage?: boolean;
71
- };
72
-
73
- export type ThorchainDepositParams = Omit<GenericTransferParams, "recipient">;
@@ -1 +0,0 @@
1
- export * from "./client-types";
@@ -1,375 +0,0 @@
1
- import type { StdFee } from "@cosmjs/amino";
2
- import type { Account } from "@cosmjs/stargate";
3
- import { base64, bech32 } from "@scure/base";
4
- import {
5
- AssetValue,
6
- BaseDecimal,
7
- Chain,
8
- type ChainId,
9
- ChainToChainId,
10
- type CosmosChain,
11
- CosmosChainPrefixes,
12
- DerivationPath,
13
- type DerivationPathArray,
14
- FeeOption,
15
- type GenericTransferParams,
16
- NetworkDerivationPath,
17
- SKConfig,
18
- SwapKitError,
19
- SwapKitNumber,
20
- applyFeeMultiplier,
21
- derivationPathToString,
22
- updateDerivationPath,
23
- } from "@swapkit/helpers";
24
- import { SwapKitApi } from "@swapkit/helpers/api";
25
- import { P, match } from "ts-pattern";
26
- import type { CosmosToolboxParams } from "../types";
27
- import {
28
- cosmosCreateTransaction,
29
- createSigningStargateClient,
30
- createStargateClient,
31
- getAssetFromDenom,
32
- getDenomWithChain,
33
- getMsgSendDenom,
34
- } from "../util";
35
-
36
- export async function fetchFeeRateFromSwapKit(chainId: ChainId, safeDefault: number) {
37
- try {
38
- const response = await SwapKitApi.getGasRate();
39
- const responseGasRate = response.find((gas) => gas.chainId === chainId)?.value;
40
-
41
- return responseGasRate ? Number.parseFloat(responseGasRate) : safeDefault;
42
- } catch (_e) {
43
- return safeDefault;
44
- }
45
- }
46
-
47
- export async function getSignerFromPhrase({
48
- phrase,
49
- prefix,
50
- ...derivationParams
51
- }: { phrase: string; prefix?: string } & (
52
- | { chain: Chain; index?: number }
53
- | { derivationPath: string }
54
- )) {
55
- const importedProtoSigning = await import("@cosmjs/proto-signing");
56
- const DirectSecp256k1HdWallet =
57
- importedProtoSigning.DirectSecp256k1HdWallet ??
58
- importedProtoSigning.default?.DirectSecp256k1HdWallet;
59
- const importedCrypto = await import("@cosmjs/crypto");
60
- const stringToPath = importedCrypto.stringToPath ?? importedCrypto.default?.stringToPath;
61
-
62
- const derivationPath =
63
- "derivationPath" in derivationParams
64
- ? derivationParams.derivationPath
65
- : `${DerivationPath[derivationParams.chain]}/${derivationParams.index}`;
66
-
67
- return DirectSecp256k1HdWallet.fromMnemonic(phrase, {
68
- prefix,
69
- hdPaths: [stringToPath(derivationPath)],
70
- });
71
- }
72
-
73
- export async function getSignerFromPrivateKey({
74
- privateKey,
75
- prefix,
76
- }: {
77
- privateKey: Uint8Array;
78
- prefix: string;
79
- }) {
80
- const importedProtoSigning = await import("@cosmjs/proto-signing");
81
- const DirectSecp256k1Wallet =
82
- importedProtoSigning.DirectSecp256k1Wallet ??
83
- importedProtoSigning.default?.DirectSecp256k1Wallet;
84
-
85
- return DirectSecp256k1Wallet.fromKey(privateKey, prefix);
86
- }
87
-
88
- const SafeDefaultFeeValues = {
89
- [Chain.Cosmos]: 500,
90
- [Chain.Kujira]: 1000,
91
- [Chain.THORChain]: 5000000,
92
- [Chain.Maya]: 5000000,
93
- };
94
-
95
- export function verifySignature(getAccount: (address: string) => Promise<Account | null>) {
96
- return async function verifySignature({
97
- signature,
98
- message,
99
- address,
100
- }: {
101
- signature: string;
102
- message: string;
103
- address: string;
104
- }) {
105
- const account = await getAccount(address);
106
- if (!account?.pubkey) throw new SwapKitError("toolbox_cosmos_verify_signature_no_pubkey");
107
-
108
- const importedCrypto = await import("@cosmjs/crypto");
109
- const Secp256k1Signature =
110
- importedCrypto.Secp256k1Signature ?? importedCrypto.default?.Secp256k1Signature;
111
- const Secp256k1 = importedCrypto.Secp256k1 ?? importedCrypto.default?.Secp256k1;
112
-
113
- const secpSignature = Secp256k1Signature.fromFixedLength(base64.decode(signature));
114
- return Secp256k1.verifySignature(secpSignature, base64.decode(message), account.pubkey.value);
115
- };
116
- }
117
-
118
- export async function createCosmosToolbox({ chain, ...toolboxParams }: CosmosToolboxParams) {
119
- const rpcUrl = SKConfig.get("rpcUrls")[chain];
120
- const chainPrefix = CosmosChainPrefixes[chain];
121
-
122
- const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
123
- const derivationPath = derivationPathToString(
124
- "derivationPath" in toolboxParams && toolboxParams.derivationPath
125
- ? toolboxParams.derivationPath
126
- : updateDerivationPath(NetworkDerivationPath[chain], { index }),
127
- );
128
-
129
- const signer = await match(toolboxParams)
130
- .with({ phrase: P.string }, ({ phrase }) =>
131
- getSignerFromPhrase({ phrase, prefix: chainPrefix, derivationPath }),
132
- )
133
- .with({ signer: P.any }, ({ signer }) => signer)
134
- .otherwise(() => undefined);
135
-
136
- async function getAccount(address: string) {
137
- const client = await createStargateClient(rpcUrl);
138
- return client.getAccount(address);
139
- }
140
-
141
- async function getAddress() {
142
- const [account] = (await signer?.getAccounts()) || [];
143
- return account?.address;
144
- }
145
-
146
- async function getPubKey() {
147
- const [account] = (await signer?.getAccounts()) || [];
148
- if (!account?.pubkey) {
149
- throw new SwapKitError("toolbox_cosmos_signer_not_defined");
150
- }
151
- return base64.encode(account?.pubkey);
152
- }
153
-
154
- async function transfer({
155
- recipient,
156
- assetValue,
157
- memo = "",
158
- feeRate,
159
- feeOptionKey = FeeOption.Fast,
160
- }: GenericTransferParams) {
161
- const from = await getAddress();
162
-
163
- if (!(signer && from)) {
164
- throw new SwapKitError("toolbox_cosmos_signer_not_defined");
165
- }
166
-
167
- const feeAssetValue = AssetValue.from({
168
- chain,
169
- });
170
- const assetDenom = getDenomWithChain(feeAssetValue);
171
-
172
- const txFee =
173
- feeRate ||
174
- feeToStdFee((await getFees(chain, SafeDefaultFeeValues[chain]))[feeOptionKey], assetDenom);
175
-
176
- const signingClient = await createSigningStargateClient(rpcUrl, signer);
177
- const message = [
178
- {
179
- denom: getMsgSendDenom(`u${assetValue.symbol}`).toLowerCase(),
180
- amount: assetValue.getBaseValue("string"),
181
- },
182
- ];
183
-
184
- const { transactionHash } = await signingClient.sendTokens(
185
- from,
186
- recipient,
187
- message,
188
- txFee,
189
- memo,
190
- );
191
-
192
- return transactionHash;
193
- }
194
-
195
- return {
196
- transfer,
197
- getAddress,
198
- getAccount,
199
- getBalance: async (address: string, _potentialScamFilter?: boolean) => {
200
- const denomBalances = await cosmosBalanceDenomsGetter(rpcUrl)(address);
201
- return await Promise.all(
202
- denomBalances
203
- .filter(({ denom }) => denom && !denom.includes("IBC/"))
204
- .map(({ denom, amount }) => {
205
- const fullDenom =
206
- [Chain.THORChain, Chain.Maya].includes(chain) &&
207
- (denom.includes("/") || denom.includes("˜"))
208
- ? `${chain}.${denom}`
209
- : denom;
210
- return getAssetFromDenom(fullDenom, amount);
211
- }),
212
- );
213
- },
214
- getSignerFromPhrase: async ({
215
- phrase,
216
- derivationPath,
217
- }: { phrase: string; derivationPath: DerivationPathArray }) =>
218
- getSignerFromPhrase({
219
- phrase,
220
- prefix: chainPrefix,
221
- derivationPath: derivationPathToString(derivationPath),
222
- index,
223
- }),
224
- getSignerFromPrivateKey: async (privateKey: Uint8Array) => {
225
- const importedSigning = await import("@cosmjs/proto-signing");
226
- const DirectSecp256k1Wallet =
227
- importedSigning.DirectSecp256k1Wallet ?? importedSigning.default?.DirectSecp256k1Wallet;
228
- return DirectSecp256k1Wallet.fromKey(privateKey, chainPrefix);
229
- },
230
- createPrivateKeyFromPhrase: createPrivateKeyFromPhrase(derivationPath),
231
- validateAddress: getCosmosValidateAddress(chainPrefix),
232
- getPubKey,
233
- getFees: () => getFees(chain, SafeDefaultFeeValues[chain]),
234
- fetchFeeRateFromSwapKit,
235
- getBalanceAsDenoms: cosmosBalanceDenomsGetter(rpcUrl),
236
- createTransaction: cosmosCreateTransaction,
237
- verifySignature: verifySignature(getAccount),
238
- };
239
- }
240
-
241
- export async function getFeeRateFromSwapKit(chainId: ChainId, safeDefault: number) {
242
- try {
243
- const response = await SwapKitApi.getGasRate();
244
- const responseGasRate = response.find((gas) => gas.chainId === chainId)?.value;
245
-
246
- return responseGasRate ? Number.parseFloat(responseGasRate) : safeDefault;
247
- } catch (_e) {
248
- return safeDefault;
249
- }
250
- }
251
-
252
- /**
253
- * @deprecated use getFeeRateFromSwapKit instead
254
- */
255
- export const getFeeRateFromThorswap = getFeeRateFromSwapKit;
256
-
257
- export function cosmosValidateAddress({
258
- address,
259
- chain,
260
- prefix: chainPrefix,
261
- }: { address: string } & (
262
- | { prefix: string; chain?: undefined }
263
- | { chain: CosmosChain; prefix?: undefined }
264
- )) {
265
- const prefix = chainPrefix || getPrefix(chain);
266
-
267
- if (!(prefix && address)) {
268
- throw new SwapKitError("toolbox_cosmos_validate_address_prefix_not_found");
269
- }
270
-
271
- return getCosmosValidateAddress(prefix)(address);
272
- }
273
-
274
- export function estimateTransactionFee({
275
- assetValue: { chain },
276
- }: {
277
- assetValue: AssetValue;
278
- }) {
279
- return AssetValue.from({ chain, value: getMinTransactionFee(chain) });
280
- }
281
-
282
- function getPrefix<C extends CosmosChain>(chain?: C) {
283
- const { isStagenet } = SKConfig.get("envs");
284
- const useStagenetPrefix = chain
285
- ? [Chain.THORChain, Chain.Maya].includes(chain) && isStagenet
286
- : false;
287
- const basePrefix = chain ? CosmosChainPrefixes[chain] : undefined;
288
-
289
- return useStagenetPrefix ? `s${basePrefix}` : basePrefix;
290
- }
291
-
292
- async function getFees(chain: Chain, safeDefault: number) {
293
- const baseFee = await fetchFeeRateFromSwapKit(ChainToChainId[chain], safeDefault);
294
- return {
295
- average: SwapKitNumber.fromBigInt(BigInt(baseFee), BaseDecimal[chain]),
296
- fast: SwapKitNumber.fromBigInt(
297
- BigInt(applyFeeMultiplier(baseFee, FeeOption.Fast, true)),
298
- BaseDecimal[chain],
299
- ),
300
- fastest: SwapKitNumber.fromBigInt(
301
- BigInt(applyFeeMultiplier(baseFee, FeeOption.Fastest, true)),
302
- BaseDecimal[chain],
303
- ),
304
- } as { [key in FeeOption]: SwapKitNumber };
305
- }
306
-
307
- function feeToStdFee(fee: SwapKitNumber, denom: string): StdFee {
308
- return {
309
- amount: [{ denom, amount: fee.getBaseValue("string") }],
310
- gas: "200000",
311
- };
312
- }
313
-
314
- function getMinTransactionFee(chain: Chain) {
315
- return (
316
- {
317
- [Chain.Cosmos]: 0.007,
318
- [Chain.Kujira]: 0.02,
319
- [Chain.THORChain]: 0.02,
320
- [Chain.Maya]: 0.02,
321
- }[chain as CosmosChain] || 0
322
- );
323
- }
324
-
325
- function getCosmosValidateAddress(prefix: string) {
326
- return function validateAddress(address: string) {
327
- if (!address.startsWith(prefix)) return false;
328
-
329
- try {
330
- const { prefix, words } = bech32.decode(address as `${string}1${string}`);
331
- const normalized = bech32.encode(prefix, words);
332
-
333
- return normalized === address.toLocaleLowerCase();
334
- } catch (_error) {
335
- return false;
336
- }
337
- };
338
- }
339
-
340
- function cosmosBalanceDenomsGetter(rpcUrl: string) {
341
- return async function getCosmosBalanceDenoms(address: string) {
342
- const client = await createStargateClient(rpcUrl);
343
- const allBalances = await client.getAllBalances(address);
344
-
345
- const balances = allBalances.map((balance) => ({
346
- ...balance,
347
- denom: balance.denom.includes("/") ? balance.denom.toUpperCase() : balance.denom,
348
- }));
349
-
350
- return balances;
351
- };
352
- }
353
-
354
- function createPrivateKeyFromPhrase(derivationPath: string) {
355
- return async function createPrivateKeyFromPhrase(phrase: string) {
356
- const importedCrypto = await import("@cosmjs/crypto");
357
- const stringToPath = importedCrypto.stringToPath ?? importedCrypto.default?.stringToPath;
358
- const Slip10Curve = importedCrypto.Slip10Curve ?? importedCrypto.default?.Slip10Curve;
359
- const Slip10 = importedCrypto.Slip10 ?? importedCrypto.default?.Slip10;
360
- const EnglishMnemonic =
361
- importedCrypto.EnglishMnemonic ?? importedCrypto.default?.EnglishMnemonic;
362
- const Bip39 = importedCrypto.Bip39 ?? importedCrypto.default?.Bip39;
363
-
364
- const mnemonicChecked = new EnglishMnemonic(phrase);
365
- const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
366
-
367
- const { privkey } = Slip10.derivePath(
368
- Slip10Curve.Secp256k1,
369
- seed,
370
- stringToPath(derivationPath),
371
- );
372
-
373
- return privkey;
374
- };
375
- }
@@ -1,33 +0,0 @@
1
- import { Chain, type CosmosChain, SwapKitError } from "@swapkit/helpers";
2
-
3
- import type { CosmosToolboxParams } from "../types";
4
- import { createCosmosToolbox } from "./cosmos";
5
- import { createThorchainToolbox } from "./thorchain";
6
-
7
- export type CosmosToolboxes = {
8
- GAIA: ReturnType<typeof createCosmosToolbox>;
9
- KUJI: ReturnType<typeof createCosmosToolbox>;
10
- MAYA: ReturnType<typeof createThorchainToolbox>;
11
- THOR: ReturnType<typeof createThorchainToolbox>;
12
- };
13
-
14
- export const getCosmosToolbox = <T extends CosmosChain>(
15
- chain: T,
16
- params?: Omit<CosmosToolboxParams, "chain">,
17
- ): CosmosToolboxes[T] => {
18
- switch (chain) {
19
- case Chain.Cosmos:
20
- case Chain.Kujira:
21
- return createCosmosToolbox({ chain, ...params }) as CosmosToolboxes[T];
22
-
23
- case Chain.Maya:
24
- case Chain.THORChain:
25
- return createThorchainToolbox({ chain, ...params }) as CosmosToolboxes[T];
26
-
27
- default:
28
- throw new SwapKitError("toolbox_cosmos_not_supported", { chain });
29
- }
30
- };
31
-
32
- export * from "./cosmos";
33
- export * from "./thorchain";