@swapkit/toolboxes 1.0.0-beta.0
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.
- package/dist/chunk-fazw0jvt.js +3 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-tvrdndbw.js +4 -0
- package/dist/chunk-tvrdndbw.js.map +9 -0
- package/dist/cosmos/index.cjs +3 -0
- package/dist/cosmos/index.cjs.map +19 -0
- package/dist/cosmos/index.js +3 -0
- package/dist/cosmos/index.js.map +19 -0
- package/dist/evm/index.cjs +3 -0
- package/dist/evm/index.cjs.map +24 -0
- package/dist/evm/index.js +3 -0
- package/dist/evm/index.js.map +24 -0
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +9 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +9 -0
- package/dist/radix/index.cjs +3 -0
- package/dist/radix/index.cjs.map +10 -0
- package/dist/radix/index.js +3 -0
- package/dist/radix/index.js.map +10 -0
- package/dist/solana/index.cjs +3 -0
- package/dist/solana/index.cjs.map +10 -0
- package/dist/solana/index.js +3 -0
- package/dist/solana/index.js.map +10 -0
- package/dist/substrate/index.cjs +3 -0
- package/dist/substrate/index.cjs.map +12 -0
- package/dist/substrate/index.js +3 -0
- package/dist/substrate/index.js.map +12 -0
- package/dist/utxo/index.cjs +3 -0
- package/dist/utxo/index.cjs.map +18 -0
- package/dist/utxo/index.js +3 -0
- package/dist/utxo/index.js.map +18 -0
- package/package.json +105 -0
- package/src/cosmos/index.ts +11 -0
- package/src/cosmos/thorchainUtils/addressFormat.ts +24 -0
- package/src/cosmos/thorchainUtils/index.ts +4 -0
- package/src/cosmos/thorchainUtils/messages.ts +244 -0
- package/src/cosmos/thorchainUtils/registry.ts +47 -0
- package/src/cosmos/thorchainUtils/types/client-types.ts +80 -0
- package/src/cosmos/thorchainUtils/types/index.ts +1 -0
- package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +2806 -0
- package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.ts +2802 -0
- package/src/cosmos/thorchainUtils/util.ts +46 -0
- package/src/cosmos/toolbox/BaseCosmosToolbox.ts +254 -0
- package/src/cosmos/toolbox/gaia.ts +39 -0
- package/src/cosmos/toolbox/getToolboxByChain.ts +29 -0
- package/src/cosmos/toolbox/kujira.ts +61 -0
- package/src/cosmos/toolbox/thorchain.ts +321 -0
- package/src/cosmos/types.ts +31 -0
- package/src/cosmos/util.ts +230 -0
- package/src/evm/__tests__/ethereum.test.ts +147 -0
- package/src/evm/api.ts +157 -0
- package/src/evm/contracts/eth/multicall.ts +165 -0
- package/src/evm/contracts/op/gasOracle.ts +151 -0
- package/src/evm/helpers.ts +145 -0
- package/src/evm/index.ts +20 -0
- package/src/evm/provider.ts +6 -0
- package/src/evm/toolbox/EVMToolbox.ts +662 -0
- package/src/evm/toolbox/arb.ts +61 -0
- package/src/evm/toolbox/avax.ts +36 -0
- package/src/evm/toolbox/base.ts +42 -0
- package/src/evm/toolbox/bsc.ts +34 -0
- package/src/evm/toolbox/eth.ts +44 -0
- package/src/evm/toolbox/getToolboxByChain.ts +42 -0
- package/src/evm/toolbox/matic.ts +42 -0
- package/src/evm/toolbox/op.ts +163 -0
- package/src/evm/types.ts +118 -0
- package/src/index.ts +0 -0
- package/src/radix/index.ts +151 -0
- package/src/radix/toolbox.ts +693 -0
- package/src/solana/index.ts +49 -0
- package/src/solana/toolbox.ts +272 -0
- package/src/substrate/index.ts +3 -0
- package/src/substrate/toolbox/baseSubstrateToolbox.ts +286 -0
- package/src/substrate/toolbox/index.ts +40 -0
- package/src/substrate/types/index.ts +2 -0
- package/src/substrate/types/network.ts +42 -0
- package/src/substrate/types/wallet.ts +78 -0
- package/src/utxo/helpers/api.ts +431 -0
- package/src/utxo/helpers/bchaddrjs.ts +177 -0
- package/src/utxo/helpers/coinselect.ts +96 -0
- package/src/utxo/helpers/index.ts +5 -0
- package/src/utxo/helpers/txSize.ts +104 -0
- package/src/utxo/helpers/utils.ts +45 -0
- package/src/utxo/index.ts +9 -0
- package/src/utxo/toolbox/bitcoinCash.ts +281 -0
- package/src/utxo/toolbox/index.ts +37 -0
- package/src/utxo/toolbox/utxo.ts +360 -0
- package/src/utxo/types.ts +70 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssetValue,
|
|
3
|
+
BaseDecimal,
|
|
4
|
+
type EVMChain,
|
|
5
|
+
FeeOption,
|
|
6
|
+
SwapKitNumber,
|
|
7
|
+
filterAssets,
|
|
8
|
+
formatBigIntToSafeValue,
|
|
9
|
+
isGasAsset,
|
|
10
|
+
} from "@swapkit/helpers";
|
|
11
|
+
import type { BrowserProvider, JsonRpcProvider, Provider } from "ethers";
|
|
12
|
+
|
|
13
|
+
import { getEvmApi } from "./api";
|
|
14
|
+
import { type EIP1559TxParams, type EVMMaxSendableAmountsParams, estimateGasPrices } from "./index";
|
|
15
|
+
|
|
16
|
+
export const estimateMaxSendableAmount = async ({
|
|
17
|
+
toolbox,
|
|
18
|
+
from,
|
|
19
|
+
memo = "",
|
|
20
|
+
feeOptionKey = FeeOption.Fastest,
|
|
21
|
+
assetValue,
|
|
22
|
+
abi,
|
|
23
|
+
funcName,
|
|
24
|
+
funcParams,
|
|
25
|
+
contractAddress,
|
|
26
|
+
txOverrides,
|
|
27
|
+
}: EVMMaxSendableAmountsParams): Promise<AssetValue> => {
|
|
28
|
+
const balance = (await toolbox.getBalance(from)).find(({ symbol, chain }) =>
|
|
29
|
+
assetValue ? symbol === assetValue.symbol : symbol === AssetValue.from({ chain })?.symbol,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const gasRate = (await toolbox.estimateGasPrices())[feeOptionKey];
|
|
33
|
+
|
|
34
|
+
if (!balance) return AssetValue.from({ chain: assetValue.chain });
|
|
35
|
+
|
|
36
|
+
if (assetValue && (balance.chain !== assetValue.chain || balance.symbol !== assetValue?.symbol)) {
|
|
37
|
+
return balance;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const gasLimit =
|
|
41
|
+
abi && funcName && funcParams && contractAddress
|
|
42
|
+
? await toolbox.estimateCall({
|
|
43
|
+
contractAddress,
|
|
44
|
+
abi,
|
|
45
|
+
funcName,
|
|
46
|
+
funcParams,
|
|
47
|
+
txOverrides,
|
|
48
|
+
})
|
|
49
|
+
: await toolbox.estimateGasLimit({
|
|
50
|
+
from,
|
|
51
|
+
recipient: from,
|
|
52
|
+
memo,
|
|
53
|
+
assetValue,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const isFeeEIP1559Compatible = "maxFeePerGas" in gasRate;
|
|
57
|
+
const isFeeEVMLegacyCompatible = "gasPrice" in gasRate;
|
|
58
|
+
|
|
59
|
+
if (!(isFeeEVMLegacyCompatible || isFeeEIP1559Compatible)) {
|
|
60
|
+
throw new Error("Could not fetch fee data");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const fee =
|
|
64
|
+
gasLimit *
|
|
65
|
+
(isFeeEIP1559Compatible
|
|
66
|
+
? (gasRate.maxFeePerGas || 1n) + (gasRate.maxPriorityFeePerGas || 1n)
|
|
67
|
+
: gasRate.gasPrice);
|
|
68
|
+
const maxSendableAmount = SwapKitNumber.fromBigInt(balance.getBaseValue("bigint")).sub(
|
|
69
|
+
fee.toString(),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return AssetValue.from({ chain: balance.chain, value: maxSendableAmount.getValue("string") });
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const toHexString = (value: bigint) => (value > 0n ? `0x${value.toString(16)}` : "0x0");
|
|
76
|
+
|
|
77
|
+
export const getBalance = async ({
|
|
78
|
+
provider,
|
|
79
|
+
address,
|
|
80
|
+
chain,
|
|
81
|
+
potentialScamFilter,
|
|
82
|
+
}: {
|
|
83
|
+
provider: JsonRpcProvider | BrowserProvider;
|
|
84
|
+
address: string;
|
|
85
|
+
chain: EVMChain;
|
|
86
|
+
potentialScamFilter?: boolean;
|
|
87
|
+
}) => {
|
|
88
|
+
const tokenBalances = await getEvmApi(chain).getBalance(address);
|
|
89
|
+
const evmGasTokenBalance = await provider.getBalance(address);
|
|
90
|
+
|
|
91
|
+
const balances = [
|
|
92
|
+
{
|
|
93
|
+
chain,
|
|
94
|
+
symbol: AssetValue.from({ chain }).symbol,
|
|
95
|
+
value: formatBigIntToSafeValue({
|
|
96
|
+
value: BigInt(evmGasTokenBalance),
|
|
97
|
+
decimal: 18,
|
|
98
|
+
bigIntDecimal: 18,
|
|
99
|
+
}),
|
|
100
|
+
decimal: BaseDecimal[chain],
|
|
101
|
+
},
|
|
102
|
+
...tokenBalances.filter((token) => !isGasAsset(token)),
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
const filteredBalances = potentialScamFilter ? filterAssets(balances) : balances;
|
|
106
|
+
|
|
107
|
+
return filteredBalances.map(
|
|
108
|
+
({ symbol, value, decimal }) =>
|
|
109
|
+
new AssetValue({
|
|
110
|
+
decimal: decimal || BaseDecimal[chain],
|
|
111
|
+
value,
|
|
112
|
+
identifier: `${chain}.${symbol}`,
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const estimateTransactionFee = async (
|
|
118
|
+
txObject: EIP1559TxParams,
|
|
119
|
+
// biome-ignore lint/style/useDefaultParameterLast: Should only be used through wrapped toolboxes
|
|
120
|
+
feeOption: FeeOption = FeeOption.Fast,
|
|
121
|
+
chain: EVMChain,
|
|
122
|
+
provider: Provider | BrowserProvider,
|
|
123
|
+
isEIP1559Compatible = true,
|
|
124
|
+
) => {
|
|
125
|
+
const gasPrices = (await estimateGasPrices(provider, isEIP1559Compatible))[feeOption];
|
|
126
|
+
const gasLimit = await provider.estimateGas(txObject);
|
|
127
|
+
const assetValue = AssetValue.from({ chain });
|
|
128
|
+
|
|
129
|
+
if (!isEIP1559Compatible && gasPrices.gasPrice) {
|
|
130
|
+
return assetValue.set(
|
|
131
|
+
SwapKitNumber.fromBigInt(gasPrices.gasPrice * gasLimit, assetValue.decimal),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (gasPrices.maxFeePerGas && gasPrices.maxPriorityFeePerGas) {
|
|
136
|
+
return assetValue.set(
|
|
137
|
+
SwapKitNumber.fromBigInt(
|
|
138
|
+
(gasPrices.maxFeePerGas + gasPrices.maxPriorityFeePerGas) * gasLimit,
|
|
139
|
+
assetValue.decimal,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
throw new Error("No gas price found");
|
|
145
|
+
};
|
package/src/evm/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package
|
|
3
|
+
*/
|
|
4
|
+
export * from "./api";
|
|
5
|
+
export * from "./helpers";
|
|
6
|
+
export * from "./provider";
|
|
7
|
+
export * from "./toolbox/EVMToolbox";
|
|
8
|
+
export * from "./types";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Toolboxes
|
|
12
|
+
*/
|
|
13
|
+
export * from "./toolbox/arb";
|
|
14
|
+
export * from "./toolbox/avax";
|
|
15
|
+
export * from "./toolbox/bsc";
|
|
16
|
+
export * from "./toolbox/eth";
|
|
17
|
+
export * from "./toolbox/getToolboxByChain";
|
|
18
|
+
export * from "./toolbox/matic";
|
|
19
|
+
export * from "./toolbox/op";
|
|
20
|
+
export * from "./toolbox/base";
|