@swapkit/toolboxes 0.0.0-nightly-20250304130539

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 (83) hide show
  1. package/dist/chunk-fazw0jvt.js +3 -0
  2. package/dist/chunk-fazw0jvt.js.map +9 -0
  3. package/dist/chunk-tvrdndbw.js +4 -0
  4. package/dist/chunk-tvrdndbw.js.map +9 -0
  5. package/dist/cosmos/index.cjs +3 -0
  6. package/dist/cosmos/index.cjs.map +19 -0
  7. package/dist/cosmos/index.js +3 -0
  8. package/dist/cosmos/index.js.map +19 -0
  9. package/dist/evm/index.cjs +3 -0
  10. package/dist/evm/index.cjs.map +18 -0
  11. package/dist/evm/index.js +3 -0
  12. package/dist/evm/index.js.map +18 -0
  13. package/dist/index.cjs +3 -0
  14. package/dist/index.cjs.map +9 -0
  15. package/dist/index.js +3 -0
  16. package/dist/index.js.map +9 -0
  17. package/dist/radix/index.cjs +3 -0
  18. package/dist/radix/index.cjs.map +10 -0
  19. package/dist/radix/index.js +3 -0
  20. package/dist/radix/index.js.map +10 -0
  21. package/dist/solana/index.cjs +3 -0
  22. package/dist/solana/index.cjs.map +10 -0
  23. package/dist/solana/index.js +3 -0
  24. package/dist/solana/index.js.map +10 -0
  25. package/dist/substrate/index.cjs +3 -0
  26. package/dist/substrate/index.cjs.map +12 -0
  27. package/dist/substrate/index.js +3 -0
  28. package/dist/substrate/index.js.map +12 -0
  29. package/dist/utxo/index.cjs +3 -0
  30. package/dist/utxo/index.cjs.map +17 -0
  31. package/dist/utxo/index.js +3 -0
  32. package/dist/utxo/index.js.map +17 -0
  33. package/package.json +102 -0
  34. package/src/cosmos/index.ts +8 -0
  35. package/src/cosmos/thorchainUtils/addressFormat.ts +23 -0
  36. package/src/cosmos/thorchainUtils/index.ts +4 -0
  37. package/src/cosmos/thorchainUtils/messages.ts +244 -0
  38. package/src/cosmos/thorchainUtils/registry.ts +47 -0
  39. package/src/cosmos/thorchainUtils/types/client-types.ts +79 -0
  40. package/src/cosmos/thorchainUtils/types/index.ts +1 -0
  41. package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +2806 -0
  42. package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.ts +2802 -0
  43. package/src/cosmos/thorchainUtils/util.ts +46 -0
  44. package/src/cosmos/toolbox/BaseCosmosToolbox.ts +257 -0
  45. package/src/cosmos/toolbox/gaia.ts +39 -0
  46. package/src/cosmos/toolbox/getToolboxByChain.ts +29 -0
  47. package/src/cosmos/toolbox/kujira.ts +61 -0
  48. package/src/cosmos/toolbox/thorchain.ts +305 -0
  49. package/src/cosmos/types.ts +42 -0
  50. package/src/cosmos/util.ts +230 -0
  51. package/src/evm/__tests__/ethereum.test.ts +147 -0
  52. package/src/evm/api.ts +157 -0
  53. package/src/evm/contracts/eth/multicall.ts +165 -0
  54. package/src/evm/contracts/op/gasOracle.ts +151 -0
  55. package/src/evm/helpers.ts +213 -0
  56. package/src/evm/index.ts +15 -0
  57. package/src/evm/toolbox/baseEVMToolbox.ts +670 -0
  58. package/src/evm/toolbox/evm.ts +89 -0
  59. package/src/evm/toolbox/getToolboxByChain.ts +37 -0
  60. package/src/evm/toolbox/op.ts +152 -0
  61. package/src/evm/types.ts +110 -0
  62. package/src/index.ts +0 -0
  63. package/src/radix/index.ts +151 -0
  64. package/src/radix/toolbox.ts +693 -0
  65. package/src/solana/index.ts +49 -0
  66. package/src/solana/toolbox.ts +271 -0
  67. package/src/substrate/index.ts +3 -0
  68. package/src/substrate/toolbox/baseSubstrateToolbox.ts +288 -0
  69. package/src/substrate/toolbox/index.ts +40 -0
  70. package/src/substrate/types/index.ts +2 -0
  71. package/src/substrate/types/network.ts +42 -0
  72. package/src/substrate/types/wallet.ts +78 -0
  73. package/src/utxo/helpers/api.ts +431 -0
  74. package/src/utxo/helpers/bchaddrjs.ts +177 -0
  75. package/src/utxo/helpers/coinselect.ts +96 -0
  76. package/src/utxo/helpers/index.ts +5 -0
  77. package/src/utxo/helpers/txSize.ts +103 -0
  78. package/src/utxo/helpers/utils.ts +48 -0
  79. package/src/utxo/index.ts +7 -0
  80. package/src/utxo/toolbox/bitcoinCash.ts +268 -0
  81. package/src/utxo/toolbox/index.ts +41 -0
  82. package/src/utxo/toolbox/utxo.ts +372 -0
  83. package/src/utxo/types.ts +51 -0
