@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.
- package/dist/src/cardano/index.cjs +4 -0
- package/dist/src/cardano/index.cjs.map +11 -0
- package/dist/src/cardano/index.js +4 -0
- package/dist/src/cardano/index.js.map +11 -0
- package/dist/src/cosmos/index.cjs +4 -0
- package/dist/src/cosmos/index.cjs.map +20 -0
- package/dist/src/cosmos/index.js +4 -0
- package/dist/src/cosmos/index.js.map +20 -0
- package/dist/src/evm/index.cjs +4 -0
- package/dist/src/evm/index.cjs.map +20 -0
- package/dist/src/evm/index.js +4 -0
- package/dist/src/evm/index.js.map +20 -0
- package/dist/src/index.cjs +5 -0
- package/dist/src/index.cjs.map +67 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +67 -0
- package/dist/src/near/index.cjs +4 -0
- package/dist/src/near/index.cjs.map +16 -0
- package/dist/src/near/index.js +4 -0
- package/dist/src/near/index.js.map +16 -0
- package/dist/src/radix/index.cjs +4 -0
- package/dist/src/radix/index.cjs.map +10 -0
- package/dist/src/radix/index.js +4 -0
- package/dist/src/radix/index.js.map +10 -0
- package/dist/src/ripple/index.cjs +4 -0
- package/dist/src/ripple/index.cjs.map +10 -0
- package/dist/src/ripple/index.js +4 -0
- package/dist/src/ripple/index.js.map +10 -0
- package/dist/src/solana/index.cjs +4 -0
- package/dist/src/solana/index.cjs.map +11 -0
- package/dist/src/solana/index.js +4 -0
- package/dist/src/solana/index.js.map +11 -0
- package/dist/src/substrate/index.cjs +4 -0
- package/dist/src/substrate/index.cjs.map +13 -0
- package/dist/src/substrate/index.js +4 -0
- package/dist/src/substrate/index.js.map +13 -0
- package/dist/src/sui/index.cjs +4 -0
- package/dist/src/sui/index.cjs.map +11 -0
- package/dist/src/sui/index.js +4 -0
- package/dist/src/sui/index.js.map +11 -0
- package/dist/src/ton/index.cjs +4 -0
- package/dist/src/ton/index.cjs.map +11 -0
- package/dist/src/ton/index.js +4 -0
- package/dist/src/ton/index.js.map +11 -0
- package/dist/src/tron/index.cjs +4 -0
- package/dist/src/tron/index.cjs.map +13 -0
- package/dist/src/tron/index.js +4 -0
- package/dist/src/tron/index.js.map +13 -0
- package/dist/src/utxo/index.cjs +5 -0
- package/dist/src/utxo/index.cjs.map +21 -0
- package/dist/src/utxo/index.js +5 -0
- package/dist/src/utxo/index.js.map +21 -0
- package/dist/types/cardano/index.d.ts +3 -0
- package/dist/types/cardano/index.d.ts.map +1 -0
- package/dist/types/cardano/toolbox.d.ts +34 -0
- package/dist/types/cardano/toolbox.d.ts.map +1 -0
- package/dist/types/cardano/types.d.ts +11 -0
- package/dist/types/cardano/types.d.ts.map +1 -0
- package/dist/types/cosmos/index.d.ts +5 -0
- package/dist/types/cosmos/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +63 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts +62 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/index.d.ts +15 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
- package/dist/types/cosmos/types.d.ts +49 -0
- package/dist/types/cosmos/types.d.ts.map +1 -0
- package/dist/types/cosmos/util.d.ts +74 -0
- package/dist/types/cosmos/util.d.ts.map +1 -0
- package/dist/types/evm/api.d.ts +8 -0
- package/dist/types/evm/api.d.ts.map +1 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
- package/dist/types/evm/helpers.d.ts +6 -0
- package/dist/types/evm/helpers.d.ts.map +1 -0
- package/dist/types/evm/index.d.ts +5 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +83 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
- package/dist/types/evm/toolbox/evm.d.ts +767 -0
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
- package/dist/types/evm/toolbox/index.d.ts +7 -0
- package/dist/types/evm/toolbox/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/op.d.ts +76 -0
- package/dist/types/evm/toolbox/op.d.ts.map +1 -0
- package/dist/types/evm/types.d.ts +108 -0
- package/dist/types/evm/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +75 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/helpers/core.d.ts +15 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -0
- package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
- package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
- package/dist/types/near/helpers/nep141.d.ts +36 -0
- package/dist/types/near/helpers/nep141.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +10 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/toolbox.d.ts +32 -0
- package/dist/types/near/toolbox.d.ts.map +1 -0
- package/dist/types/near/types/contract.d.ts +38 -0
- package/dist/types/near/types/contract.d.ts.map +1 -0
- package/dist/types/near/types/nep141.d.ts +29 -0
- package/dist/types/near/types/nep141.d.ts.map +1 -0
- package/dist/types/near/types/toolbox.d.ts +51 -0
- package/dist/types/near/types/toolbox.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +47 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +14 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/ripple/index.d.ts +46 -0
- package/dist/types/ripple/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +23 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/toolbox.d.ts +51 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -0
- package/dist/types/substrate/balance.d.ts +17 -0
- package/dist/types/substrate/balance.d.ts.map +1 -0
- package/dist/types/substrate/index.d.ts +3 -0
- package/dist/types/substrate/index.d.ts.map +1 -0
- package/dist/types/substrate/substrate.d.ts +148 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -0
- package/dist/types/substrate/types.d.ts +100 -0
- package/dist/types/substrate/types.d.ts.map +1 -0
- package/dist/types/sui/index.d.ts +3 -0
- package/dist/types/sui/index.d.ts.map +1 -0
- package/dist/types/sui/toolbox.d.ts +19 -0
- package/dist/types/sui/toolbox.d.ts.map +1 -0
- package/dist/types/sui/types.d.ts +16 -0
- package/dist/types/sui/types.d.ts.map +1 -0
- package/dist/types/ton/index.d.ts +3 -0
- package/dist/types/ton/index.d.ts.map +1 -0
- package/dist/types/ton/toolbox.d.ts +14 -0
- package/dist/types/ton/toolbox.d.ts.map +1 -0
- package/dist/types/ton/types.d.ts +22 -0
- package/dist/types/ton/types.d.ts.map +1 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
- package/dist/types/tron/helpers/trongrid.d.ts +8 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
- package/dist/types/tron/index.d.ts +6 -0
- package/dist/types/tron/index.d.ts.map +1 -0
- package/dist/types/tron/toolbox.d.ts +26 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -0
- package/dist/types/tron/types.d.ts +103 -0
- package/dist/types/tron/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +26 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utxo/helpers/api.d.ts +101 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
- package/dist/types/utxo/helpers/coinselect.d.ts +17 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
- package/dist/types/utxo/helpers/index.d.ts +5 -0
- package/dist/types/utxo/helpers/index.d.ts.map +1 -0
- package/dist/types/utxo/helpers/txSize.d.ts +21 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
- package/dist/types/utxo/index.d.ts +7 -0
- package/dist/types/utxo/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +93 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/index.d.ts +28 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/params.d.ts +32 -0
- package/dist/types/utxo/toolbox/params.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/utxo.d.ts +103 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/validators.d.ts +4 -0
- package/dist/types/utxo/toolbox/validators.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/zcash.d.ts +72 -0
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
- package/dist/types/utxo/types.d.ts +46 -0
- package/dist/types/utxo/types.d.ts.map +1 -0
- package/package.json +205 -0
- package/src/__tests__/address-validation-all-chains.test.ts +162 -0
- package/src/__tests__/addressValidator.test.ts +162 -0
- package/src/cardano/__tests__/toolbox.test.ts +48 -0
- package/src/cardano/index.ts +2 -0
- package/src/cardano/toolbox.ts +168 -0
- package/src/cardano/types.ts +10 -0
- package/src/cosmos/__tests__/toolbox.test.ts +91 -0
- package/src/cosmos/index.ts +4 -0
- package/src/cosmos/thorchainUtils/addressFormat.ts +22 -0
- package/src/cosmos/thorchainUtils/index.ts +4 -0
- package/src/cosmos/thorchainUtils/messages.ts +212 -0
- package/src/cosmos/thorchainUtils/registry.ts +43 -0
- package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +2800 -0
- package/src/cosmos/thorchainUtils/types/client-types.ts +54 -0
- package/src/cosmos/thorchainUtils/types/index.ts +1 -0
- package/src/cosmos/toolbox/cosmos.ts +345 -0
- package/src/cosmos/toolbox/index.ts +35 -0
- package/src/cosmos/toolbox/thorchain.ts +249 -0
- package/src/cosmos/types.ts +48 -0
- package/src/cosmos/util.ts +214 -0
- package/src/evm/__tests__/address-validation.test.ts +84 -0
- package/src/evm/__tests__/ethereum.test.ts +137 -0
- package/src/evm/__tests__/signMessage.test.ts +60 -0
- package/src/evm/api.ts +10 -0
- package/src/evm/contracts/eth/multicall.ts +165 -0
- package/src/evm/contracts/op/gasOracle.ts +145 -0
- package/src/evm/helpers.ts +73 -0
- package/src/evm/index.ts +4 -0
- package/src/evm/toolbox/baseEVMToolbox.ts +695 -0
- package/src/evm/toolbox/evm.ts +67 -0
- package/src/evm/toolbox/index.ts +44 -0
- package/src/evm/toolbox/op.ts +156 -0
- package/src/evm/types.ts +146 -0
- package/src/index.ts +260 -0
- package/src/near/__tests__/core.test.ts +70 -0
- package/src/near/helpers/core.ts +85 -0
- package/src/near/helpers/gasEstimation.ts +96 -0
- package/src/near/helpers/nep141.ts +50 -0
- package/src/near/index.ts +21 -0
- package/src/near/toolbox.ts +421 -0
- package/src/near/types/contract.ts +32 -0
- package/src/near/types/nep141.ts +34 -0
- package/src/near/types/toolbox.ts +55 -0
- package/src/near/types.ts +44 -0
- package/src/radix/index.ts +132 -0
- package/src/ripple/index.ts +179 -0
- package/src/solana/index.ts +36 -0
- package/src/solana/toolbox.ts +415 -0
- package/src/substrate/balance.ts +88 -0
- package/src/substrate/index.ts +2 -0
- package/src/substrate/substrate.ts +281 -0
- package/src/substrate/types.ts +115 -0
- package/src/sui/__tests__/toolbox.test.ts +82 -0
- package/src/sui/index.ts +2 -0
- package/src/sui/toolbox.ts +165 -0
- package/src/sui/types.ts +11 -0
- package/src/ton/__tests__/toolbox.test.ts +63 -0
- package/src/ton/index.ts +2 -0
- package/src/ton/toolbox.ts +136 -0
- package/src/ton/types.ts +13 -0
- package/src/tron/__tests__/toolbox.test.ts +221 -0
- package/src/tron/helpers/trc20.abi.ts +107 -0
- package/src/tron/helpers/trongrid.ts +53 -0
- package/src/tron/index.ts +21 -0
- package/src/tron/toolbox.ts +585 -0
- package/src/tron/types.ts +83 -0
- package/src/types.ts +28 -0
- package/src/utils.ts +27 -0
- package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
- package/src/utxo/helpers/api.ts +471 -0
- package/src/utxo/helpers/bchaddrjs.ts +166 -0
- package/src/utxo/helpers/coinselect.ts +92 -0
- package/src/utxo/helpers/index.ts +4 -0
- package/src/utxo/helpers/txSize.ts +137 -0
- package/src/utxo/index.ts +6 -0
- package/src/utxo/toolbox/bitcoinCash.ts +243 -0
- package/src/utxo/toolbox/index.ts +59 -0
- package/src/utxo/toolbox/params.ts +18 -0
- package/src/utxo/toolbox/utxo.ts +439 -0
- package/src/utxo/toolbox/validators.ts +36 -0
- package/src/utxo/toolbox/zcash.ts +245 -0
- package/src/utxo/types.ts +39 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Chain, type EVMChain, FeeOption, getRPCUrl } from "@uswap/helpers";
|
|
2
|
+
import { match, P } from "ts-pattern";
|
|
3
|
+
|
|
4
|
+
import { getEvmApi } from "../api";
|
|
5
|
+
import { multicallAbi } from "../contracts/eth/multicall";
|
|
6
|
+
import { getIsEIP1559Compatible, getNetworkParams, getProvider } from "../helpers";
|
|
7
|
+
import type { EVMToolboxParams } from "../types";
|
|
8
|
+
import { BaseEVMToolbox } from "./baseEVMToolbox";
|
|
9
|
+
|
|
10
|
+
export async function ETHToolbox({ provider, ...signer }: EVMToolboxParams) {
|
|
11
|
+
const evmToolbox = await createEvmToolbox(Chain.Ethereum)({ provider, ...signer });
|
|
12
|
+
async function multicall(
|
|
13
|
+
callTuples: { address: string; data: string }[],
|
|
14
|
+
multicallAddress = "0x5ba1e12693dc8f9c48aad8770482f4739beed696",
|
|
15
|
+
funcName = "aggregate",
|
|
16
|
+
feeOptionKey: FeeOption = FeeOption.Fast,
|
|
17
|
+
) {
|
|
18
|
+
const txObject = await evmToolbox.createContractTxObject({
|
|
19
|
+
abi: multicallAbi,
|
|
20
|
+
contractAddress: multicallAddress,
|
|
21
|
+
funcName,
|
|
22
|
+
funcParams: [callTuples],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return evmToolbox.sendTransaction({ ...txObject, feeOptionKey });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { ...evmToolbox, multicall };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const ARBToolbox = createEvmToolbox(Chain.Arbitrum);
|
|
32
|
+
export const AURORAToolbox = createEvmToolbox(Chain.Aurora);
|
|
33
|
+
export const AVAXToolbox = createEvmToolbox(Chain.Avalanche);
|
|
34
|
+
export const BASEToolbox = createEvmToolbox(Chain.Base);
|
|
35
|
+
export const BERAToolbox = createEvmToolbox(Chain.Berachain);
|
|
36
|
+
export const BSCToolbox = createEvmToolbox(Chain.BinanceSmartChain);
|
|
37
|
+
export const BotanixToolbox = createEvmToolbox(Chain.Botanix);
|
|
38
|
+
export const COREToolbox = createEvmToolbox(Chain.Core);
|
|
39
|
+
export const CORNToolbox = createEvmToolbox(Chain.Corn);
|
|
40
|
+
export const CROToolbox = createEvmToolbox(Chain.Cronos);
|
|
41
|
+
export const GNOToolbox = createEvmToolbox(Chain.Gnosis);
|
|
42
|
+
export const HYPEREVMToolbox = createEvmToolbox(Chain.Hyperevm);
|
|
43
|
+
export const MATICToolbox = createEvmToolbox(Chain.Polygon);
|
|
44
|
+
export const SONICToolbox = createEvmToolbox(Chain.Sonic);
|
|
45
|
+
export const UNIToolbox = createEvmToolbox(Chain.Unichain);
|
|
46
|
+
export const XLayerToolbox = createEvmToolbox(Chain.XLayer);
|
|
47
|
+
export const MONADToolbox = createEvmToolbox(Chain.Monad);
|
|
48
|
+
export const MEGAETHToolbox = createEvmToolbox(Chain.MegaETH as EVMChain);
|
|
49
|
+
|
|
50
|
+
function createEvmToolbox<C extends EVMChain>(chain: C) {
|
|
51
|
+
return async function createEvmToolbox({ provider: providerParam, ...toolboxSignerParams }: EVMToolboxParams) {
|
|
52
|
+
const { HDNodeWallet } = await import("ethers");
|
|
53
|
+
const rpcUrl = await getRPCUrl(chain);
|
|
54
|
+
|
|
55
|
+
const provider = providerParam || (await getProvider(chain, rpcUrl));
|
|
56
|
+
|
|
57
|
+
const isEIP1559Compatible = getIsEIP1559Compatible(chain);
|
|
58
|
+
const signer = match(toolboxSignerParams)
|
|
59
|
+
.with({ phrase: P.string }, ({ phrase }) => HDNodeWallet.fromPhrase(phrase).connect(provider))
|
|
60
|
+
.with({ signer: P.any }, ({ signer }) => signer)
|
|
61
|
+
.otherwise(() => undefined);
|
|
62
|
+
|
|
63
|
+
const evmToolbox = BaseEVMToolbox({ chain, isEIP1559Compatible, provider, signer });
|
|
64
|
+
|
|
65
|
+
return { ...evmToolbox, getBalance: getEvmApi(chain).getBalance, getNetworkParams: getNetworkParams(chain) };
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Chain, type EVMChain } from "@uswap/helpers";
|
|
2
|
+
import { match } from "ts-pattern";
|
|
3
|
+
|
|
4
|
+
import { getProvider } from "../helpers";
|
|
5
|
+
import type { EVMToolboxes, EVMToolboxParams } from "../types";
|
|
6
|
+
import * as evmToolboxes from "./evm";
|
|
7
|
+
import { OPToolbox } from "./op";
|
|
8
|
+
|
|
9
|
+
export async function getEvmToolbox<T extends EVMChain>(chain: T, params?: EVMToolboxParams) {
|
|
10
|
+
const toolboxParams = { ...params, provider: params?.provider || (await getProvider(chain)) };
|
|
11
|
+
|
|
12
|
+
type Toolbox = Promise<EVMToolboxes[T]>;
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
match(chain as EVMChain)
|
|
16
|
+
.returnType<Toolbox>()
|
|
17
|
+
.with(Chain.Arbitrum, () => evmToolboxes.ARBToolbox(toolboxParams) as Toolbox)
|
|
18
|
+
.with(Chain.Aurora, () => evmToolboxes.AURORAToolbox(toolboxParams) as Toolbox)
|
|
19
|
+
.with(Chain.Avalanche, () => evmToolboxes.AVAXToolbox(toolboxParams) as Toolbox)
|
|
20
|
+
.with(Chain.Base, () => evmToolboxes.BASEToolbox(toolboxParams) as Toolbox)
|
|
21
|
+
.with(Chain.Berachain, () => evmToolboxes.BERAToolbox(toolboxParams) as Toolbox)
|
|
22
|
+
.with(Chain.BinanceSmartChain, () => evmToolboxes.BSCToolbox(toolboxParams) as Toolbox)
|
|
23
|
+
.with(Chain.Botanix, () => evmToolboxes.SONICToolbox(toolboxParams) as Toolbox)
|
|
24
|
+
.with(Chain.Core, () => evmToolboxes.COREToolbox(toolboxParams) as Toolbox)
|
|
25
|
+
.with(Chain.Corn, () => evmToolboxes.CORNToolbox(toolboxParams) as Toolbox)
|
|
26
|
+
.with(Chain.Cronos, () => evmToolboxes.CROToolbox(toolboxParams) as Toolbox)
|
|
27
|
+
.with(Chain.Ethereum, () => evmToolboxes.ETHToolbox(toolboxParams) as Toolbox)
|
|
28
|
+
.with(Chain.Gnosis, () => evmToolboxes.GNOToolbox(toolboxParams) as Toolbox)
|
|
29
|
+
.with(Chain.Hyperevm, () => evmToolboxes.HYPEREVMToolbox(toolboxParams) as Toolbox)
|
|
30
|
+
.with(Chain.Optimism, () => OPToolbox(toolboxParams) as Toolbox)
|
|
31
|
+
.with(Chain.Polygon, () => evmToolboxes.MATICToolbox(toolboxParams) as Toolbox)
|
|
32
|
+
.with(Chain.Sonic, () => evmToolboxes.SONICToolbox(toolboxParams) as Toolbox)
|
|
33
|
+
.with(Chain.Unichain, () => evmToolboxes.UNIToolbox(toolboxParams) as Toolbox)
|
|
34
|
+
.with(Chain.XLayer, () => evmToolboxes.XLayerToolbox(toolboxParams) as Toolbox)
|
|
35
|
+
.with(Chain.Monad, () => evmToolboxes.MONADToolbox(toolboxParams) as Toolbox)
|
|
36
|
+
// @ts-expect-error TODO: Remove once live
|
|
37
|
+
.with(Chain.MegaETH, () => evmToolboxes.MEGAETHToolbox(toolboxParams) as Toolbox)
|
|
38
|
+
.exhaustive()
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export * from "./baseEVMToolbox";
|
|
43
|
+
export * from "./evm";
|
|
44
|
+
export * from "./op";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { applyFeeMultiplierToBigInt, Chain, FeeOption, getChainConfig, getRPCUrl, SwapKitError } from "@uswap/helpers";
|
|
2
|
+
import type { Authorization, BrowserProvider, JsonRpcProvider, Provider, TransactionRequest } from "ethers";
|
|
3
|
+
import { Contract, HDNodeWallet } from "ethers";
|
|
4
|
+
import { match, P } from "ts-pattern";
|
|
5
|
+
import { getEvmApi } from "../api";
|
|
6
|
+
import { gasOracleAbi } from "../contracts/op/gasOracle";
|
|
7
|
+
import { getProvider } from "../helpers";
|
|
8
|
+
import type { EVMToolboxParams } from "../types";
|
|
9
|
+
import { BaseEVMToolbox } from "./baseEVMToolbox";
|
|
10
|
+
|
|
11
|
+
const GAS_PRICE_ORACLE_ADDRESS = "0x420000000000000000000000000000000000000f";
|
|
12
|
+
|
|
13
|
+
function connectGasPriceOracle<P extends Provider>(provider: P) {
|
|
14
|
+
return new Contract(GAS_PRICE_ORACLE_ADDRESS, gasOracleAbi, provider);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function getL1GasPriceFetcher<P extends Provider>(provider: P) {
|
|
18
|
+
return async function getL1GasPrice() {
|
|
19
|
+
const gasPriceOracle = connectGasPriceOracle(provider);
|
|
20
|
+
|
|
21
|
+
if (gasPriceOracle && "l1BaseFee" in gasPriceOracle) {
|
|
22
|
+
return (await gasPriceOracle?.l1BaseFee()) as unknown as bigint;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function serializeTx<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
|
|
30
|
+
return async function serializeTx({ from, to, nonce, ...tx }: TransactionRequest) {
|
|
31
|
+
const { Transaction } = await import("ethers");
|
|
32
|
+
|
|
33
|
+
if (!to) throw new SwapKitError("toolbox_evm_invalid_transaction", { error: "Missing to address" });
|
|
34
|
+
|
|
35
|
+
return Transaction.from({
|
|
36
|
+
...tx,
|
|
37
|
+
authorizationList: tx.authorizationList as Authorization[],
|
|
38
|
+
nonce: nonce ? nonce : from ? await provider.getTransactionCount(from) : 0,
|
|
39
|
+
to: to as string,
|
|
40
|
+
}).serialized;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function estimateL1GasCost<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
|
|
45
|
+
return async function estimateL1GasCost(tx: TransactionRequest) {
|
|
46
|
+
const gasPriceOracle = connectGasPriceOracle(provider);
|
|
47
|
+
const serializedTx = await serializeTx(provider)(tx);
|
|
48
|
+
|
|
49
|
+
if (gasPriceOracle && "getL1Fee" in gasPriceOracle) {
|
|
50
|
+
return gasPriceOracle.getL1Fee(serializedTx);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function estimateL2GasCost<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
|
|
56
|
+
return async function estimateL2GasCost(tx: TransactionRequest) {
|
|
57
|
+
const l2GasPrice = await provider.send("eth_gasPrice", []);
|
|
58
|
+
const l2GasCost = await provider.estimateGas(tx);
|
|
59
|
+
return l2GasPrice.mul(l2GasCost);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function estimateTotalGasCost<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
|
|
64
|
+
return async function estimateTotalGasCost(tx: TransactionRequest) {
|
|
65
|
+
const l1GasCost = await estimateL1GasCost(provider)(tx);
|
|
66
|
+
const l2GasCost = await estimateL2GasCost(provider)(tx);
|
|
67
|
+
return l1GasCost.add(l2GasCost);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function estimateL1Gas<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
|
|
72
|
+
return async function estimateL1Gas(tx: TransactionRequest) {
|
|
73
|
+
const gasPriceOracle = connectGasPriceOracle(provider);
|
|
74
|
+
const serializedTx = await serializeTx(provider)(tx);
|
|
75
|
+
|
|
76
|
+
if (gasPriceOracle && "getL1GasUsed" in gasPriceOracle) {
|
|
77
|
+
return gasPriceOracle.getL1GasUsed(serializedTx);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getNetworkParams() {
|
|
83
|
+
const { baseDecimal, chainId, explorerUrl, name, rpcUrls } = getChainConfig(Chain.Optimism);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
blockExplorerUrls: [explorerUrl],
|
|
87
|
+
chainId,
|
|
88
|
+
chainName: name,
|
|
89
|
+
nativeCurrency: { decimals: baseDecimal, name: "Ethereum", symbol: Chain.Ethereum },
|
|
90
|
+
rpcUrls,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function estimateGasPrices(provider: Provider) {
|
|
95
|
+
try {
|
|
96
|
+
const { maxFeePerGas, maxPriorityFeePerGas, gasPrice } = await provider.getFeeData();
|
|
97
|
+
const l1GasPrice = await getL1GasPriceFetcher(provider)();
|
|
98
|
+
const price = gasPrice as bigint;
|
|
99
|
+
|
|
100
|
+
if (!(maxFeePerGas && maxPriorityFeePerGas)) {
|
|
101
|
+
throw new SwapKitError("toolbox_evm_no_fee_data");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
[FeeOption.Average]: { gasPrice: price, l1GasPrice, maxFeePerGas, maxPriorityFeePerGas },
|
|
106
|
+
[FeeOption.Fast]: {
|
|
107
|
+
gasPrice: applyFeeMultiplierToBigInt(price, FeeOption.Fast),
|
|
108
|
+
l1GasPrice: applyFeeMultiplierToBigInt(l1GasPrice || 0n, FeeOption.Fast),
|
|
109
|
+
maxFeePerGas,
|
|
110
|
+
maxPriorityFeePerGas: applyFeeMultiplierToBigInt(maxPriorityFeePerGas, FeeOption.Fast),
|
|
111
|
+
},
|
|
112
|
+
[FeeOption.Fastest]: {
|
|
113
|
+
gasPrice: applyFeeMultiplierToBigInt(price, FeeOption.Fastest),
|
|
114
|
+
l1GasPrice: applyFeeMultiplierToBigInt(l1GasPrice || 0n, FeeOption.Fastest),
|
|
115
|
+
maxFeePerGas,
|
|
116
|
+
maxPriorityFeePerGas: applyFeeMultiplierToBigInt(maxPriorityFeePerGas, FeeOption.Fastest),
|
|
117
|
+
},
|
|
118
|
+
} as {
|
|
119
|
+
[key in FeeOption]: {
|
|
120
|
+
l1GasPrice?: bigint;
|
|
121
|
+
gasPrice?: bigint;
|
|
122
|
+
maxFeePerGas?: bigint;
|
|
123
|
+
maxPriorityFeePerGas?: bigint;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
throw new SwapKitError("toolbox_evm_gas_estimation_error", {
|
|
128
|
+
error: (error as any).msg ?? (error as any).toString(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function OPToolbox({ provider: providerParam, ...toolboxSignerParams }: EVMToolboxParams) {
|
|
134
|
+
const chain = Chain.Optimism;
|
|
135
|
+
const rpcUrl = await getRPCUrl(chain);
|
|
136
|
+
const provider = providerParam || (await getProvider(chain, rpcUrl));
|
|
137
|
+
const signer = match(toolboxSignerParams)
|
|
138
|
+
.with({ phrase: P.string }, ({ phrase }) => HDNodeWallet.fromPhrase(phrase).connect(provider))
|
|
139
|
+
.with({ signer: P.any }, ({ signer }) => signer)
|
|
140
|
+
.otherwise(() => undefined);
|
|
141
|
+
|
|
142
|
+
const evmToolbox = BaseEVMToolbox({ provider, signer });
|
|
143
|
+
const getL1GasPrice = getL1GasPriceFetcher(provider);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
...evmToolbox,
|
|
147
|
+
estimateGasPrices: estimateGasPrices(provider),
|
|
148
|
+
estimateL1Gas: estimateL1Gas(provider),
|
|
149
|
+
estimateL1GasCost: estimateL1GasCost(provider),
|
|
150
|
+
estimateL2GasCost: estimateL2GasCost(provider),
|
|
151
|
+
estimateTotalGasCost: estimateTotalGasCost(provider),
|
|
152
|
+
getBalance: getEvmApi(Chain.Optimism).getBalance,
|
|
153
|
+
getL1GasPrice,
|
|
154
|
+
getNetworkParams,
|
|
155
|
+
};
|
|
156
|
+
}
|
package/src/evm/types.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AssetValue,
|
|
3
|
+
ChainSigner,
|
|
4
|
+
DerivationPathArray,
|
|
5
|
+
FeeOption,
|
|
6
|
+
GenericCreateTransactionParams,
|
|
7
|
+
GenericTransferParams,
|
|
8
|
+
} from "@uswap/helpers";
|
|
9
|
+
import { Chain } from "@uswap/helpers";
|
|
10
|
+
import type {
|
|
11
|
+
BigNumberish,
|
|
12
|
+
BrowserProvider,
|
|
13
|
+
JsonFragment,
|
|
14
|
+
JsonRpcProvider,
|
|
15
|
+
JsonRpcSigner,
|
|
16
|
+
Signer,
|
|
17
|
+
Transaction,
|
|
18
|
+
} from "ethers";
|
|
19
|
+
import type { getProvider } from "./index";
|
|
20
|
+
import type {
|
|
21
|
+
ARBToolbox,
|
|
22
|
+
AURORAToolbox,
|
|
23
|
+
AVAXToolbox,
|
|
24
|
+
BASEToolbox,
|
|
25
|
+
BERAToolbox,
|
|
26
|
+
BSCToolbox,
|
|
27
|
+
COREToolbox,
|
|
28
|
+
CORNToolbox,
|
|
29
|
+
CROToolbox,
|
|
30
|
+
ETHToolbox,
|
|
31
|
+
GNOToolbox,
|
|
32
|
+
HYPEREVMToolbox,
|
|
33
|
+
MATICToolbox,
|
|
34
|
+
MEGAETHToolbox,
|
|
35
|
+
MONADToolbox,
|
|
36
|
+
OPToolbox,
|
|
37
|
+
SONICToolbox,
|
|
38
|
+
UNIToolbox,
|
|
39
|
+
XLayerToolbox,
|
|
40
|
+
} from "./toolbox";
|
|
41
|
+
|
|
42
|
+
export enum EthNetwork {
|
|
43
|
+
Test = "goerli",
|
|
44
|
+
Main = "homestead",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type ApproveParams = {
|
|
48
|
+
assetAddress: string;
|
|
49
|
+
spenderAddress: string;
|
|
50
|
+
feeOptionKey?: FeeOption;
|
|
51
|
+
amount?: BigNumberish;
|
|
52
|
+
from?: string;
|
|
53
|
+
// Optional fallback in case estimation for gas limit fails
|
|
54
|
+
gasLimitFallback?: BigNumberish;
|
|
55
|
+
nonce?: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type ApprovedParams = { assetAddress: string; spenderAddress: string; from: string };
|
|
59
|
+
|
|
60
|
+
export type IsApprovedParams = ApprovedParams & { amount?: BigNumberish };
|
|
61
|
+
|
|
62
|
+
export type CallParams = {
|
|
63
|
+
callProvider?: Awaited<ReturnType<typeof getProvider>>;
|
|
64
|
+
contractAddress: string;
|
|
65
|
+
abi: readonly JsonFragment[];
|
|
66
|
+
funcName: string;
|
|
67
|
+
funcParams?: unknown[];
|
|
68
|
+
txOverrides?: Partial<Transaction>;
|
|
69
|
+
feeOption?: FeeOption;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type EstimateCallParams = Pick<
|
|
73
|
+
CallParams,
|
|
74
|
+
"contractAddress" | "abi" | "funcName" | "funcParams" | "txOverrides"
|
|
75
|
+
>;
|
|
76
|
+
|
|
77
|
+
export type EVMTransferParams = GenericTransferParams & { sender?: string };
|
|
78
|
+
|
|
79
|
+
export type EVMCreateTransactionParams = Omit<GenericCreateTransactionParams, "feeRate"> & {
|
|
80
|
+
gasLimit?: bigint;
|
|
81
|
+
gasPrice?: bigint;
|
|
82
|
+
maxFeePerGas?: bigint;
|
|
83
|
+
maxPriorityFeePerGas?: bigint;
|
|
84
|
+
data?: string;
|
|
85
|
+
nonce?: number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export type EVMMaxSendableAmountsParams = {
|
|
89
|
+
from: string;
|
|
90
|
+
assetValue: AssetValue;
|
|
91
|
+
feeOptionKey?: FeeOption;
|
|
92
|
+
memo?: string;
|
|
93
|
+
abi?: readonly JsonFragment[];
|
|
94
|
+
funcName?: string;
|
|
95
|
+
contractAddress?: string;
|
|
96
|
+
funcParams?: unknown[];
|
|
97
|
+
txOverrides?: Partial<Transaction>;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type EVMTxBaseParams<T = bigint> = {
|
|
101
|
+
to?: string;
|
|
102
|
+
from?: string;
|
|
103
|
+
nonce?: number;
|
|
104
|
+
gasLimit?: T;
|
|
105
|
+
data?: string;
|
|
106
|
+
value?: T;
|
|
107
|
+
chainId?: T;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export type EIP1559TxParams<T = bigint> = EVMTxBaseParams<T> & {
|
|
111
|
+
type?: number;
|
|
112
|
+
maxFeePerGas?: T;
|
|
113
|
+
maxPriorityFeePerGas?: T;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export type LegacyEVMTxParams<T = bigint> = EVMTxBaseParams<T> & { gasPrice?: T };
|
|
117
|
+
|
|
118
|
+
export type EVMTxParams = EIP1559TxParams | LegacyEVMTxParams;
|
|
119
|
+
|
|
120
|
+
export type EVMToolboxParams = { provider?: BrowserProvider | JsonRpcProvider } & (
|
|
121
|
+
| { signer?: (ChainSigner<EVMTransferParams, string> & Signer) | JsonRpcSigner }
|
|
122
|
+
| { phrase?: string; derivationPath?: DerivationPathArray; index?: number }
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
export type EVMToolboxes = {
|
|
126
|
+
[Chain.Arbitrum]: Awaited<ReturnType<typeof ARBToolbox>>;
|
|
127
|
+
[Chain.Aurora]: Awaited<ReturnType<typeof AURORAToolbox>>;
|
|
128
|
+
[Chain.Avalanche]: Awaited<ReturnType<typeof AVAXToolbox>>;
|
|
129
|
+
[Chain.Base]: Awaited<ReturnType<typeof BASEToolbox>>;
|
|
130
|
+
[Chain.Berachain]: Awaited<ReturnType<typeof BERAToolbox>>;
|
|
131
|
+
[Chain.BinanceSmartChain]: Awaited<ReturnType<typeof BSCToolbox>>;
|
|
132
|
+
[Chain.Botanix]: Awaited<ReturnType<typeof SONICToolbox>>;
|
|
133
|
+
[Chain.Core]: Awaited<ReturnType<typeof COREToolbox>>;
|
|
134
|
+
[Chain.Corn]: Awaited<ReturnType<typeof CORNToolbox>>;
|
|
135
|
+
[Chain.Cronos]: Awaited<ReturnType<typeof CROToolbox>>;
|
|
136
|
+
[Chain.Ethereum]: Awaited<ReturnType<typeof ETHToolbox>>;
|
|
137
|
+
[Chain.Gnosis]: Awaited<ReturnType<typeof GNOToolbox>>;
|
|
138
|
+
[Chain.Hyperevm]: Awaited<ReturnType<typeof HYPEREVMToolbox>>;
|
|
139
|
+
[Chain.MegaETH]: Awaited<ReturnType<typeof MEGAETHToolbox>>;
|
|
140
|
+
[Chain.Monad]: Awaited<ReturnType<typeof MONADToolbox>>;
|
|
141
|
+
[Chain.Optimism]: Awaited<ReturnType<typeof OPToolbox>>;
|
|
142
|
+
[Chain.Polygon]: Awaited<ReturnType<typeof MATICToolbox>>;
|
|
143
|
+
[Chain.Sonic]: Awaited<ReturnType<typeof SONICToolbox>>;
|
|
144
|
+
[Chain.Unichain]: Awaited<ReturnType<typeof UNIToolbox>>;
|
|
145
|
+
[Chain.XLayer]: Awaited<ReturnType<typeof XLayerToolbox>>;
|
|
146
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssetValue,
|
|
3
|
+
Chain,
|
|
4
|
+
type CosmosChain,
|
|
5
|
+
CosmosChains,
|
|
6
|
+
type EVMChain,
|
|
7
|
+
EVMChains,
|
|
8
|
+
FeeOption,
|
|
9
|
+
type GenericCreateTransactionParams,
|
|
10
|
+
type SubstrateChain,
|
|
11
|
+
SubstrateChains,
|
|
12
|
+
SwapKitError,
|
|
13
|
+
type UTXOChain,
|
|
14
|
+
UTXOChains,
|
|
15
|
+
} from "@uswap/helpers";
|
|
16
|
+
import type { getCardanoToolbox } from "./cardano";
|
|
17
|
+
import type { getCosmosToolbox } from "./cosmos";
|
|
18
|
+
import type { ETHToolbox, EVMCreateTransactionParams, getEvmToolbox } from "./evm";
|
|
19
|
+
import type { getNearToolbox } from "./near";
|
|
20
|
+
import type { RadixToolbox } from "./radix";
|
|
21
|
+
import type { getRippleToolbox } from "./ripple";
|
|
22
|
+
import type { getSolanaToolbox, SolanaCreateTransactionParams } from "./solana";
|
|
23
|
+
import type { getSubstrateToolbox } from "./substrate";
|
|
24
|
+
import type { getSuiToolbox, SuiCreateTransactionParams } from "./sui";
|
|
25
|
+
import type { getTONToolbox } from "./ton";
|
|
26
|
+
import type { createTronToolbox } from "./tron";
|
|
27
|
+
import type { getUtxoToolbox } from "./utxo";
|
|
28
|
+
|
|
29
|
+
export * from "./types";
|
|
30
|
+
|
|
31
|
+
export async function getAddressValidator() {
|
|
32
|
+
const { match } = await import("ts-pattern");
|
|
33
|
+
const { evmValidateAddress } = await import("./evm");
|
|
34
|
+
const { getCardanoAddressValidator } = await import("./cardano");
|
|
35
|
+
const { getCosmosValidateAddress } = await import("./cosmos");
|
|
36
|
+
const { getSolanaAddressValidator } = await import("./solana");
|
|
37
|
+
const { getSuiAddressValidator } = await import("./sui");
|
|
38
|
+
const { getTONAddressValidator } = await import("./ton");
|
|
39
|
+
const { getTronAddressValidator } = await import("./tron");
|
|
40
|
+
const { getUTXOAddressValidator } = await import("./utxo");
|
|
41
|
+
const { getValidateNearAddress } = await import("./near");
|
|
42
|
+
const { radixValidateAddress } = await import("./radix");
|
|
43
|
+
const { rippleValidateAddress } = await import("./ripple");
|
|
44
|
+
const { substrateValidateAddress } = await import("./substrate");
|
|
45
|
+
|
|
46
|
+
const cardanoValidateAddress = await getCardanoAddressValidator();
|
|
47
|
+
const nearValidateAddress = await getValidateNearAddress();
|
|
48
|
+
const solanaValidateAddress = await getSolanaAddressValidator();
|
|
49
|
+
const suiValidateAddress = await getSuiAddressValidator();
|
|
50
|
+
const tonValidateAddress = await getTONAddressValidator();
|
|
51
|
+
const tronValidateAddress = await getTronAddressValidator();
|
|
52
|
+
const utxoValidateAddress = await getUTXOAddressValidator();
|
|
53
|
+
|
|
54
|
+
return function validateAddress({ address, chain }: { address: string; chain: Chain }) {
|
|
55
|
+
const isValid = match(chain)
|
|
56
|
+
.with(...EVMChains, () => evmValidateAddress({ address }))
|
|
57
|
+
.with(...UTXOChains, () => utxoValidateAddress({ address, chain: chain as UTXOChain }))
|
|
58
|
+
.with(Chain.Cosmos, Chain.Kujira, Chain.Noble, Chain.Maya, Chain.THORChain, (chain) => {
|
|
59
|
+
const cosmosValidateAddress = getCosmosValidateAddress(chain);
|
|
60
|
+
return cosmosValidateAddress(address);
|
|
61
|
+
})
|
|
62
|
+
.with(Chain.Chainflip, Chain.Polkadot, () =>
|
|
63
|
+
substrateValidateAddress({ address, chain: chain as SubstrateChain }),
|
|
64
|
+
)
|
|
65
|
+
.with(Chain.Radix, () => radixValidateAddress(address))
|
|
66
|
+
.with(Chain.Near, () => nearValidateAddress(address))
|
|
67
|
+
.with(Chain.Ripple, () => rippleValidateAddress(address))
|
|
68
|
+
.with(Chain.Solana, () => solanaValidateAddress(address))
|
|
69
|
+
.with(Chain.Sui, () => suiValidateAddress(address))
|
|
70
|
+
.with(Chain.Ton, () => tonValidateAddress(address))
|
|
71
|
+
.with(Chain.Tron, () => tronValidateAddress(address))
|
|
72
|
+
.with(Chain.Cardano, () => cardanoValidateAddress(address))
|
|
73
|
+
.otherwise(() => false);
|
|
74
|
+
|
|
75
|
+
return isValid;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getFeeEstimator<T extends keyof CreateTransactionParams>(chain: T) {
|
|
80
|
+
return async function estimateFee(params: CreateTransactionParams[T]) {
|
|
81
|
+
const { match } = await import("ts-pattern");
|
|
82
|
+
|
|
83
|
+
return match(chain as Chain)
|
|
84
|
+
.returnType<Promise<AssetValue>>()
|
|
85
|
+
.with(...EVMChains, async (chain) => {
|
|
86
|
+
const toolbox = await getToolbox(chain);
|
|
87
|
+
const txObject = await toolbox.createTransaction(params);
|
|
88
|
+
|
|
89
|
+
return (toolbox as Awaited<ReturnType<typeof ETHToolbox>>).estimateTransactionFee({
|
|
90
|
+
...txObject,
|
|
91
|
+
chain,
|
|
92
|
+
feeOption: params.feeOptionKey || FeeOption.Fast,
|
|
93
|
+
});
|
|
94
|
+
})
|
|
95
|
+
.with(
|
|
96
|
+
Chain.Bitcoin,
|
|
97
|
+
Chain.BitcoinCash,
|
|
98
|
+
Chain.Dogecoin,
|
|
99
|
+
Chain.Dash,
|
|
100
|
+
Chain.Litecoin,
|
|
101
|
+
Chain.Polkadot,
|
|
102
|
+
Chain.Solana,
|
|
103
|
+
Chain.Ripple,
|
|
104
|
+
Chain.Tron,
|
|
105
|
+
Chain.Near,
|
|
106
|
+
Chain.Cardano,
|
|
107
|
+
async (chain) => {
|
|
108
|
+
const toolbox = await getToolbox(chain);
|
|
109
|
+
return toolbox.estimateTransactionFee(params) as Promise<AssetValue>;
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
.with(Chain.Sui, async () => {
|
|
113
|
+
const { getSuiToolbox } = await import("./sui");
|
|
114
|
+
const suiToolbox = await getSuiToolbox();
|
|
115
|
+
return suiToolbox.estimateTransactionFee(params as SuiCreateTransactionParams);
|
|
116
|
+
})
|
|
117
|
+
.with(Chain.Ton, async () => {
|
|
118
|
+
const { getTONToolbox } = await import("./ton");
|
|
119
|
+
const tonToolbox = await getTONToolbox();
|
|
120
|
+
return tonToolbox.estimateTransactionFee();
|
|
121
|
+
})
|
|
122
|
+
.with(...CosmosChains, async () => {
|
|
123
|
+
const { estimateTransactionFee } = await import("./cosmos");
|
|
124
|
+
return estimateTransactionFee(params);
|
|
125
|
+
})
|
|
126
|
+
.otherwise(async () => AssetValue.from({ chain }));
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type Toolboxes = {
|
|
131
|
+
[key in EVMChain]: Awaited<ReturnType<typeof getEvmToolbox>>;
|
|
132
|
+
} & {
|
|
133
|
+
[key in UTXOChain]: Awaited<ReturnType<typeof getUtxoToolbox>>;
|
|
134
|
+
} & {
|
|
135
|
+
[key in CosmosChain]: Awaited<ReturnType<typeof getCosmosToolbox>>;
|
|
136
|
+
} & {
|
|
137
|
+
[key in SubstrateChain]: Awaited<ReturnType<typeof getSubstrateToolbox>>;
|
|
138
|
+
} & {
|
|
139
|
+
[Chain.Radix]: Awaited<ReturnType<typeof RadixToolbox>>;
|
|
140
|
+
[Chain.Near]: Awaited<ReturnType<typeof getNearToolbox>>;
|
|
141
|
+
[Chain.Ripple]: Awaited<ReturnType<typeof getRippleToolbox>>;
|
|
142
|
+
[Chain.Solana]: Awaited<ReturnType<typeof getSolanaToolbox>>;
|
|
143
|
+
[Chain.Sui]: Awaited<ReturnType<typeof getSuiToolbox>>;
|
|
144
|
+
[Chain.Ton]: Awaited<ReturnType<typeof getTONToolbox>>;
|
|
145
|
+
[Chain.Tron]: Awaited<ReturnType<typeof createTronToolbox>>;
|
|
146
|
+
[Chain.Cardano]: Awaited<ReturnType<typeof getCardanoToolbox>>;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
type ToolboxParams = { [key in EVMChain]: Parameters<typeof getEvmToolbox>[1] } & {
|
|
150
|
+
[key in UTXOChain]: undefined;
|
|
151
|
+
} & {
|
|
152
|
+
[key in CosmosChain]: Parameters<typeof getCosmosToolbox>[1];
|
|
153
|
+
} & {
|
|
154
|
+
[key in SubstrateChain]: Parameters<typeof getSubstrateToolbox>[1];
|
|
155
|
+
} & {
|
|
156
|
+
[Chain.Radix]: Parameters<typeof RadixToolbox>[0];
|
|
157
|
+
[Chain.Near]: Parameters<typeof getNearToolbox>[0];
|
|
158
|
+
[Chain.Ripple]: Parameters<typeof getRippleToolbox>[0];
|
|
159
|
+
[Chain.Solana]: Parameters<typeof getSolanaToolbox>[0];
|
|
160
|
+
[Chain.Sui]: Parameters<typeof getSuiToolbox>[0];
|
|
161
|
+
[Chain.Ton]: Parameters<typeof getTONToolbox>[0];
|
|
162
|
+
[Chain.Tron]: Parameters<typeof createTronToolbox>[0];
|
|
163
|
+
[Chain.Cardano]: Parameters<typeof getCardanoToolbox>[0];
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
type CreateTransactionParams = { [key in EVMChain]: EVMCreateTransactionParams } & {
|
|
167
|
+
[key in UTXOChain]: GenericCreateTransactionParams;
|
|
168
|
+
} & {
|
|
169
|
+
[key in CosmosChain]: GenericCreateTransactionParams;
|
|
170
|
+
} & {
|
|
171
|
+
[key in SubstrateChain]: GenericCreateTransactionParams;
|
|
172
|
+
} & {
|
|
173
|
+
[Chain.Radix]: GenericCreateTransactionParams;
|
|
174
|
+
[Chain.Ripple]: GenericCreateTransactionParams;
|
|
175
|
+
[Chain.Near]: GenericCreateTransactionParams;
|
|
176
|
+
[Chain.Solana]: SolanaCreateTransactionParams;
|
|
177
|
+
[Chain.Sui]: SuiCreateTransactionParams;
|
|
178
|
+
[Chain.Ton]: GenericCreateTransactionParams;
|
|
179
|
+
[Chain.Tron]: GenericCreateTransactionParams;
|
|
180
|
+
[Chain.Cardano]: GenericCreateTransactionParams;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export async function getToolbox<T extends keyof Toolboxes>(
|
|
184
|
+
chain: T,
|
|
185
|
+
params?: ToolboxParams[T],
|
|
186
|
+
): Promise<Toolboxes[T]> {
|
|
187
|
+
const { match } = await import("ts-pattern");
|
|
188
|
+
|
|
189
|
+
return match(chain as Chain)
|
|
190
|
+
.returnType<Promise<Toolboxes[T]>>()
|
|
191
|
+
.with(...EVMChains, async () => {
|
|
192
|
+
const { getEvmToolbox } = await import("./evm/toolbox");
|
|
193
|
+
const evmToolbox = await getEvmToolbox(chain as EVMChain, params as Parameters<typeof getEvmToolbox>[1]);
|
|
194
|
+
return evmToolbox as Toolboxes[T];
|
|
195
|
+
})
|
|
196
|
+
.with(...UTXOChains, async () => {
|
|
197
|
+
const { getUtxoToolbox } = await import("./utxo");
|
|
198
|
+
const utxoToolbox = await getUtxoToolbox(chain as UTXOChain, params as Parameters<typeof getUtxoToolbox>[1]);
|
|
199
|
+
return utxoToolbox as Toolboxes[T];
|
|
200
|
+
})
|
|
201
|
+
.with(...CosmosChains, async () => {
|
|
202
|
+
const { getCosmosToolbox } = await import("./cosmos");
|
|
203
|
+
const cosmosToolbox = await getCosmosToolbox(
|
|
204
|
+
chain as Exclude<CosmosChain, Chain.Harbor>,
|
|
205
|
+
params as Parameters<typeof getCosmosToolbox>[1],
|
|
206
|
+
);
|
|
207
|
+
return cosmosToolbox as Toolboxes[T];
|
|
208
|
+
})
|
|
209
|
+
.with(...SubstrateChains, async () => {
|
|
210
|
+
const { getSubstrateToolbox } = await import("./substrate");
|
|
211
|
+
const substrateToolbox = await getSubstrateToolbox(
|
|
212
|
+
chain as SubstrateChain,
|
|
213
|
+
params as Parameters<typeof getSubstrateToolbox>[1],
|
|
214
|
+
);
|
|
215
|
+
return substrateToolbox as Toolboxes[T];
|
|
216
|
+
})
|
|
217
|
+
.with(Chain.Radix, async () => {
|
|
218
|
+
const { RadixToolbox } = await import("./radix");
|
|
219
|
+
const radixToolbox = await RadixToolbox(params as Parameters<typeof RadixToolbox>[0]);
|
|
220
|
+
return radixToolbox as Toolboxes[T];
|
|
221
|
+
})
|
|
222
|
+
.with(Chain.Ripple, async () => {
|
|
223
|
+
const { getRippleToolbox } = await import("./ripple");
|
|
224
|
+
const rippleToolbox = await getRippleToolbox(params as Parameters<typeof getRippleToolbox>[0]);
|
|
225
|
+
return rippleToolbox as Toolboxes[T];
|
|
226
|
+
})
|
|
227
|
+
.with(Chain.Solana, async () => {
|
|
228
|
+
const { getSolanaToolbox } = await import("./solana");
|
|
229
|
+
const solanaToolbox = await getSolanaToolbox(params as Parameters<typeof getSolanaToolbox>[0]);
|
|
230
|
+
return solanaToolbox as Toolboxes[T];
|
|
231
|
+
})
|
|
232
|
+
.with(Chain.Sui, async () => {
|
|
233
|
+
const { getSuiToolbox } = await import("./sui");
|
|
234
|
+
const suiToolbox = await getSuiToolbox(params as Parameters<typeof getSuiToolbox>[0]);
|
|
235
|
+
return suiToolbox as Toolboxes[T];
|
|
236
|
+
})
|
|
237
|
+
.with(Chain.Tron, async () => {
|
|
238
|
+
const { createTronToolbox } = await import("./tron");
|
|
239
|
+
const tronToolbox = await createTronToolbox(params as Parameters<typeof createTronToolbox>[0]);
|
|
240
|
+
return tronToolbox as Toolboxes[T];
|
|
241
|
+
})
|
|
242
|
+
.with(Chain.Near, async () => {
|
|
243
|
+
const { getNearToolbox } = await import("./near");
|
|
244
|
+
const nearToolbox = await getNearToolbox(params as Parameters<typeof getNearToolbox>[0]);
|
|
245
|
+
return nearToolbox as Toolboxes[T];
|
|
246
|
+
})
|
|
247
|
+
.with(Chain.Cardano, async () => {
|
|
248
|
+
const { getCardanoToolbox } = await import("./cardano");
|
|
249
|
+
const cardanoToolbox = await getCardanoToolbox(params as Parameters<typeof getCardanoToolbox>[0]);
|
|
250
|
+
return cardanoToolbox as Toolboxes[T];
|
|
251
|
+
})
|
|
252
|
+
.with(Chain.Ton, async () => {
|
|
253
|
+
const { getTONToolbox } = await import("./ton");
|
|
254
|
+
const tonToolbox = await getTONToolbox(params as Parameters<typeof getTONToolbox>[0]);
|
|
255
|
+
return tonToolbox as Toolboxes[T];
|
|
256
|
+
})
|
|
257
|
+
.otherwise(() => {
|
|
258
|
+
throw new SwapKitError("toolbox_not_supported", { chain });
|
|
259
|
+
});
|
|
260
|
+
}
|