@swapkit/helpers 2.6.0 → 3.0.0-beta.1
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/api/index.cjs +3 -0
- package/dist/api/index.cjs.map +16 -0
- package/dist/api/index.js +3 -0
- package/dist/api/index.js.map +16 -0
- package/dist/chunk-1gwdyyeh.js +4 -0
- package/dist/chunk-1gwdyyeh.js.map +10 -0
- package/dist/chunk-2bhvezjh.js +4 -0
- package/dist/chunk-2bhvezjh.js.map +10 -0
- package/dist/chunk-3ptwhtpd.js +3 -0
- package/dist/chunk-3ptwhtpd.js.map +10 -0
- package/dist/chunk-3wnfcm30.js +4 -0
- package/dist/chunk-3wnfcm30.js.map +9 -0
- package/dist/chunk-40epvqbm.js +3 -0
- package/dist/chunk-40epvqbm.js.map +10 -0
- package/dist/chunk-49nsjaay.js +3 -0
- package/dist/chunk-49nsjaay.js.map +10 -0
- package/dist/chunk-4b66tryh.js +4 -0
- package/dist/chunk-4b66tryh.js.map +10 -0
- package/dist/chunk-4kzjrrjv.js +3 -0
- package/dist/chunk-4kzjrrjv.js.map +10 -0
- package/dist/chunk-5m49s8z8.js +4 -0
- package/dist/chunk-5m49s8z8.js.map +10 -0
- package/dist/chunk-9b6y7r75.js +4 -0
- package/dist/chunk-9b6y7r75.js.map +10 -0
- package/dist/chunk-9zcbvqq2.js +3 -0
- package/dist/chunk-9zcbvqq2.js.map +10 -0
- package/dist/chunk-a0bpx1q3.js +4 -0
- package/dist/chunk-a0bpx1q3.js.map +10 -0
- package/dist/chunk-a0fxkkfh.js +4 -0
- package/dist/chunk-a0fxkkfh.js.map +10 -0
- package/dist/chunk-a6bjd9ar.js +3 -0
- package/dist/chunk-a6bjd9ar.js.map +10 -0
- package/dist/chunk-bg6pz5qh.js +3 -0
- package/dist/chunk-bg6pz5qh.js.map +10 -0
- package/dist/chunk-cpdcnp2x.js +4 -0
- package/dist/chunk-cpdcnp2x.js.map +10 -0
- package/dist/chunk-fazw0jvt.js +3 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-fr86y3rx.js +4 -0
- package/dist/chunk-fr86y3rx.js.map +10 -0
- package/dist/chunk-ftdtdkhk.js +3 -0
- package/dist/chunk-ftdtdkhk.js.map +10 -0
- package/dist/chunk-gjeaw024.js +4 -0
- package/dist/chunk-gjeaw024.js.map +10 -0
- package/dist/chunk-hdcdd2cf.js +4 -0
- package/dist/chunk-hdcdd2cf.js.map +10 -0
- package/dist/chunk-q2pb6ggs.js +3 -0
- package/dist/chunk-q2pb6ggs.js.map +10 -0
- package/dist/chunk-t9s9811k.js +3 -0
- package/dist/chunk-t9s9811k.js.map +10 -0
- package/dist/chunk-v4dvhh90.js +4 -0
- package/dist/chunk-v4dvhh90.js.map +10 -0
- package/dist/chunk-vzptz52h.js +4 -0
- package/dist/chunk-vzptz52h.js.map +10 -0
- package/dist/chunk-wyr5d8ad.js +3 -0
- package/dist/chunk-wyr5d8ad.js.map +10 -0
- package/dist/chunk-x0jsq6ag.js +3 -0
- package/dist/chunk-x0jsq6ag.js.map +10 -0
- package/dist/chunk-x2fe9scs.js +3 -0
- package/dist/chunk-x2fe9scs.js.map +10 -0
- package/dist/chunk-xm5jkehh.js +4 -0
- package/dist/chunk-xm5jkehh.js.map +10 -0
- package/dist/chunk-xzb1nshn.js +3 -0
- package/dist/chunk-xzb1nshn.js.map +10 -0
- package/dist/contracts/index.cjs +3 -0
- package/dist/contracts/index.cjs.map +38 -0
- package/dist/contracts/index.js +3 -0
- package/dist/contracts/index.js.map +38 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +30 -0
- package/dist/index.js +3 -3
- package/dist/index.js.map +23 -23
- package/dist/tokens/index.cjs +3 -0
- package/dist/tokens/index.cjs.map +10 -0
- package/dist/tokens/index.js +3 -0
- package/dist/tokens/index.js.map +10 -0
- package/package.json +35 -16
- package/src/api/index.ts +19 -0
- package/src/api/microgard/endpoints.ts +83 -0
- package/src/api/microgard/types.ts +60 -0
- package/src/api/midgard/endpoints.ts +154 -0
- package/src/api/midgard/types.ts +155 -0
- package/src/api/swapkitApi/endpoints.ts +161 -0
- package/src/api/swapkitApi/types.ts +576 -0
- package/src/api/thornode/endpoints.ts +78 -0
- package/src/api/thornode/types.ts +241 -0
- package/src/api/thorswapStatic/endpoints.ts +46 -0
- package/src/api/thorswapStatic/types.ts +18 -0
- package/src/contracts/abis/avaxGeneric.ts +92 -0
- package/src/contracts/abis/avaxWoofi.ts +145 -0
- package/src/contracts/abis/bscGeneric.ts +106 -0
- package/src/contracts/abis/chainflipGateway.ts +330 -0
- package/src/contracts/abis/erc20.ts +99 -0
- package/src/contracts/abis/ethGeneric.ts +92 -0
- package/src/contracts/abis/mayaEvmVaults.ts +331 -0
- package/src/contracts/abis/pancakeV2.ts +145 -0
- package/src/contracts/abis/pangolin.ts +120 -0
- package/src/contracts/abis/sushiswap.ts +120 -0
- package/src/contracts/abis/tcEthVault.ts +650 -0
- package/src/contracts/abis/traderJoe.ts +120 -0
- package/src/contracts/abis/uniswapV2.ts +120 -0
- package/src/contracts/abis/uniswapV2Leg.ts +128 -0
- package/src/contracts/abis/uniswapV3_100.ts +128 -0
- package/src/contracts/abis/uniswapV3_10000.ts +128 -0
- package/src/contracts/abis/uniswapV3_3000.ts +128 -0
- package/src/contracts/abis/uniswapV3_500.ts +128 -0
- package/src/contracts/index.ts +95 -0
- package/src/contracts/routers/index.ts +58 -0
- package/src/contracts/routers/kyber.ts +402 -0
- package/src/contracts/routers/oneinch.ts +2188 -0
- package/src/contracts/routers/pancakeswap.ts +340 -0
- package/src/contracts/routers/pangolin.ts +340 -0
- package/src/contracts/routers/sushiswap.ts +340 -0
- package/src/contracts/routers/traderJoe.ts +340 -0
- package/src/contracts/routers/uniswapv2.ts +340 -0
- package/src/contracts/routers/uniswapv3.ts +254 -0
- package/src/contracts/routers/woofi.ts +171 -0
- package/src/index.ts +19 -15
- package/src/modules/__tests__/assetValue.test.ts +50 -24
- package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
- package/src/modules/assetValue.ts +82 -141
- package/src/modules/bigIntArithmetics.ts +6 -2
- package/src/modules/requestClient.ts +53 -65
- package/src/modules/swapKitConfig.ts +134 -0
- package/src/modules/swapKitError.ts +19 -14
- package/src/tokens/index.ts +15 -0
- package/src/tokens/lists/camelot_v3.ts +15920 -0
- package/src/tokens/lists/caviar_v1.ts +1694 -0
- package/src/tokens/lists/chainflip.ts +104 -0
- package/src/tokens/lists/index.ts +14 -0
- package/src/tokens/lists/jupiter.ts +29606 -0
- package/src/tokens/lists/mayachain.ts +513 -0
- package/src/tokens/lists/oneinch.ts +14238 -0
- package/src/tokens/lists/openocean_v2.ts +11514 -0
- package/src/tokens/lists/pancakeswap.ts +4296 -0
- package/src/tokens/lists/pangolin_v1.ts +175 -0
- package/src/tokens/lists/sushiswap_v2.ts +965 -0
- package/src/tokens/lists/thorchain.ts +669 -0
- package/src/tokens/lists/traderjoe_v2.ts +1384 -0
- package/src/tokens/lists/uniswap_v2.ts +5596 -0
- package/src/tokens/lists/uniswap_v3.ts +5946 -0
- package/src/types/chains.ts +48 -51
- package/src/types/commonTypes.ts +3 -90
- package/src/types/derivationPath.ts +11 -24
- package/src/types/index.ts +3 -4
- package/src/types/quotes.ts +0 -1
- package/src/types/radix.ts +2 -2
- package/src/types/sdk.ts +0 -40
- package/src/types/tokens.ts +14 -17
- package/src/types/wallet.ts +70 -33
- package/src/{helpers → utils}/__tests__/asset.test.ts +0 -27
- package/src/utils/asset.ts +235 -0
- package/src/{helpers → utils}/derivationPath.ts +31 -9
- package/src/{helpers → utils}/memo.ts +0 -82
- package/src/{helpers → utils}/others.ts +12 -5
- package/src/utils/plugin.ts +13 -0
- package/src/utils/tokens.ts +73 -0
- package/src/{helpers → utils}/validators.ts +7 -5
- package/src/{helpers/web3wallets.ts → utils/wallets.ts} +189 -145
- package/src/helpers/asset.ts +0 -324
- package/src/modules/walletUtils.ts +0 -30
- package/src/types/network.ts +0 -49
- /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
- /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
- /package/src/{helpers → utils}/liquidity.ts +0 -0
package/src/types/wallet.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import type { CosmosWallets, ThorchainWallets } from "@swapkit/
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
1
|
+
import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolboxes/cosmos";
|
|
2
|
+
import type { EVMToolboxes } from "@swapkit/toolboxes/evm";
|
|
3
|
+
import type { RadixWallet } from "@swapkit/toolboxes/radix";
|
|
4
|
+
import type { RippleWallet } from "@swapkit/toolboxes/ripple";
|
|
5
|
+
import type { SolanaWallet } from "@swapkit/toolboxes/solana";
|
|
6
|
+
import type { SubstrateToolboxes } from "@swapkit/toolboxes/substrate";
|
|
7
|
+
import type { UTXOToolboxes } from "@swapkit/toolboxes/utxo";
|
|
8
|
+
import type { BrowserProvider } from "ethers";
|
|
7
9
|
import type { Eip1193Provider } from "ethers";
|
|
8
10
|
|
|
9
11
|
import type { AssetValue } from "../modules/assetValue";
|
|
10
|
-
import
|
|
11
|
-
import type {
|
|
12
|
+
import { Chain, type ChainId } from "./chains";
|
|
13
|
+
import type { FeeOption } from "./sdk";
|
|
12
14
|
|
|
13
15
|
declare global {
|
|
14
16
|
interface WindowEventMap {
|
|
@@ -16,13 +18,34 @@ declare global {
|
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
export type {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
export type EthereumWindowProvider = BrowserProvider & {
|
|
22
|
+
__XDEFI?: boolean;
|
|
23
|
+
isBraveWallet?: boolean;
|
|
24
|
+
isCoinbaseWallet?: boolean;
|
|
25
|
+
isMetaMask?: boolean;
|
|
26
|
+
isOkxWallet?: boolean;
|
|
27
|
+
isKeepKeyWallet?: boolean;
|
|
28
|
+
isTrust?: boolean;
|
|
29
|
+
isTalisman?: boolean;
|
|
30
|
+
on: (event: string, callback?: () => void) => void;
|
|
31
|
+
overrideIsMetaMask?: boolean;
|
|
32
|
+
request: <T = unknown>(args: {
|
|
33
|
+
method: string;
|
|
34
|
+
params?: unknown[];
|
|
35
|
+
}) => Promise<T>;
|
|
36
|
+
selectedProvider?: EthereumWindowProvider;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type NetworkParams = {
|
|
40
|
+
chainId: ChainId;
|
|
41
|
+
chainName: string;
|
|
42
|
+
nativeCurrency: {
|
|
43
|
+
name: string;
|
|
44
|
+
symbol: string;
|
|
45
|
+
decimals: number;
|
|
46
|
+
};
|
|
47
|
+
rpcUrls: string[];
|
|
48
|
+
blockExplorerUrls: string[];
|
|
26
49
|
};
|
|
27
50
|
|
|
28
51
|
export enum WalletOption {
|
|
@@ -43,6 +66,7 @@ export enum WalletOption {
|
|
|
43
66
|
METAMASK = "METAMASK",
|
|
44
67
|
OKX = "OKX",
|
|
45
68
|
OKX_MOBILE = "OKX_MOBILE",
|
|
69
|
+
ONEKEY = "ONEKEY",
|
|
46
70
|
PHANTOM = "PHANTOM",
|
|
47
71
|
POLKADOT_JS = "POLKADOT_JS",
|
|
48
72
|
RADIX_WALLET = "RADIX_WALLET",
|
|
@@ -64,7 +88,7 @@ export type ChainWallet<T extends Chain> = {
|
|
|
64
88
|
chain: T;
|
|
65
89
|
address: string;
|
|
66
90
|
balance: AssetValue[];
|
|
67
|
-
walletType: WalletOption;
|
|
91
|
+
walletType: WalletOption | string;
|
|
68
92
|
disconnect?: () => void;
|
|
69
93
|
signMessage?: (message: string) => Promise<string>;
|
|
70
94
|
};
|
|
@@ -75,28 +99,19 @@ export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
|
|
|
75
99
|
};
|
|
76
100
|
|
|
77
101
|
export type FullWallet = BaseWallet<
|
|
78
|
-
|
|
79
|
-
|
|
102
|
+
EVMToolboxes &
|
|
103
|
+
UTXOToolboxes &
|
|
80
104
|
CosmosWallets &
|
|
81
105
|
ThorchainWallets &
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
SubstrateToolboxes & {
|
|
107
|
+
[Chain.Radix]: RadixWallet;
|
|
108
|
+
[Chain.Ripple]: RippleWallet;
|
|
109
|
+
[Chain.Solana]: SolanaWallet;
|
|
110
|
+
}
|
|
85
111
|
>;
|
|
86
112
|
|
|
87
|
-
|
|
88
|
-
* @deprecated use FullWallet instead
|
|
89
|
-
*/
|
|
90
|
-
export type Wallet = FullWallet;
|
|
91
|
-
|
|
92
|
-
export type SwapKitWallet<ConnectParams extends any[]> = (
|
|
93
|
-
params: ConnectWalletParams,
|
|
94
|
-
) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;
|
|
95
|
-
|
|
96
|
-
export type SwapKitPluginParams<Config = {}> = {
|
|
113
|
+
export type SwapKitPluginParams = {
|
|
97
114
|
getWallet: <T extends CryptoChain>(chain: T) => FullWallet[T];
|
|
98
|
-
stagenet?: boolean;
|
|
99
|
-
config: Config;
|
|
100
115
|
};
|
|
101
116
|
|
|
102
117
|
export type EIP6963ProviderInfo = {
|
|
@@ -120,3 +135,25 @@ export type EIP6963Provider = {
|
|
|
120
135
|
export type EIP6963AnnounceProviderEvent = Event & {
|
|
121
136
|
detail: EIP6963Provider;
|
|
122
137
|
};
|
|
138
|
+
|
|
139
|
+
export type ChainSigner<T, S> = {
|
|
140
|
+
signTransaction: (params: T) => Promise<S>;
|
|
141
|
+
getAddress: () => Promise<string>;
|
|
142
|
+
sign?: (message: string) => Promise<string>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export type GenericTransferParams = {
|
|
146
|
+
recipient: string;
|
|
147
|
+
assetValue: AssetValue;
|
|
148
|
+
memo?: string;
|
|
149
|
+
feeRate?: number;
|
|
150
|
+
feeOptionKey?: FeeOption;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export type GenericCreateTransactionParams = Omit<
|
|
154
|
+
GenericTransferParams,
|
|
155
|
+
"feeOptionKey" & "feeRate"
|
|
156
|
+
> & {
|
|
157
|
+
sender: string;
|
|
158
|
+
feeRate: number;
|
|
159
|
+
};
|
|
@@ -163,7 +163,6 @@ describe("assetFromString", () => {
|
|
|
163
163
|
symbol: "RUNE",
|
|
164
164
|
ticker: "RUNE",
|
|
165
165
|
synth: false,
|
|
166
|
-
trade: false,
|
|
167
166
|
});
|
|
168
167
|
});
|
|
169
168
|
|
|
@@ -176,31 +175,6 @@ describe("assetFromString", () => {
|
|
|
176
175
|
symbol: "PENDLE-LPT-0x1234",
|
|
177
176
|
ticker: "PENDLE-LPT",
|
|
178
177
|
synth: false,
|
|
179
|
-
trade: false,
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test("should return the correct trade asset", () => {
|
|
184
|
-
const assetString = "ETH~PENDLE-LPT-0x1234";
|
|
185
|
-
const result = assetFromString(assetString);
|
|
186
|
-
expect(result).toEqual({
|
|
187
|
-
chain: Chain.THORChain,
|
|
188
|
-
symbol: "ETH~PENDLE-LPT-0x1234",
|
|
189
|
-
ticker: "PENDLE-LPT",
|
|
190
|
-
synth: false,
|
|
191
|
-
trade: true,
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test("should return the correct synth asset", () => {
|
|
196
|
-
const assetString = "ETH/PENDLE-LPT-0x1234";
|
|
197
|
-
const result = assetFromString(assetString);
|
|
198
|
-
expect(result).toEqual({
|
|
199
|
-
chain: Chain.THORChain,
|
|
200
|
-
symbol: "ETH/PENDLE-LPT-0x1234",
|
|
201
|
-
ticker: "PENDLE-LPT",
|
|
202
|
-
synth: true,
|
|
203
|
-
trade: false,
|
|
204
178
|
});
|
|
205
179
|
});
|
|
206
180
|
|
|
@@ -214,7 +188,6 @@ describe("assetFromString", () => {
|
|
|
214
188
|
symbol: "xwBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
|
|
215
189
|
ticker: "xwBTC",
|
|
216
190
|
synth: false,
|
|
217
|
-
trade: false,
|
|
218
191
|
});
|
|
219
192
|
});
|
|
220
193
|
});
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { match } from "ts-pattern";
|
|
2
|
+
import { AssetValue } from "../modules/assetValue";
|
|
3
|
+
import { RequestClient } from "../modules/requestClient";
|
|
4
|
+
import { SKConfig } from "../modules/swapKitConfig";
|
|
5
|
+
import { BaseDecimal, Chain, type EVMChain, EVMChains, UTXOChains } from "../types/chains";
|
|
6
|
+
import type { RadixCoreStateResourceDTO } from "../types/radix";
|
|
7
|
+
import type { TokenNames } from "../types/tokens";
|
|
8
|
+
|
|
9
|
+
export type CommonAssetString = (typeof CommonAssetStrings)[number] | Chain;
|
|
10
|
+
|
|
11
|
+
export type ConditionalAssetValueReturn<T extends boolean> = T extends true
|
|
12
|
+
? Promise<AssetValue[]>
|
|
13
|
+
: AssetValue[];
|
|
14
|
+
|
|
15
|
+
export const CommonAssetStrings = [
|
|
16
|
+
`${Chain.Maya}.MAYA`,
|
|
17
|
+
`${Chain.Ethereum}.THOR`,
|
|
18
|
+
`${Chain.Ethereum}.vTHOR`,
|
|
19
|
+
`${Chain.Kujira}.USK`,
|
|
20
|
+
`${Chain.Ethereum}.FLIP`,
|
|
21
|
+
`${Chain.Radix}.XRD`,
|
|
22
|
+
] as const;
|
|
23
|
+
|
|
24
|
+
const ethGasChains = [
|
|
25
|
+
Chain.Arbitrum,
|
|
26
|
+
Chain.Avalanche,
|
|
27
|
+
Chain.Base,
|
|
28
|
+
Chain.Ethereum,
|
|
29
|
+
Chain.Optimism,
|
|
30
|
+
Chain.Polygon,
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
async function getContractDecimals({ chain, to }: { chain: EVMChain; to: string }) {
|
|
34
|
+
const getDecimalMethodHex = "0x313ce567";
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const { result } = await RequestClient.post<{ result: string }>(
|
|
38
|
+
SKConfig.get("rpcUrls")[chain],
|
|
39
|
+
{
|
|
40
|
+
headers: {
|
|
41
|
+
accept: "*/*",
|
|
42
|
+
"content-type": "application/json",
|
|
43
|
+
"cache-control": "no-cache",
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
id: 44,
|
|
47
|
+
jsonrpc: "2.0",
|
|
48
|
+
method: "eth_call",
|
|
49
|
+
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, "latest"],
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return Number.parseInt(BigInt(result || BaseDecimal[chain]).toString());
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error);
|
|
57
|
+
return BaseDecimal[chain];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function getRadixAssetDecimal(symbol: string) {
|
|
62
|
+
if (symbol === Chain.Radix) return BaseDecimal.XRD;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const resourceAddress = symbol.split("-")[1]?.toLowerCase();
|
|
66
|
+
|
|
67
|
+
const { manager } = await RequestClient.post<RadixCoreStateResourceDTO>(
|
|
68
|
+
`${SKConfig.get("rpcUrls").XRD}/state/resource`,
|
|
69
|
+
{
|
|
70
|
+
headers: { Accept: "*/*", "Content-Type": "application/json" },
|
|
71
|
+
body: JSON.stringify({
|
|
72
|
+
network: "mainnet",
|
|
73
|
+
resource_address: resourceAddress,
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return manager.divisibility.value.divisibility;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error(error);
|
|
81
|
+
return BaseDecimal[Chain.Radix];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function getEVMAssetDecimal({ chain, symbol }: { chain: EVMChain; symbol: string }) {
|
|
86
|
+
if (EVMChains.includes(symbol as EVMChain)) return BaseDecimal[symbol as EVMChain];
|
|
87
|
+
|
|
88
|
+
const splitSymbol = symbol.split("-");
|
|
89
|
+
const address =
|
|
90
|
+
splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]?.toLowerCase();
|
|
91
|
+
|
|
92
|
+
const decimal = await (address?.startsWith("0x")
|
|
93
|
+
? getContractDecimals({ chain, to: address })
|
|
94
|
+
: BaseDecimal[chain as EVMChain]);
|
|
95
|
+
|
|
96
|
+
return decimal;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function getDecimal({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
100
|
+
return match(chain)
|
|
101
|
+
.with(...EVMChains, (chain) => getEVMAssetDecimal({ chain, symbol }))
|
|
102
|
+
.with(Chain.Radix, () => getRadixAssetDecimal(symbol))
|
|
103
|
+
.otherwise(() => BaseDecimal[chain]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @deprecated Use AssetValue.from({ chain }) instead
|
|
108
|
+
*/
|
|
109
|
+
export function getGasAsset({ chain }: { chain: Chain }) {
|
|
110
|
+
return AssetValue.from({ chain });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function isGasAsset({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
114
|
+
return match(chain)
|
|
115
|
+
.with(...ethGasChains, () => symbol === "ETH")
|
|
116
|
+
.with(Chain.BinanceSmartChain, () => symbol === "BNB")
|
|
117
|
+
.with(Chain.Maya, () => symbol === "CACAO")
|
|
118
|
+
.with(Chain.Cosmos, () => symbol === "ATOM")
|
|
119
|
+
.with(Chain.THORChain, () => symbol === "RUNE")
|
|
120
|
+
.with(Chain.Radix, () => `${chain}.${symbol}` === getCommonAssetInfo(chain).identifier)
|
|
121
|
+
.otherwise(() => symbol === chain);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const getCommonAssetInfo = (assetString: CommonAssetString) => {
|
|
125
|
+
const decimal = BaseDecimal[assetString as Chain];
|
|
126
|
+
|
|
127
|
+
const commonAssetInfo = match(assetString.toUpperCase())
|
|
128
|
+
.with(...ethGasChains, () => ({
|
|
129
|
+
identifier: `${assetString.toUpperCase()}.ETH`,
|
|
130
|
+
decimal,
|
|
131
|
+
}))
|
|
132
|
+
.with(Chain.THORChain, () => ({
|
|
133
|
+
identifier: `${assetString.toUpperCase()}.RUNE`,
|
|
134
|
+
decimal,
|
|
135
|
+
}))
|
|
136
|
+
.with(Chain.Cosmos, () => ({ identifier: `${assetString.toUpperCase()}.ATOM`, decimal }))
|
|
137
|
+
.with(Chain.Maya, () => ({
|
|
138
|
+
identifier: `${assetString.toUpperCase()}.CACAO`,
|
|
139
|
+
decimal: 10,
|
|
140
|
+
}))
|
|
141
|
+
.with(Chain.BinanceSmartChain, () => ({
|
|
142
|
+
identifier: `${assetString.toUpperCase()}.BNB`,
|
|
143
|
+
decimal,
|
|
144
|
+
}))
|
|
145
|
+
.with(
|
|
146
|
+
...UTXOChains,
|
|
147
|
+
Chain.Solana,
|
|
148
|
+
Chain.Chainflip,
|
|
149
|
+
Chain.Kujira,
|
|
150
|
+
Chain.Ripple,
|
|
151
|
+
Chain.Polkadot,
|
|
152
|
+
() => ({ identifier: `${assetString.toUpperCase()}.${assetString.toUpperCase()}`, decimal }),
|
|
153
|
+
)
|
|
154
|
+
.with(Chain.Radix, "XRD.XRD", () => ({
|
|
155
|
+
identifier: "XRD.XRD-resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd",
|
|
156
|
+
decimal,
|
|
157
|
+
}))
|
|
158
|
+
.with("KUJI.USK", () => ({ identifier: assetString.toUpperCase(), decimal: 6 }))
|
|
159
|
+
.with("ETH.FLIP", () => ({
|
|
160
|
+
identifier: "ETH.FLIP-0x826180541412D574cf1336d22c0C0a287822678A",
|
|
161
|
+
decimal: BaseDecimal.ETH,
|
|
162
|
+
}))
|
|
163
|
+
.with("ETH.THOR", () => ({
|
|
164
|
+
identifier: "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
|
|
165
|
+
decimal: BaseDecimal.ETH,
|
|
166
|
+
}))
|
|
167
|
+
.with("ETH.vTHOR", () => ({
|
|
168
|
+
identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
|
|
169
|
+
decimal: BaseDecimal.ETH,
|
|
170
|
+
}))
|
|
171
|
+
.with("MAYA.MAYA", () => ({ identifier: assetString.toUpperCase(), decimal: 4 }))
|
|
172
|
+
// Just to be sure that we are not missing any chain
|
|
173
|
+
.otherwise(() => ({ identifier: assetString, decimal }));
|
|
174
|
+
|
|
175
|
+
return commonAssetInfo;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export function getAssetType({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
179
|
+
if (symbol.includes("/")) return "Synth";
|
|
180
|
+
if (symbol.includes("~")) return "Trade";
|
|
181
|
+
|
|
182
|
+
const isNative = match(chain)
|
|
183
|
+
.with(
|
|
184
|
+
Chain.Radix,
|
|
185
|
+
() => symbol === Chain.Radix || `${chain}.${symbol}` === getCommonAssetInfo(chain).identifier,
|
|
186
|
+
)
|
|
187
|
+
.with(Chain.Arbitrum, Chain.Optimism, Chain.Base, () => symbol === Chain.Ethereum)
|
|
188
|
+
.with(Chain.Cosmos, () => symbol === "ATOM")
|
|
189
|
+
.with(Chain.BinanceSmartChain, () => symbol === "BNB")
|
|
190
|
+
.with(Chain.Maya, () => symbol === "CACAO")
|
|
191
|
+
.with(Chain.THORChain, () => symbol === "RUNE")
|
|
192
|
+
.otherwise(() => symbol === chain);
|
|
193
|
+
|
|
194
|
+
return isNative ? "Native" : chain;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export const assetFromString = (assetString: string) => {
|
|
198
|
+
const [chain, ...symbolArray] = assetString.split(".") as [Chain, ...(string | undefined)[]];
|
|
199
|
+
const synth = assetString.includes("/");
|
|
200
|
+
const symbol = symbolArray.join(".");
|
|
201
|
+
const splitSymbol = symbol?.split("-");
|
|
202
|
+
const ticker = splitSymbol?.length
|
|
203
|
+
? splitSymbol.length === 1
|
|
204
|
+
? splitSymbol[0]
|
|
205
|
+
: splitSymbol.slice(0, -1).join("-")
|
|
206
|
+
: undefined;
|
|
207
|
+
|
|
208
|
+
return { chain, symbol, ticker, synth };
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export async function findAssetBy(
|
|
212
|
+
params:
|
|
213
|
+
| { chain: EVMChain | Chain.Radix | Chain.Solana; contract: string }
|
|
214
|
+
| { identifier: `${Chain}.${string}` },
|
|
215
|
+
) {
|
|
216
|
+
const { tokenLists } = await import("@swapkit/helpers/tokens");
|
|
217
|
+
|
|
218
|
+
for (const tokenList of Object.values(tokenLists)) {
|
|
219
|
+
for (const { identifier, chain: tokenChain, ...rest } of tokenList.tokens) {
|
|
220
|
+
if ("identifier" in params && identifier === params.identifier) {
|
|
221
|
+
return identifier as TokenNames;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (
|
|
225
|
+
"address" in rest &&
|
|
226
|
+
"chain" in params &&
|
|
227
|
+
tokenChain === params.chain &&
|
|
228
|
+
rest.address.toLowerCase() === params.contract.toLowerCase()
|
|
229
|
+
)
|
|
230
|
+
return identifier as TokenNames;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
@@ -13,10 +13,27 @@ type Params = {
|
|
|
13
13
|
type?: "legacy" | "ledgerLive" | "nativeSegwitMiddleAccount" | "segwit" | "account";
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
export function updateDerivationPath(
|
|
17
|
+
path: DerivationPathArray,
|
|
18
|
+
params: { index: number } | { account: number } | { change: number },
|
|
19
|
+
) {
|
|
20
|
+
if ("index" in params) {
|
|
21
|
+
const newPath = [...path.slice(0, path.length - 1), params.index];
|
|
22
|
+
return newPath as DerivationPathArray;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if ("change" in params) {
|
|
26
|
+
const [network, chainId, account, , index] = path;
|
|
27
|
+
return [network, chainId, account, params.change, index] as DerivationPathArray;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if ("account" in params) {
|
|
31
|
+
const [network, chainId, , change, index] = path;
|
|
32
|
+
return [network, chainId, params.account, change, index] as DerivationPathArray;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return path;
|
|
36
|
+
}
|
|
20
37
|
|
|
21
38
|
export function derivationPathToString([network, chainId, account, change, index]:
|
|
22
39
|
| [number, number, number, number, number?]
|
|
@@ -29,15 +46,20 @@ export function derivationPathToString([network, chainId, account, change, index
|
|
|
29
46
|
// TODO @towan - sort this out and make it more readable
|
|
30
47
|
export function getDerivationPathFor({ chain, index, addressIndex = 0, type }: Params) {
|
|
31
48
|
if (EVMChains.includes(chain as EVMChain)) {
|
|
32
|
-
if (type && ["legacy", "account"].includes(type))
|
|
49
|
+
if (type && ["legacy", "account"].includes(type)) {
|
|
33
50
|
return [44, 60, 0, index] as DerivationPathArray;
|
|
34
|
-
|
|
35
|
-
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (type === "ledgerLive") {
|
|
54
|
+
return [44, 60, index, 0, addressIndex] as DerivationPathArray;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
36
58
|
}
|
|
37
59
|
|
|
38
60
|
if (chain === Chain.Solana) {
|
|
39
61
|
if (type === "account") return [44, 501, 0, index] as DerivationPathArray;
|
|
40
|
-
return
|
|
62
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
41
63
|
}
|
|
42
64
|
|
|
43
65
|
const chainId = chain === Chain.Litecoin ? 2 : 0;
|
|
@@ -50,7 +72,7 @@ export function getDerivationPathFor({ chain, index, addressIndex = 0, type }: P
|
|
|
50
72
|
case "legacy":
|
|
51
73
|
return [44, chainId, 0, 0, index] as DerivationPathArray;
|
|
52
74
|
default:
|
|
53
|
-
return
|
|
75
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
54
76
|
}
|
|
55
77
|
}
|
|
56
78
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { SwapKitError } from "../modules/swapKitError";
|
|
2
1
|
import { Chain } from "../types/chains";
|
|
3
2
|
import { MemoType } from "../types/sdk";
|
|
4
3
|
|
|
@@ -108,23 +107,6 @@ export function getMemoForLoan(
|
|
|
108
107
|
return addAffiliate(`${baseMemo}${minAmountPart}`, affiliate);
|
|
109
108
|
}
|
|
110
109
|
|
|
111
|
-
export function getMemoForTcyClaim(
|
|
112
|
-
memoType: MemoType.CLAIM_TCY,
|
|
113
|
-
{ address }: WithAffiliate<{ address: string }>,
|
|
114
|
-
) {
|
|
115
|
-
return `${memoType}:${address}`;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function getMemoForTcyStake(
|
|
119
|
-
memoType: MemoType.STAKE_TCY | MemoType.UNSTAKE_TCY,
|
|
120
|
-
{ unstakeBps, ...affiliate }: WithAffiliate<{ unstakeBps?: number }>,
|
|
121
|
-
) {
|
|
122
|
-
const bps = unstakeBps ? `:${unstakeBps}` : "";
|
|
123
|
-
const baseMemo = `${memoType}${bps}`;
|
|
124
|
-
|
|
125
|
-
return addAffiliate(`${baseMemo}`, affiliate);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
110
|
/**
|
|
129
111
|
* Internal helpers
|
|
130
112
|
*/
|
|
@@ -179,67 +161,3 @@ type WithdrawParams = {
|
|
|
179
161
|
basisPoints: number;
|
|
180
162
|
targetAsset?: string;
|
|
181
163
|
};
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc.
|
|
185
|
-
*/
|
|
186
|
-
export const getMemoFor = <T extends MemoType>(memoType: T, options: any) => {
|
|
187
|
-
switch (memoType) {
|
|
188
|
-
case MemoType.LEAVE:
|
|
189
|
-
case MemoType.BOND: {
|
|
190
|
-
return getMemoForLeaveAndBond({ type: memoType, address: options?.address });
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
case MemoType.UNBOND: {
|
|
194
|
-
return getMemoForUnbond({ address: options?.address, unbondAmount: options?.unbondAmount });
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
case MemoType.NAME_REGISTER: {
|
|
198
|
-
return getMemoForNameRegister(options);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
case MemoType.OPEN_LOAN:
|
|
202
|
-
case MemoType.CLOSE_LOAN: {
|
|
203
|
-
return getMemoForLoan(memoType, options);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
case MemoType.DEPOSIT: {
|
|
207
|
-
const { chain, symbol, address, singleSide } = options;
|
|
208
|
-
|
|
209
|
-
if (singleSide) {
|
|
210
|
-
return getMemoForSaverDeposit({ chain, symbol });
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return getMemoForDeposit({ chain, symbol, address });
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
case MemoType.WITHDRAW: {
|
|
217
|
-
const {
|
|
218
|
-
chain,
|
|
219
|
-
ticker,
|
|
220
|
-
symbol,
|
|
221
|
-
basisPoints,
|
|
222
|
-
targetAssetString: targetAsset,
|
|
223
|
-
singleSide,
|
|
224
|
-
} = options;
|
|
225
|
-
|
|
226
|
-
if (singleSide) {
|
|
227
|
-
return getMemoForSaverWithdraw({ chain, symbol, basisPoints });
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return getMemoForWithdraw({
|
|
231
|
-
chain,
|
|
232
|
-
ticker,
|
|
233
|
-
symbol,
|
|
234
|
-
basisPoints,
|
|
235
|
-
targetAsset,
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
default:
|
|
240
|
-
throw new SwapKitError({
|
|
241
|
-
errorKey: "helpers_invalid_memo_type",
|
|
242
|
-
info: { memoType },
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RequestClient } from "../modules/requestClient";
|
|
2
|
+
import { SKConfig } from "../modules/swapKitConfig";
|
|
2
3
|
import { type ErrorKeys, SwapKitError } from "../modules/swapKitError";
|
|
3
|
-
import { Chain, ChainId, StagenetChain
|
|
4
|
+
import { Chain, ChainId, StagenetChain } from "../types";
|
|
4
5
|
|
|
5
6
|
// 10 rune for register, 1 rune per year
|
|
6
7
|
// MINIMUM_REGISTRATION_FEE = 11
|
|
@@ -53,10 +54,9 @@ export function getChainIdentifier<T extends Chain>(chain: T) {
|
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
const skipWarnings = ["production", "test"].includes(process.env.NODE_ENV || "");
|
|
57
57
|
const warnings = new Set();
|
|
58
58
|
export function warnOnce(condition: boolean, warning: string) {
|
|
59
|
-
if (
|
|
59
|
+
if (condition) {
|
|
60
60
|
if (warnings.has(warning)) {
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
@@ -67,11 +67,18 @@ export function warnOnce(condition: boolean, warning: string) {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
export async function getDynamicChainId(chainId: ChainId = ChainId.THORChain) {
|
|
70
|
-
if (![ChainId.THORChainStagenet, ChainId.THORChain, "thorchain-mainnet-v1"].includes(chainId))
|
|
70
|
+
if (![ChainId.THORChainStagenet, ChainId.THORChain, "thorchain-mainnet-v1"].includes(chainId)) {
|
|
71
71
|
return chainId;
|
|
72
|
+
}
|
|
73
|
+
|
|
72
74
|
try {
|
|
75
|
+
const rpcUrl =
|
|
76
|
+
SKConfig.get("rpcUrls")[
|
|
77
|
+
chainId !== ChainId.THORChain ? StagenetChain.THORChain : Chain.THORChain
|
|
78
|
+
];
|
|
79
|
+
|
|
73
80
|
const response = await RequestClient.get<{ result: { node_info: { network: string } } }>(
|
|
74
|
-
`${
|
|
81
|
+
`${rpcUrl}/status`,
|
|
75
82
|
);
|
|
76
83
|
return response.result.node_info.network as ChainId;
|
|
77
84
|
} catch (_error) {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ProviderName, SwapKitPluginParams } from "../types";
|
|
2
|
+
|
|
3
|
+
export function createPlugin<
|
|
4
|
+
const Name extends string,
|
|
5
|
+
T extends (params: SwapKitPluginParams) => Record<string, unknown>,
|
|
6
|
+
K extends { supportedSwapkitProviders?: (ProviderName | string)[] },
|
|
7
|
+
>({ name, properties, methods }: { name: Name; properties?: K; methods: T }) {
|
|
8
|
+
function plugin(pluginParams: SwapKitPluginParams) {
|
|
9
|
+
return { ...methods(pluginParams), ...properties } as K & ReturnType<T>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return { [name]: plugin } as { [key in Name]: typeof plugin };
|
|
13
|
+
}
|