@@ -0,0 +1,213 @@
1
+ import {
2
+ AssetValue,
3
+ BaseDecimal,
4
+ Chain,
5
+ ChainToExplorerUrl,
6
+ ChainToHexChainId,
7
+ type EVMChain,
8
+ FeeOption,
9
+ type NetworkParams,
10
+ SKConfig,
11
+ SwapKitNumber,
12
+ filterAssets,
13
+ formatBigIntToSafeValue,
14
+ isGasAsset,
15
+ } from "@swapkit/helpers";
16
+ import { type BrowserProvider, JsonRpcProvider, type Provider } from "ethers";
17
+
18
+ import { getEvmApi } from "./api";
19
+ import { getEstimateGasPrices } from "./toolbox/baseEVMToolbox";
20
+ import type { EIP1559TxParams, EVMMaxSendableAmountsParams } from "./types";
21
+
22
+ export const getProvider = (chain: EVMChain, customUrl?: string) => {
23
+ return new JsonRpcProvider(customUrl || SKConfig.get("rpcUrls")[chain]);
24
+ };
25
+
26
+ /**
27
+ * @deprecated
28
+ */
29
+ export const estimateMaxSendableAmount = async ({
30
+ toolbox,
31
+ from,
32
+ memo = "",
33
+ feeOptionKey = FeeOption.Fastest,
34
+ assetValue,
35
+ abi,
36
+ funcName,
37
+ funcParams,
38
+ contractAddress,
39
+ txOverrides,
40
+ }: EVMMaxSendableAmountsParams): Promise<AssetValue> => {
41
+ const balances = await toolbox.getBalance(from);
42
+ const balance = balances.find(({ symbol, chain }) =>
43
+ assetValue ? symbol === assetValue.symbol : symbol === AssetValue.from({ chain })?.symbol,
44
+ );
45
+
46
+ const gasRate = (await toolbox.estimateGasPrices())[feeOptionKey];
47
+
48
+ if (!balance) return AssetValue.from({ chain: assetValue.chain });
49
+
50
+ if (assetValue && (balance.chain !== assetValue.chain || balance.symbol !== assetValue?.symbol)) {
51
+ return balance;
52
+ }
53
+
54
+ const gasLimit =
55
+ abi && funcName && funcParams && contractAddress
56
+ ? await toolbox.estimateCall({
57
+ contractAddress,
58
+ abi,
59
+ funcName,
60
+ funcParams,
61
+ txOverrides,
62
+ })
63
+ : await toolbox.estimateGasLimit({
64
+ from,
65
+ recipient: from,
66
+ memo,
67
+ assetValue,
68
+ });
69
+
70
+ const isFeeEIP1559Compatible = "maxFeePerGas" in gasRate;
71
+ const isFeeEVMLegacyCompatible = "gasPrice" in gasRate;
72
+
73
+ if (!(isFeeEVMLegacyCompatible || isFeeEIP1559Compatible)) {
74
+ throw new Error("Could not fetch fee data");
75
+ }
76
+
77
+ const fee =
78
+ gasLimit *
79
+ (isFeeEIP1559Compatible
80
+ ? (gasRate.maxFeePerGas || 1n) + (gasRate.maxPriorityFeePerGas || 1n)
81
+ : gasRate.gasPrice);
82
+ const maxSendableAmount = SwapKitNumber.fromBigInt(balance.getBaseValue("bigint")).sub(
83
+ fee.toString(),
84
+ );
85
+
86
+ return AssetValue.from({ chain: balance.chain, value: maxSendableAmount.getValue("string") });
87
+ };
88
+
89
+ export function toHexString(value: bigint) {
90
+ return value > 0n ? `0x${value.toString(16)}` : "0x0";
91
+ }
92
+
93
+ export function getBalance<C extends EVMChain>({
94
+ provider: toolboxProvider,
95
+ chain,
96
+ }: { provider?: BrowserProvider | JsonRpcProvider; chain: C }) {
97
+ return async function getBalance(
98
+ address: string,
99
+ scamFilter = true,
100
+ overwriteProvider?: BrowserProvider | JsonRpcProvider,
101
+ ) {
102
+ const provider = overwriteProvider || toolboxProvider;
103
+ const tokenBalances = await getEvmApi(chain).getBalance(address);
104
+ const evmGasTokenBalance = (await provider?.getBalance(address)) || 0n;
105
+
106
+ const balances = [
107
+ {
108
+ chain,
109
+ decimal: BaseDecimal[chain],
110
+ symbol: AssetValue.from({ chain }).symbol,
111
+ value: formatBigIntToSafeValue({
112
+ value: BigInt(evmGasTokenBalance),
113
+ decimal: BaseDecimal[chain],
114
+ bigIntDecimal: BaseDecimal[chain],
115
+ }),
116
+ },
117
+ ...tokenBalances.filter((token) => !isGasAsset(token)),
118
+ ];
119
+
120
+ const filteredBalances = scamFilter ? filterAssets(balances) : balances;
121
+
122
+ return filteredBalances.map(
123
+ ({ symbol, value, decimal }) =>
124
+ new AssetValue({
125
+ decimal: decimal || BaseDecimal[chain],
126
+ value,
127
+ identifier: `${chain}.${symbol}`,
128
+ }),
129
+ );
130
+ };
131
+ }
132
+
133
+ export function getEstimateTransactionFee({
134
+ provider,
135
+ isEIP1559Compatible = true,
136
+ }: { provider: Provider | BrowserProvider; isEIP1559Compatible?: boolean }) {
137
+ return async function estimateTransactionFee({
138
+ feeOption = FeeOption.Fast,
139
+ chain,
140
+ ...txObject
141
+ }: EIP1559TxParams & { feeOption: FeeOption; chain: EVMChain }) {
142
+ const estimateGasPrices = getEstimateGasPrices({ provider, isEIP1559Compatible });
143
+ const gasPrices = await estimateGasPrices();
144
+ const gasLimit = await provider.estimateGas(txObject);
145
+
146
+ const assetValue = AssetValue.from({ chain });
147
+ const { gasPrice, maxFeePerGas, maxPriorityFeePerGas } = gasPrices[feeOption];
148
+
149
+ if (!isEIP1559Compatible && gasPrice) {
150
+ return assetValue.set(SwapKitNumber.fromBigInt(gasPrice * gasLimit, assetValue.decimal));
151
+ }
152
+
153
+ if (maxFeePerGas && maxPriorityFeePerGas) {
154
+ const fee = (maxFeePerGas + maxPriorityFeePerGas) * gasLimit;
155
+
156
+ return assetValue.set(SwapKitNumber.fromBigInt(fee, assetValue.decimal));
157
+ }
158
+
159
+ throw new Error("No gas price found");
160
+ };
161
+ }
162
+
163
+ export function getNetworkParams<C extends EVMChain>(chain: C) {
164
+ return () =>
165
+ (Chain.Ethereum === chain
166
+ ? undefined
167
+ : {
168
+ ...getNetworkInfo({ chain }),
169
+ chainId: ChainToHexChainId[chain],
170
+ rpcUrls: [SKConfig.get("rpcUrls")[chain]],
171
+ blockExplorerUrls: [ChainToExplorerUrl[chain]],
172
+ }) as C extends Chain.Ethereum ? undefined : NetworkParams;
173
+ }
174
+
175
+ export function getIsEIP1559Compatible<C extends EVMChain>(chain: C) {
176
+ const notCompatible = [Chain.Arbitrum, Chain.BinanceSmartChain];
177
+
178
+ return !notCompatible.includes(chain);
179
+ }
180
+
181
+ function getNetworkInfo<C extends EVMChain>({ chain }: { chain: C }) {
182
+ const decimals = BaseDecimal[chain];
183
+
184
+ switch (chain) {
185
+ case Chain.Arbitrum:
186
+ return {
187
+ chainName: "Arbitrum One",
188
+ nativeCurrency: { name: "Ethereum", symbol: Chain.Ethereum, decimals },
189
+ };
190
+ case Chain.Avalanche:
191
+ return {
192
+ chainName: "Avalanche Network",
193
+ nativeCurrency: { name: "Avalanche", symbol: chain, decimals },
194
+ };
195
+ case Chain.Base:
196
+ return {
197
+ chainName: "Base Mainnet",
198
+ nativeCurrency: { name: "Ethereum", symbol: Chain.Ethereum, decimals },
199
+ };
200
+ case Chain.BinanceSmartChain:
201
+ return {
202
+ chainName: "BNB Chain",
203
+ nativeCurrency: { name: "Binance Coin", symbol: "BNB", decimals },
204
+ };
205
+ case Chain.Polygon:
206
+ return {
207
+ chainName: "Polygon Mainnet",
208
+ nativeCurrency: { name: "Polygon", symbol: Chain.Polygon, decimals },
209
+ };
210
+ default:
211
+ throw new Error(`Chain ${chain} is not supported`);
212
+ }
213
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Package
3
+ */
4
+ export * from "./api";
5
+ export * from "./helpers";
6
+ export * from "./toolbox/baseEVMToolbox";
7
+ export * from "./types";
8
+
9
+ /**
10
+ * Toolboxes
11
+ */
12
+ export * from "./toolbox/baseEVMToolbox";
13
+ export * from "./toolbox/getToolboxByChain";
14
+ export * from "./toolbox/op";
15
+ export * from "./toolbox/evm";