@swapkit/helpers 3.0.0-beta.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 +2 -2
- package/dist/api/index.cjs.map +6 -6
- package/dist/api/index.js +2 -2
- package/dist/api/index.js.map +6 -6
- 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-nm00d52v.js → chunk-3wnfcm30.js} +2 -2
- package/dist/{chunk-nm00d52v.js.map → chunk-3wnfcm30.js.map} +1 -1
- 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-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.map +2 -2
- package/dist/contracts/index.js +1 -1
- package/dist/contracts/index.js.map +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +19 -18
- package/dist/index.js +3 -3
- package/dist/index.js.map +19 -18
- package/dist/tokens/index.cjs +2 -2
- package/dist/tokens/index.cjs.map +3 -16
- package/dist/tokens/index.js +2 -2
- package/dist/tokens/index.js.map +3 -16
- package/package.json +8 -5
- package/src/api/midgard/endpoints.ts +120 -132
- package/src/api/swapkitApi/endpoints.ts +53 -158
- package/src/api/swapkitApi/types.ts +318 -514
- package/src/api/thorswapStatic/endpoints.ts +18 -2
- package/src/contracts/index.ts +1 -1
- package/src/index.ts +15 -10
- package/src/modules/__tests__/assetValue.test.ts +50 -22
- package/src/modules/__tests__/swapKitConfig.test.ts +3 -3
- package/src/modules/assetValue.ts +77 -67
- package/src/modules/requestClient.ts +52 -55
- package/src/modules/swapKitConfig.ts +8 -5
- package/src/modules/swapKitError.ts +18 -11
- package/src/tokens/lists/index.ts +1 -0
- package/src/types/chains.ts +22 -5
- package/src/types/commonTypes.ts +1 -1
- package/src/types/derivationPath.ts +11 -24
- package/src/types/radix.ts +2 -2
- package/src/types/sdk.ts +0 -7
- package/src/types/tokens.ts +13 -16
- package/src/types/wallet.ts +42 -21
- package/src/utils/asset.ts +118 -217
- package/src/utils/derivationPath.ts +24 -7
- package/src/utils/plugin.ts +1 -1
- package/src/utils/tokens.ts +73 -0
- package/src/utils/validators.ts +3 -2
- package/src/utils/wallets.ts +19 -10
package/src/utils/asset.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { match } from "ts-pattern";
|
|
1
2
|
import { AssetValue } from "../modules/assetValue";
|
|
2
3
|
import { RequestClient } from "../modules/requestClient";
|
|
3
4
|
import { SKConfig } from "../modules/swapKitConfig";
|
|
4
|
-
import { BaseDecimal, Chain, type EVMChain, EVMChains } from "../types/chains";
|
|
5
|
+
import { BaseDecimal, Chain, type EVMChain, EVMChains, UTXOChains } from "../types/chains";
|
|
5
6
|
import type { RadixCoreStateResourceDTO } from "../types/radix";
|
|
6
7
|
import type { TokenNames } from "../types/tokens";
|
|
7
8
|
|
|
8
|
-
const getDecimalMethodHex = "0x313ce567";
|
|
9
|
-
|
|
10
9
|
export type CommonAssetString = (typeof CommonAssetStrings)[number] | Chain;
|
|
11
10
|
|
|
12
11
|
export type ConditionalAssetValueReturn<T extends boolean> = T extends true
|
|
@@ -22,7 +21,18 @@ export const CommonAssetStrings = [
|
|
|
22
21
|
`${Chain.Radix}.XRD`,
|
|
23
22
|
] as const;
|
|
24
23
|
|
|
25
|
-
const
|
|
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
|
+
|
|
26
36
|
try {
|
|
27
37
|
const { result } = await RequestClient.post<{ result: string }>(
|
|
28
38
|
SKConfig.get("rpcUrls")[chain],
|
|
@@ -46,19 +56,18 @@ const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string
|
|
|
46
56
|
console.error(error);
|
|
47
57
|
return BaseDecimal[chain];
|
|
48
58
|
}
|
|
49
|
-
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function getRadixAssetDecimal(symbol: string) {
|
|
62
|
+
if (symbol === Chain.Radix) return BaseDecimal.XRD;
|
|
50
63
|
|
|
51
|
-
const getRadixResourceDecimals = async ({ symbol }: { symbol: string }) => {
|
|
52
64
|
try {
|
|
53
65
|
const resourceAddress = symbol.split("-")[1]?.toLowerCase();
|
|
54
66
|
|
|
55
67
|
const { manager } = await RequestClient.post<RadixCoreStateResourceDTO>(
|
|
56
68
|
`${SKConfig.get("rpcUrls").XRD}/state/resource`,
|
|
57
69
|
{
|
|
58
|
-
headers: {
|
|
59
|
-
Accept: "*/*",
|
|
60
|
-
"Content-Type": "application/json",
|
|
61
|
-
},
|
|
70
|
+
headers: { Accept: "*/*", "Content-Type": "application/json" },
|
|
62
71
|
body: JSON.stringify({
|
|
63
72
|
network: "mainnet",
|
|
64
73
|
resource_address: resourceAddress,
|
|
@@ -71,173 +80,119 @@ const getRadixResourceDecimals = async ({ symbol }: { symbol: string }) => {
|
|
|
71
80
|
console.error(error);
|
|
72
81
|
return BaseDecimal[Chain.Radix];
|
|
73
82
|
}
|
|
74
|
-
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function getEVMAssetDecimal({ chain, symbol }: { chain: EVMChain; symbol: string }) {
|
|
86
|
+
if (EVMChains.includes(symbol as EVMChain)) return BaseDecimal[symbol as EVMChain];
|
|
75
87
|
|
|
76
|
-
const getETHAssetDecimal = (symbol: string) => {
|
|
77
|
-
if (symbol === Chain.Ethereum) return BaseDecimal.ETH;
|
|
78
88
|
const splitSymbol = symbol.split("-");
|
|
79
89
|
const address =
|
|
80
90
|
splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]?.toLowerCase();
|
|
81
91
|
|
|
82
|
-
|
|
83
|
-
? getContractDecimals({ chain
|
|
84
|
-
: BaseDecimal
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const getAVAXAssetDecimal = (symbol: string) => {
|
|
88
|
-
const splitSymbol = symbol.split("-");
|
|
89
|
-
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
|
|
90
|
-
|
|
91
|
-
return address?.startsWith("0x")
|
|
92
|
-
? getContractDecimals({ chain: Chain.Avalanche, to: address.toLowerCase() })
|
|
93
|
-
: BaseDecimal.AVAX;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const getBSCAssetDecimal = (symbol: string) => {
|
|
97
|
-
if (symbol === Chain.BinanceSmartChain) return BaseDecimal.BSC;
|
|
98
|
-
|
|
99
|
-
return BaseDecimal.BSC;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const getRadixAssetDecimal = (symbol: string) => {
|
|
103
|
-
if (symbol === Chain.Radix) return BaseDecimal.XRD;
|
|
92
|
+
const decimal = await (address?.startsWith("0x")
|
|
93
|
+
? getContractDecimals({ chain, to: address })
|
|
94
|
+
: BaseDecimal[chain as EVMChain]);
|
|
104
95
|
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const getDecimal = ({ chain, symbol }: { chain: Chain; symbol: string }) => {
|
|
109
|
-
switch (chain) {
|
|
110
|
-
case Chain.Ethereum:
|
|
111
|
-
return getETHAssetDecimal(symbol);
|
|
112
|
-
case Chain.Avalanche:
|
|
113
|
-
return getAVAXAssetDecimal(symbol);
|
|
114
|
-
case Chain.BinanceSmartChain:
|
|
115
|
-
return getBSCAssetDecimal(symbol);
|
|
116
|
-
case Chain.Radix:
|
|
117
|
-
return getRadixAssetDecimal(symbol);
|
|
118
|
-
default:
|
|
119
|
-
return BaseDecimal[chain];
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export const getGasAsset = ({ chain }: { chain: Chain }) => {
|
|
124
|
-
switch (chain) {
|
|
125
|
-
case Chain.Arbitrum:
|
|
126
|
-
case Chain.Base:
|
|
127
|
-
case Chain.Optimism:
|
|
128
|
-
return AssetValue.from({ asset: `${chain}.ETH` });
|
|
129
|
-
case Chain.Maya:
|
|
130
|
-
return AssetValue.from({ asset: `${chain}.CACAO` });
|
|
131
|
-
case Chain.Cosmos:
|
|
132
|
-
return AssetValue.from({ asset: `${chain}.ATOM` });
|
|
133
|
-
case Chain.BinanceSmartChain:
|
|
134
|
-
return AssetValue.from({ asset: `${chain}.BNB` });
|
|
135
|
-
case Chain.THORChain:
|
|
136
|
-
return AssetValue.from({ asset: `${chain}.RUNE` });
|
|
96
|
+
return decimal;
|
|
97
|
+
}
|
|
137
98
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
+
}
|
|
142
105
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
case Chain.Maya:
|
|
150
|
-
return symbol === "CACAO";
|
|
151
|
-
case Chain.Cosmos:
|
|
152
|
-
return symbol === "ATOM";
|
|
153
|
-
case Chain.BinanceSmartChain:
|
|
154
|
-
return symbol === "BNB";
|
|
155
|
-
case Chain.THORChain:
|
|
156
|
-
return symbol === "RUNE";
|
|
157
|
-
case Chain.Radix:
|
|
158
|
-
return `${chain}.${symbol}` === getCommonAssetInfo(chain).identifier;
|
|
106
|
+
/**
|
|
107
|
+
* @deprecated Use AssetValue.from({ chain }) instead
|
|
108
|
+
*/
|
|
109
|
+
export function getGasAsset({ chain }: { chain: Chain }) {
|
|
110
|
+
return AssetValue.from({ chain });
|
|
111
|
+
}
|
|
159
112
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
+
}
|
|
164
123
|
|
|
165
124
|
export const getCommonAssetInfo = (assetString: CommonAssetString) => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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;
|
|
212
176
|
};
|
|
213
177
|
|
|
214
|
-
export
|
|
178
|
+
export function getAssetType({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
215
179
|
if (symbol.includes("/")) return "Synth";
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
case Chain.Maya:
|
|
233
|
-
return symbol === "CACAO" ? "Native" : chain;
|
|
234
|
-
case Chain.THORChain:
|
|
235
|
-
return symbol === "RUNE" ? "Native" : chain;
|
|
236
|
-
|
|
237
|
-
default:
|
|
238
|
-
return symbol === chain ? "Native" : chain;
|
|
239
|
-
}
|
|
240
|
-
};
|
|
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
|
+
}
|
|
241
196
|
|
|
242
197
|
export const assetFromString = (assetString: string) => {
|
|
243
198
|
const [chain, ...symbolArray] = assetString.split(".") as [Chain, ...(string | undefined)[]];
|
|
@@ -253,36 +208,6 @@ export const assetFromString = (assetString: string) => {
|
|
|
253
208
|
return { chain, symbol, ticker, synth };
|
|
254
209
|
};
|
|
255
210
|
|
|
256
|
-
const potentialScamRegex = new RegExp(
|
|
257
|
-
/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/,
|
|
258
|
-
"gmi",
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
const evmAssetHasAddress = (assetString: string) => {
|
|
262
|
-
const [chain, symbol] = assetString.split(".") as [EVMChain, string];
|
|
263
|
-
if (!EVMChains.includes(chain as EVMChain)) return true;
|
|
264
|
-
const splitSymbol = symbol.split("-");
|
|
265
|
-
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
|
|
266
|
-
|
|
267
|
-
return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
export const filterAssets = (
|
|
271
|
-
tokens: {
|
|
272
|
-
value: string;
|
|
273
|
-
decimal: number;
|
|
274
|
-
chain: Chain;
|
|
275
|
-
symbol: string;
|
|
276
|
-
}[],
|
|
277
|
-
) =>
|
|
278
|
-
tokens.filter(({ chain, value, symbol }) => {
|
|
279
|
-
const assetString = `${chain}.${symbol}`;
|
|
280
|
-
|
|
281
|
-
return (
|
|
282
|
-
!potentialScamRegex.test(assetString) && evmAssetHasAddress(assetString) && value !== "0"
|
|
283
|
-
);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
211
|
export async function findAssetBy(
|
|
287
212
|
params:
|
|
288
213
|
| { chain: EVMChain | Chain.Radix | Chain.Solana; contract: string }
|
|
@@ -308,27 +233,3 @@ export async function findAssetBy(
|
|
|
308
233
|
|
|
309
234
|
return;
|
|
310
235
|
}
|
|
311
|
-
|
|
312
|
-
export const blockTimes = {
|
|
313
|
-
[Chain.Arbitrum]: 1,
|
|
314
|
-
[Chain.Avalanche]: 3,
|
|
315
|
-
[Chain.Base]: 1,
|
|
316
|
-
[Chain.BinanceSmartChain]: 3,
|
|
317
|
-
[Chain.Bitcoin]: 600,
|
|
318
|
-
[Chain.BitcoinCash]: 600,
|
|
319
|
-
[Chain.Chainflip]: 5,
|
|
320
|
-
[Chain.Cosmos]: 1.5,
|
|
321
|
-
[Chain.Dash]: 150,
|
|
322
|
-
[Chain.Dogecoin]: 600,
|
|
323
|
-
[Chain.Ethereum]: 12.5,
|
|
324
|
-
[Chain.Fiat]: 60,
|
|
325
|
-
[Chain.Kujira]: 2.2,
|
|
326
|
-
[Chain.Litecoin]: 150,
|
|
327
|
-
[Chain.Maya]: 6,
|
|
328
|
-
[Chain.Optimism]: 1,
|
|
329
|
-
[Chain.Polkadot]: 6,
|
|
330
|
-
[Chain.Polygon]: 2.1,
|
|
331
|
-
[Chain.Radix]: 5,
|
|
332
|
-
[Chain.Solana]: 1,
|
|
333
|
-
[Chain.THORChain]: 6,
|
|
334
|
-
};
|
|
@@ -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?]
|
|
@@ -37,12 +54,12 @@ export function getDerivationPathFor({ chain, index, addressIndex = 0, type }: P
|
|
|
37
54
|
return [44, 60, index, 0, addressIndex] as DerivationPathArray;
|
|
38
55
|
}
|
|
39
56
|
|
|
40
|
-
return
|
|
57
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
41
58
|
}
|
|
42
59
|
|
|
43
60
|
if (chain === Chain.Solana) {
|
|
44
61
|
if (type === "account") return [44, 501, 0, index] as DerivationPathArray;
|
|
45
|
-
return
|
|
62
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
46
63
|
}
|
|
47
64
|
|
|
48
65
|
const chainId = chain === Chain.Litecoin ? 2 : 0;
|
|
@@ -55,7 +72,7 @@ export function getDerivationPathFor({ chain, index, addressIndex = 0, type }: P
|
|
|
55
72
|
case "legacy":
|
|
56
73
|
return [44, chainId, 0, 0, index] as DerivationPathArray;
|
|
57
74
|
default:
|
|
58
|
-
return
|
|
75
|
+
return updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
59
76
|
}
|
|
60
77
|
}
|
|
61
78
|
|
package/src/utils/plugin.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ProviderName, SwapKitPluginParams } from "../types";
|
|
|
3
3
|
export function createPlugin<
|
|
4
4
|
const Name extends string,
|
|
5
5
|
T extends (params: SwapKitPluginParams) => Record<string, unknown>,
|
|
6
|
-
K extends { supportedSwapkitProviders?: ProviderName[] },
|
|
6
|
+
K extends { supportedSwapkitProviders?: (ProviderName | string)[] },
|
|
7
7
|
>({ name, properties, methods }: { name: Name; properties?: K; methods: T }) {
|
|
8
8
|
function plugin(pluginParams: SwapKitPluginParams) {
|
|
9
9
|
return { ...methods(pluginParams), ...properties } as K & ReturnType<T>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { match } from "ts-pattern";
|
|
2
|
+
import type * as tokenLists from "../tokens/lists";
|
|
3
|
+
|
|
4
|
+
export type TokenLists = {
|
|
5
|
+
camelot: typeof tokenLists.CamelotV3List;
|
|
6
|
+
caviar: typeof tokenLists.CaviarV1List;
|
|
7
|
+
chainflip: typeof tokenLists.ChainflipList;
|
|
8
|
+
jupiter: typeof tokenLists.JupiterList;
|
|
9
|
+
mayachain: typeof tokenLists.MayaList;
|
|
10
|
+
oneinch: typeof tokenLists.OneInchList;
|
|
11
|
+
openocean: typeof tokenLists.OpenOceanV2List;
|
|
12
|
+
pancakeswap: typeof tokenLists.PancakeswapList;
|
|
13
|
+
pangolin: typeof tokenLists.PangolinList;
|
|
14
|
+
sushiswap: typeof tokenLists.SushiswapList;
|
|
15
|
+
thorchain: typeof tokenLists.ThorchainList;
|
|
16
|
+
traderjoe: typeof tokenLists.TraderjoeV2List;
|
|
17
|
+
uniswap: typeof tokenLists.UniswapV2List;
|
|
18
|
+
uniswapv3: typeof tokenLists.UniswapV3List;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type TokenListName = keyof TokenLists;
|
|
22
|
+
|
|
23
|
+
const defaultLists = [
|
|
24
|
+
"camelot",
|
|
25
|
+
"caviar",
|
|
26
|
+
"chainflip",
|
|
27
|
+
"jupiter",
|
|
28
|
+
"mayachain",
|
|
29
|
+
"oneinch",
|
|
30
|
+
"openocean",
|
|
31
|
+
"pancakeswap",
|
|
32
|
+
"pangolin",
|
|
33
|
+
"sushiswap",
|
|
34
|
+
"thorchain",
|
|
35
|
+
"traderjoe",
|
|
36
|
+
"uniswap",
|
|
37
|
+
"uniswapv3",
|
|
38
|
+
] as TokenListName[];
|
|
39
|
+
|
|
40
|
+
export async function loadTokenLists<T extends TokenListName[]>(pickedLists?: T) {
|
|
41
|
+
const listsToLoad = pickedLists || defaultLists;
|
|
42
|
+
const lists = {} as { [key in T[number]]: TokenLists[key] };
|
|
43
|
+
|
|
44
|
+
for (const list of listsToLoad) {
|
|
45
|
+
const tokenList = await loadTokenList(list);
|
|
46
|
+
|
|
47
|
+
// @ts-expect-error - It's fine to do this because we know the type of the list
|
|
48
|
+
lists[list] = tokenList;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return lists;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function loadTokenList<T extends TokenListName>(listName: T): Promise<TokenLists[T]> {
|
|
55
|
+
const { list } = await match(listName as TokenListName)
|
|
56
|
+
.with("camelot", () => import("../tokens/lists/camelot_v3"))
|
|
57
|
+
.with("caviar", () => import("../tokens/lists/caviar_v1"))
|
|
58
|
+
.with("chainflip", () => import("../tokens/lists/chainflip"))
|
|
59
|
+
.with("jupiter", () => import("../tokens/lists/jupiter"))
|
|
60
|
+
.with("mayachain", () => import("../tokens/lists/mayachain"))
|
|
61
|
+
.with("oneinch", () => import("../tokens/lists/oneinch"))
|
|
62
|
+
.with("openocean", () => import("../tokens/lists/openocean_v2"))
|
|
63
|
+
.with("pancakeswap", () => import("../tokens/lists/pancakeswap"))
|
|
64
|
+
.with("pangolin", () => import("../tokens/lists/pangolin_v1"))
|
|
65
|
+
.with("sushiswap", () => import("../tokens/lists/sushiswap_v2"))
|
|
66
|
+
.with("thorchain", () => import("../tokens/lists/thorchain"))
|
|
67
|
+
.with("traderjoe", () => import("../tokens/lists/traderjoe_v2"))
|
|
68
|
+
.with("uniswap", () => import("../tokens/lists/uniswap_v2"))
|
|
69
|
+
.with("uniswapv3", () => import("../tokens/lists/uniswap_v3"))
|
|
70
|
+
.exhaustive();
|
|
71
|
+
|
|
72
|
+
return list as unknown as TokenLists[T];
|
|
73
|
+
}
|
package/src/utils/validators.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { SKConfig } from "../modules/swapKitConfig";
|
|
1
2
|
import { SwapKitError } from "../modules/swapKitError";
|
|
2
|
-
import { Chain } from "../types/chains";
|
|
3
|
+
import type { Chain } from "../types/chains";
|
|
3
4
|
|
|
4
5
|
// Backward compatibility
|
|
5
|
-
const supportedChains = [...
|
|
6
|
+
const supportedChains = ["TERRA", ...SKConfig.get("chains")];
|
|
6
7
|
|
|
7
8
|
export function validateIdentifier(identifier = "") {
|
|
8
9
|
const uppercasedIdentifier = identifier.toUpperCase();
|
package/src/utils/wallets.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { getEvmToolbox } from "@swapkit/toolboxes/evm";
|
|
2
|
+
import type { BrowserProvider, JsonRpcProvider } from "ethers";
|
|
2
3
|
import { SwapKitError } from "../modules/swapKitError";
|
|
3
4
|
import {
|
|
4
5
|
type AddChainType,
|
|
@@ -19,6 +20,7 @@ declare const window: {
|
|
|
19
20
|
braveSolana: any;
|
|
20
21
|
bitkeep?: { ethereum: EthereumWindowProvider };
|
|
21
22
|
xfi?: { ethereum: EthereumWindowProvider };
|
|
23
|
+
$onekey?: { ethereum: EthereumWindowProvider };
|
|
22
24
|
} & Window;
|
|
23
25
|
|
|
24
26
|
export function isWeb3Detected() {
|
|
@@ -40,6 +42,7 @@ export function listWeb3EVMWallets() {
|
|
|
40
42
|
window?.ethereum?.selectedProvider?.isCoinbaseWallet) ||
|
|
41
43
|
window?.coinbaseWalletExtension;
|
|
42
44
|
const bitgetEnabled = window?.bitkeep?.ethereum;
|
|
45
|
+
const onekeyEnabled = window?.$onekey?.ethereum;
|
|
43
46
|
|
|
44
47
|
const wallets = [];
|
|
45
48
|
if (metamaskEnabled) wallets.push(WalletOption.METAMASK);
|
|
@@ -49,6 +52,7 @@ export function listWeb3EVMWallets() {
|
|
|
49
52
|
if (coinbaseEnabled) wallets.push(WalletOption.COINBASE_WEB);
|
|
50
53
|
if (okxMobileEnabled()) wallets.push(WalletOption.OKX_MOBILE);
|
|
51
54
|
if (bitgetEnabled) wallets.push(WalletOption.BITGET);
|
|
55
|
+
if (onekeyEnabled) wallets.push(WalletOption.ONEKEY);
|
|
52
56
|
|
|
53
57
|
return wallets;
|
|
54
58
|
}
|
|
@@ -116,12 +120,15 @@ export function wrapMethodWithNetworkSwitch<T extends (...args: any[]) => any>(
|
|
|
116
120
|
}) as unknown as T;
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
export function prepareNetworkSwitch<
|
|
123
|
+
export function prepareNetworkSwitch<
|
|
124
|
+
T extends Awaited<ReturnType<typeof getEvmToolbox>>,
|
|
125
|
+
M extends keyof T,
|
|
126
|
+
>({
|
|
120
127
|
toolbox,
|
|
121
128
|
chain,
|
|
122
129
|
provider = window.ethereum,
|
|
123
130
|
methodNames = [],
|
|
124
|
-
}: { toolbox: T; chain: Chain; provider?: BrowserProvider; methodNames?:
|
|
131
|
+
}: { toolbox: T; chain: Chain; provider?: BrowserProvider | JsonRpcProvider; methodNames?: M[] }) {
|
|
125
132
|
const methodsToWrap = [
|
|
126
133
|
...methodNames,
|
|
127
134
|
"approve",
|
|
@@ -138,18 +145,19 @@ export function prepareNetworkSwitch<T extends { [key: string]: (...args: any[])
|
|
|
138
145
|
"estimateGasLimit",
|
|
139
146
|
"estimateGasPrices",
|
|
140
147
|
"createContractTxObject",
|
|
141
|
-
];
|
|
148
|
+
] as M[];
|
|
142
149
|
const wrappedMethods = methodsToWrap.reduce((object, methodName) => {
|
|
143
150
|
if (!toolbox[methodName]) return object;
|
|
151
|
+
|
|
144
152
|
const method = toolbox[methodName];
|
|
145
153
|
|
|
146
154
|
if (typeof method !== "function") return object;
|
|
147
155
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
};
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
const wrappedMethod = wrapMethodWithNetworkSwitch(method, provider, chain);
|
|
158
|
+
|
|
159
|
+
// biome-ignore lint/performance/noAccumulatingSpread: valid use case
|
|
160
|
+
return { ...object, [methodName]: wrappedMethod };
|
|
153
161
|
}, {});
|
|
154
162
|
|
|
155
163
|
return { ...toolbox, ...wrappedMethods };
|
|
@@ -171,6 +179,7 @@ export function getETHDefaultWallet() {
|
|
|
171
179
|
if (isBraveWallet) return WalletOption.BRAVE;
|
|
172
180
|
if (overrideIsMetaMask && selectedProvider?.isCoinbaseWallet) return WalletOption.COINBASE_WEB;
|
|
173
181
|
if (__XDEFI) WalletOption.CTRL;
|
|
182
|
+
if (window?.$onekey?.ethereum) return WalletOption.ONEKEY;
|
|
174
183
|
return WalletOption.METAMASK;
|
|
175
184
|
}
|
|
176
185
|
|
|
@@ -220,7 +229,7 @@ export function createWallet<
|
|
|
220
229
|
}) => (...params: ConnectParams) => Promise<boolean>;
|
|
221
230
|
name: Name;
|
|
222
231
|
supportedChains: SupportedChains;
|
|
223
|
-
walletType?: WalletType;
|
|
232
|
+
walletType?: WalletType | string;
|
|
224
233
|
}) {
|
|
225
234
|
function connectWallet(connectParams: {
|
|
226
235
|
addChain: AddChainType;
|