@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,433 +0,0 @@
1
- import type {
2
- Connection,
3
- PublicKey,
4
- Signer,
5
- Transaction,
6
- TransactionInstruction,
7
- VersionedTransaction,
8
- } from "@solana/web3.js";
9
- import {
10
- AssetValue,
11
- BaseDecimal,
12
- Chain,
13
- DerivationPath,
14
- type DerivationPathArray,
15
- type GenericCreateTransactionParams,
16
- NetworkDerivationPath,
17
- SKConfig,
18
- SwapKitError,
19
- derivationPathToString,
20
- updateDerivationPath,
21
- } from "@swapkit/helpers";
22
- import { P } from "ts-pattern";
23
- import { match } from "ts-pattern";
24
- import type { SolanaCreateTransactionParams, SolanaProvider, SolanaTransferParams } from ".";
25
-
26
- type SolanaSigner = SolanaProvider | Signer;
27
-
28
- type TokenMetadata = {
29
- name: string;
30
- symbol: string;
31
- decimals: number;
32
- logoURI?: string;
33
- tags?: string[];
34
- daily_volume?: number;
35
- };
36
-
37
- async function fetchTokenMetaData(mintAddress: string): Promise<TokenMetadata | null> {
38
- try {
39
- const response = await fetch(`https://lite-api.jup.ag/tokens/v1/token/${mintAddress}`);
40
- if (!response.ok) return null;
41
- return await response.json();
42
- } catch {
43
- return null;
44
- }
45
- }
46
-
47
- async function getSolanaBalance(address: string) {
48
- const connection = await getConnection();
49
- const { PublicKey } = await import("@solana/web3.js");
50
- const { TOKEN_PROGRAM_ID } = await import("@solana/spl-token");
51
- const publicKey = new PublicKey(address);
52
-
53
- const balances: AssetValue[] = [];
54
-
55
- // Get SOL balance
56
- const solBalance = await connection.getBalance(publicKey);
57
- if (solBalance > 0) {
58
- balances.push(
59
- AssetValue.from({
60
- chain: Chain.Solana,
61
- value: solBalance,
62
- fromBaseDecimal: BaseDecimal[Chain.Solana],
63
- }),
64
- );
65
- }
66
-
67
- // Get token balances
68
- const tokenAccounts = await connection.getParsedTokenAccountsByOwner(publicKey, {
69
- programId: TOKEN_PROGRAM_ID,
70
- });
71
-
72
- for (const { account } of tokenAccounts.value) {
73
- const tokenInfo = account.data.parsed.info;
74
- const mintAddress = tokenInfo.mint;
75
- const amount = tokenInfo.tokenAmount.amount;
76
-
77
- if (Number(amount) === 0) continue;
78
-
79
- // Fetch token metadata from Jupiter
80
- const metadata = await fetchTokenMetaData(mintAddress);
81
- const symbol = metadata?.symbol || "UNKNOWN";
82
- const decimals = metadata?.decimals || tokenInfo.tokenAmount.decimals;
83
-
84
- balances.push(
85
- AssetValue.from({
86
- asset: `${Chain.Solana}.${symbol}-${mintAddress}`,
87
- value: amount,
88
- fromBaseDecimal: decimals,
89
- }),
90
- );
91
- }
92
-
93
- return balances;
94
- }
95
-
96
- export async function getSolanaAddressValidator() {
97
- const { PublicKey } = await import("@solana/web3.js");
98
-
99
- return (address: string) => {
100
- try {
101
- const pubkey = new PublicKey(address);
102
- return PublicKey.isOnCurve(pubkey.toBytes());
103
- } catch (_) {
104
- return false;
105
- }
106
- };
107
- }
108
-
109
- export async function getSolanaToolbox(
110
- toolboxParams?:
111
- | { signer?: SolanaSigner }
112
- | { phrase?: string; index?: number; derivationPath?: DerivationPathArray },
113
- ) {
114
- const index = toolboxParams && "index" in toolboxParams ? toolboxParams.index || 0 : 0;
115
- const derivationPath = derivationPathToString(
116
- toolboxParams && "derivationPath" in toolboxParams && toolboxParams.derivationPath
117
- ? toolboxParams.derivationPath
118
- : updateDerivationPath(NetworkDerivationPath[Chain.Solana], { index }),
119
- );
120
-
121
- const signer = await match(toolboxParams)
122
- .with({ phrase: P.string }, ({ phrase }) => createKeysForPath({ phrase, derivationPath }))
123
- .with({ signer: P.any }, ({ signer }) => signer)
124
- .otherwise(() => undefined);
125
-
126
- function getAddress() {
127
- return signer?.publicKey ? getAddressFromPubKey(signer.publicKey) : "";
128
- }
129
-
130
- return {
131
- getConnection,
132
- getAddress,
133
- createKeysForPath,
134
- getAddressFromPubKey,
135
- getPubkeyFromAddress,
136
- createTransaction: createTransaction(getConnection),
137
- createTransactionFromInstructions,
138
- getBalance: (addressParam?: string) => {
139
- const address = addressParam || getAddress();
140
- if (!address) throw new SwapKitError("core_wallet_connection_not_found");
141
- return getSolanaBalance(address);
142
- },
143
- transfer: transfer(getConnection, signer),
144
- broadcastTransaction: broadcastTransaction(getConnection),
145
- getAddressValidator: getSolanaAddressValidator,
146
- signTransaction: signTransaction(getConnection, signer),
147
- estimateTransactionFee: estimateTransactionFee(getConnection),
148
- };
149
- }
150
-
151
- function estimateTransactionFee(getConnection: () => Promise<Connection>) {
152
- return async ({
153
- recipient,
154
- assetValue,
155
- memo,
156
- isProgramDerivedAddress,
157
- sender,
158
- }: Omit<GenericCreateTransactionParams, "feeRate"> & {
159
- isProgramDerivedAddress?: boolean;
160
- }) => {
161
- const connection = await getConnection();
162
-
163
- const transaction = await createTransaction(getConnection)({
164
- recipient,
165
- assetValue,
166
- memo,
167
- isProgramDerivedAddress,
168
- sender,
169
- });
170
-
171
- const message = transaction.compileMessage();
172
- const feeInLamports = await connection.getFeeForMessage(message);
173
-
174
- if (feeInLamports.value === null) {
175
- throw new SwapKitError(
176
- "toolbox_solana_fee_estimation_failed",
177
- "Could not estimate Solana fee.",
178
- );
179
- }
180
-
181
- return AssetValue.from({
182
- chain: Chain.Solana,
183
- value: feeInLamports.value,
184
- fromBaseDecimal: BaseDecimal[Chain.Solana],
185
- });
186
- };
187
- }
188
-
189
- async function getConnection() {
190
- const { Connection } = await import("@solana/web3.js");
191
- return new Connection(SKConfig.get("rpcUrls").SOL, "confirmed");
192
- }
193
-
194
- function createAssetTransaction(getConnection: () => Promise<Connection>) {
195
- return async ({
196
- assetValue,
197
- recipient,
198
- sender,
199
- isProgramDerivedAddress,
200
- }: SolanaCreateTransactionParams) => {
201
- const connection = await getConnection();
202
- const fromPubkey = await getPubkeyFromAddress(sender);
203
-
204
- if (assetValue.isGasAsset) {
205
- const { Transaction, SystemProgram, PublicKey } = await import("@solana/web3.js");
206
-
207
- return new Transaction().add(
208
- SystemProgram.transfer({
209
- fromPubkey: fromPubkey,
210
- lamports: assetValue.getBaseValue("number"),
211
- toPubkey: new PublicKey(recipient),
212
- }),
213
- );
214
- }
215
- if (assetValue.address) {
216
- return createSolanaTokenTransaction({
217
- amount: assetValue.getBaseValue("number"),
218
- connection,
219
- decimals: assetValue.decimal as number,
220
- from: fromPubkey,
221
- recipient,
222
- tokenAddress: assetValue.address,
223
- isProgramDerivedAddress,
224
- });
225
- }
226
-
227
- return undefined;
228
- };
229
- }
230
-
231
- async function createSolanaTokenTransaction({
232
- tokenAddress,
233
- recipient,
234
- from,
235
- connection,
236
- amount,
237
- decimals,
238
- isProgramDerivedAddress,
239
- }: {
240
- tokenAddress: string;
241
- recipient: string;
242
- from: PublicKey;
243
- connection: Connection;
244
- amount: number;
245
- decimals: number;
246
- isProgramDerivedAddress?: boolean;
247
- }) {
248
- const {
249
- getAssociatedTokenAddress,
250
- getAccount,
251
- createAssociatedTokenAccountInstruction,
252
- createTransferCheckedInstruction,
253
- } = await import("@solana/spl-token");
254
- const { Transaction, PublicKey } = await import("@solana/web3.js");
255
-
256
- const transaction = new Transaction();
257
- const tokenPublicKey = new PublicKey(tokenAddress);
258
- const fromSPLAddress = await getAssociatedTokenAddress(tokenPublicKey, from);
259
-
260
- const recipientPublicKey = new PublicKey(recipient);
261
- const recipientSPLAddress = await getAssociatedTokenAddress(
262
- tokenPublicKey,
263
- recipientPublicKey,
264
- isProgramDerivedAddress,
265
- );
266
-
267
- let recipientAccountExists = false;
268
- try {
269
- await getAccount(connection, recipientSPLAddress);
270
- recipientAccountExists = true;
271
- } catch (_) {
272
- // Recipient's associated token account doesn't exist
273
- }
274
-
275
- if (!recipientAccountExists) {
276
- transaction.add(
277
- createAssociatedTokenAccountInstruction(
278
- from,
279
- recipientSPLAddress,
280
- recipientPublicKey,
281
- tokenPublicKey,
282
- ),
283
- );
284
- }
285
-
286
- transaction.add(
287
- createTransferCheckedInstruction(
288
- fromSPLAddress,
289
- tokenPublicKey,
290
- recipientSPLAddress,
291
- from,
292
- amount,
293
- decimals,
294
- ),
295
- );
296
-
297
- return transaction;
298
- }
299
-
300
- function createTransaction(getConnection: () => Promise<Connection>) {
301
- return async ({
302
- recipient,
303
- assetValue,
304
- memo,
305
- isProgramDerivedAddress,
306
- sender,
307
- }: SolanaCreateTransactionParams) => {
308
- const { createMemoInstruction } = await import("@solana/spl-memo");
309
-
310
- const fromPubkey = await getPubkeyFromAddress(sender);
311
- const validateAddress = await getSolanaAddressValidator();
312
-
313
- if (!(isProgramDerivedAddress || validateAddress(recipient))) {
314
- throw new SwapKitError("core_transaction_invalid_recipient_address");
315
- }
316
-
317
- const connection = await getConnection();
318
- const transaction = await createAssetTransaction(getConnection)({
319
- assetValue,
320
- recipient,
321
- sender,
322
- isProgramDerivedAddress,
323
- });
324
-
325
- if (!transaction) {
326
- throw new SwapKitError("core_transaction_invalid_sender_address");
327
- }
328
-
329
- if (memo) transaction.add(createMemoInstruction(memo));
330
-
331
- const blockHash = await connection.getLatestBlockhash();
332
- transaction.recentBlockhash = blockHash.blockhash;
333
- transaction.feePayer = fromPubkey;
334
-
335
- return transaction;
336
- };
337
- }
338
-
339
- async function createTransactionFromInstructions({
340
- instructions,
341
- }: { instructions: TransactionInstruction[]; isProgramDerivedAddress?: boolean }) {
342
- const { Transaction } = await import("@solana/web3.js");
343
- const transaction = new Transaction().add(...instructions);
344
-
345
- if (!transaction) {
346
- throw new SwapKitError("core_transaction_invalid_sender_address");
347
- }
348
-
349
- return transaction;
350
- }
351
-
352
- function transfer(getConnection: () => Promise<Connection>, signer?: SolanaSigner) {
353
- return async ({ recipient, assetValue, memo, isProgramDerivedAddress }: SolanaTransferParams) => {
354
- if (!signer) {
355
- throw new SwapKitError("core_transaction_invalid_sender_address");
356
- }
357
-
358
- const sender =
359
- signer.publicKey?.toString() ??
360
- (await (signer as SolanaProvider).connect()).publicKey.toString();
361
-
362
- const transaction = await createTransaction(getConnection)({
363
- recipient,
364
- assetValue,
365
- memo,
366
- isProgramDerivedAddress,
367
- sender,
368
- });
369
-
370
- if ("connect" in signer) {
371
- const signedTransaction = await signer.signTransaction(transaction);
372
- return broadcastTransaction(getConnection)(signedTransaction);
373
- }
374
-
375
- transaction.sign(signer);
376
-
377
- return broadcastTransaction(getConnection)(transaction);
378
- };
379
- }
380
-
381
- function broadcastTransaction(getConnection: () => Promise<Connection>) {
382
- return async (transaction: Transaction | VersionedTransaction) => {
383
- const connection = await getConnection();
384
- return connection.sendRawTransaction(transaction.serialize());
385
- };
386
- }
387
-
388
- function signTransaction(getConnection: () => Promise<Connection>, signer?: SolanaSigner) {
389
- return async (transaction: Transaction | VersionedTransaction) => {
390
- const { VersionedTransaction } = await import("@solana/web3.js");
391
- if (!signer) {
392
- throw new SwapKitError("toolbox_solana_no_signer");
393
- }
394
-
395
- if (!(transaction instanceof VersionedTransaction)) {
396
- const connection = await getConnection();
397
-
398
- const blockHash = await connection.getLatestBlockhash();
399
- transaction.recentBlockhash = blockHash.blockhash;
400
- transaction.feePayer = signer.publicKey || undefined;
401
- }
402
-
403
- if ("connect" in signer) {
404
- const signedTransaction = await signer.signTransaction(transaction);
405
- return signedTransaction;
406
- }
407
-
408
- await transaction.sign([signer] as Signer & Signer[]);
409
- return transaction;
410
- };
411
- }
412
-
413
- export async function createKeysForPath({
414
- phrase,
415
- derivationPath = DerivationPath.SOL,
416
- }: { phrase: string; derivationPath?: string }) {
417
- const { HDKey } = await import("micro-key-producer/slip10.js");
418
- const { mnemonicToSeedSync } = await import("@scure/bip39");
419
- const { Keypair } = await import("@solana/web3.js");
420
- const seed = mnemonicToSeedSync(phrase);
421
- const hdKey = HDKey.fromMasterSeed(seed);
422
-
423
- return Keypair.fromSeed(hdKey.derive(derivationPath, true).privateKey);
424
- }
425
-
426
- function getAddressFromPubKey(publicKey: PublicKey) {
427
- return publicKey.toString();
428
- }
429
-
430
- async function getPubkeyFromAddress(address: string) {
431
- const { PublicKey } = await import("@solana/web3.js");
432
- return new PublicKey(address);
433
- }
@@ -1,92 +0,0 @@
1
- import type { ApiPromise } from "@polkadot/api";
2
- import { AssetValue, Chain, SwapKitNumber } from "@swapkit/helpers";
3
-
4
- /**
5
- * Get balance for standard Substrate chains (Polkadot, etc.)
6
- * Uses api.query.system.account to query free and reserved balances
7
- */
8
- export async function getSubstrateBalance(
9
- api: ApiPromise,
10
- gasAsset: AssetValue,
11
- address: string,
12
- ): Promise<AssetValue[]> {
13
- try {
14
- const account = await api.query.system?.account?.(address);
15
-
16
- if (!account) {
17
- return [gasAsset.set(0)];
18
- }
19
-
20
- const {
21
- // @ts-expect-error
22
- data: { free },
23
- } = account;
24
-
25
- // Convert the free balance to string using SwapKitNumber for proper decimal handling
26
- const freeBalance = SwapKitNumber.fromBigInt(
27
- BigInt(free.toString()),
28
- gasAsset.decimal,
29
- ).getValue("string");
30
-
31
- return [gasAsset.set(freeBalance)];
32
- } catch (error) {
33
- console.error("Error fetching substrate balance:", error);
34
- return [gasAsset.set(0)];
35
- }
36
- }
37
-
38
- /**
39
- * Get balance for Chainflip chain
40
- * Uses api.query.flip.account to query FLIP balances
41
- */
42
- export async function getChainflipBalance(
43
- api: ApiPromise,
44
- gasAsset: AssetValue,
45
- address: string,
46
- ): Promise<AssetValue[]> {
47
- try {
48
- // Chainflip uses a custom flip pallet for account balances
49
- const flipAccount = await api.query.flip?.account?.(address);
50
-
51
- if (!flipAccount) {
52
- return [gasAsset.set(0)];
53
- }
54
-
55
- // Extract balance from the flip account structure
56
- // The structure has a balance field directly
57
- //@ts-expect-error
58
- const balance = flipAccount.balance || flipAccount.data?.balance;
59
-
60
- if (!balance || balance.isEmpty) {
61
- return [gasAsset.set(0)];
62
- }
63
-
64
- // Convert balance to string using SwapKitNumber
65
- const balanceStr = SwapKitNumber.fromBigInt(
66
- BigInt(balance.toString()),
67
- gasAsset.decimal,
68
- ).getValue("string");
69
-
70
- return [gasAsset.set(balanceStr)];
71
- } catch (error) {
72
- console.error("Error fetching chainflip balance:", error);
73
- return [gasAsset.set(0)];
74
- }
75
- }
76
-
77
- /**
78
- * Factory function to create chain-specific balance getter
79
- */
80
- export function createBalanceGetter(chain: Chain, api: ApiPromise) {
81
- return function getBalance(address: string): Promise<AssetValue[]> {
82
- const gasAsset = AssetValue.from({ chain });
83
-
84
- switch (chain) {
85
- case Chain.Chainflip:
86
- return getChainflipBalance(api, gasAsset, address);
87
-
88
- default:
89
- return getSubstrateBalance(api, gasAsset, address);
90
- }
91
- };
92
- }