@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.
Files changed (167) hide show
  1. package/dist/api/index.cjs +3 -0
  2. package/dist/api/index.cjs.map +16 -0
  3. package/dist/api/index.js +3 -0
  4. package/dist/api/index.js.map +16 -0
  5. package/dist/chunk-1gwdyyeh.js +4 -0
  6. package/dist/chunk-1gwdyyeh.js.map +10 -0
  7. package/dist/chunk-2bhvezjh.js +4 -0
  8. package/dist/chunk-2bhvezjh.js.map +10 -0
  9. package/dist/chunk-3ptwhtpd.js +3 -0
  10. package/dist/chunk-3ptwhtpd.js.map +10 -0
  11. package/dist/chunk-3wnfcm30.js +4 -0
  12. package/dist/chunk-3wnfcm30.js.map +9 -0
  13. package/dist/chunk-40epvqbm.js +3 -0
  14. package/dist/chunk-40epvqbm.js.map +10 -0
  15. package/dist/chunk-49nsjaay.js +3 -0
  16. package/dist/chunk-49nsjaay.js.map +10 -0
  17. package/dist/chunk-4b66tryh.js +4 -0
  18. package/dist/chunk-4b66tryh.js.map +10 -0
  19. package/dist/chunk-4kzjrrjv.js +3 -0
  20. package/dist/chunk-4kzjrrjv.js.map +10 -0
  21. package/dist/chunk-5m49s8z8.js +4 -0
  22. package/dist/chunk-5m49s8z8.js.map +10 -0
  23. package/dist/chunk-9b6y7r75.js +4 -0
  24. package/dist/chunk-9b6y7r75.js.map +10 -0
  25. package/dist/chunk-9zcbvqq2.js +3 -0
  26. package/dist/chunk-9zcbvqq2.js.map +10 -0
  27. package/dist/chunk-a0bpx1q3.js +4 -0
  28. package/dist/chunk-a0bpx1q3.js.map +10 -0
  29. package/dist/chunk-a0fxkkfh.js +4 -0
  30. package/dist/chunk-a0fxkkfh.js.map +10 -0
  31. package/dist/chunk-a6bjd9ar.js +3 -0
  32. package/dist/chunk-a6bjd9ar.js.map +10 -0
  33. package/dist/chunk-bg6pz5qh.js +3 -0
  34. package/dist/chunk-bg6pz5qh.js.map +10 -0
  35. package/dist/chunk-cpdcnp2x.js +4 -0
  36. package/dist/chunk-cpdcnp2x.js.map +10 -0
  37. package/dist/chunk-fazw0jvt.js +3 -0
  38. package/dist/chunk-fazw0jvt.js.map +9 -0
  39. package/dist/chunk-fr86y3rx.js +4 -0
  40. package/dist/chunk-fr86y3rx.js.map +10 -0
  41. package/dist/chunk-ftdtdkhk.js +3 -0
  42. package/dist/chunk-ftdtdkhk.js.map +10 -0
  43. package/dist/chunk-gjeaw024.js +4 -0
  44. package/dist/chunk-gjeaw024.js.map +10 -0
  45. package/dist/chunk-hdcdd2cf.js +4 -0
  46. package/dist/chunk-hdcdd2cf.js.map +10 -0
  47. package/dist/chunk-q2pb6ggs.js +3 -0
  48. package/dist/chunk-q2pb6ggs.js.map +10 -0
  49. package/dist/chunk-t9s9811k.js +3 -0
  50. package/dist/chunk-t9s9811k.js.map +10 -0
  51. package/dist/chunk-v4dvhh90.js +4 -0
  52. package/dist/chunk-v4dvhh90.js.map +10 -0
  53. package/dist/chunk-vzptz52h.js +4 -0
  54. package/dist/chunk-vzptz52h.js.map +10 -0
  55. package/dist/chunk-wyr5d8ad.js +3 -0
  56. package/dist/chunk-wyr5d8ad.js.map +10 -0
  57. package/dist/chunk-x0jsq6ag.js +3 -0
  58. package/dist/chunk-x0jsq6ag.js.map +10 -0
  59. package/dist/chunk-x2fe9scs.js +3 -0
  60. package/dist/chunk-x2fe9scs.js.map +10 -0
  61. package/dist/chunk-xm5jkehh.js +4 -0
  62. package/dist/chunk-xm5jkehh.js.map +10 -0
  63. package/dist/chunk-xzb1nshn.js +3 -0
  64. package/dist/chunk-xzb1nshn.js.map +10 -0
  65. package/dist/contracts/index.cjs +3 -0
  66. package/dist/contracts/index.cjs.map +38 -0
  67. package/dist/contracts/index.js +3 -0
  68. package/dist/contracts/index.js.map +38 -0
  69. package/dist/index.cjs +6 -0
  70. package/dist/index.cjs.map +30 -0
  71. package/dist/index.js +3 -3
  72. package/dist/index.js.map +23 -23
  73. package/dist/tokens/index.cjs +3 -0
  74. package/dist/tokens/index.cjs.map +10 -0
  75. package/dist/tokens/index.js +3 -0
  76. package/dist/tokens/index.js.map +10 -0
  77. package/package.json +35 -16
  78. package/src/api/index.ts +19 -0
  79. package/src/api/microgard/endpoints.ts +83 -0
  80. package/src/api/microgard/types.ts +60 -0
  81. package/src/api/midgard/endpoints.ts +154 -0
  82. package/src/api/midgard/types.ts +155 -0
  83. package/src/api/swapkitApi/endpoints.ts +161 -0
  84. package/src/api/swapkitApi/types.ts +576 -0
  85. package/src/api/thornode/endpoints.ts +78 -0
  86. package/src/api/thornode/types.ts +241 -0
  87. package/src/api/thorswapStatic/endpoints.ts +46 -0
  88. package/src/api/thorswapStatic/types.ts +18 -0
  89. package/src/contracts/abis/avaxGeneric.ts +92 -0
  90. package/src/contracts/abis/avaxWoofi.ts +145 -0
  91. package/src/contracts/abis/bscGeneric.ts +106 -0
  92. package/src/contracts/abis/chainflipGateway.ts +330 -0
  93. package/src/contracts/abis/erc20.ts +99 -0
  94. package/src/contracts/abis/ethGeneric.ts +92 -0
  95. package/src/contracts/abis/mayaEvmVaults.ts +331 -0
  96. package/src/contracts/abis/pancakeV2.ts +145 -0
  97. package/src/contracts/abis/pangolin.ts +120 -0
  98. package/src/contracts/abis/sushiswap.ts +120 -0
  99. package/src/contracts/abis/tcEthVault.ts +650 -0
  100. package/src/contracts/abis/traderJoe.ts +120 -0
  101. package/src/contracts/abis/uniswapV2.ts +120 -0
  102. package/src/contracts/abis/uniswapV2Leg.ts +128 -0
  103. package/src/contracts/abis/uniswapV3_100.ts +128 -0
  104. package/src/contracts/abis/uniswapV3_10000.ts +128 -0
  105. package/src/contracts/abis/uniswapV3_3000.ts +128 -0
  106. package/src/contracts/abis/uniswapV3_500.ts +128 -0
  107. package/src/contracts/index.ts +95 -0
  108. package/src/contracts/routers/index.ts +58 -0
  109. package/src/contracts/routers/kyber.ts +402 -0
  110. package/src/contracts/routers/oneinch.ts +2188 -0
  111. package/src/contracts/routers/pancakeswap.ts +340 -0
  112. package/src/contracts/routers/pangolin.ts +340 -0
  113. package/src/contracts/routers/sushiswap.ts +340 -0
  114. package/src/contracts/routers/traderJoe.ts +340 -0
  115. package/src/contracts/routers/uniswapv2.ts +340 -0
  116. package/src/contracts/routers/uniswapv3.ts +254 -0
  117. package/src/contracts/routers/woofi.ts +171 -0
  118. package/src/index.ts +19 -15
  119. package/src/modules/__tests__/assetValue.test.ts +50 -24
  120. package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
  121. package/src/modules/assetValue.ts +82 -141
  122. package/src/modules/bigIntArithmetics.ts +6 -2
  123. package/src/modules/requestClient.ts +53 -65
  124. package/src/modules/swapKitConfig.ts +134 -0
  125. package/src/modules/swapKitError.ts +19 -14
  126. package/src/tokens/index.ts +15 -0
  127. package/src/tokens/lists/camelot_v3.ts +15920 -0
  128. package/src/tokens/lists/caviar_v1.ts +1694 -0
  129. package/src/tokens/lists/chainflip.ts +104 -0
  130. package/src/tokens/lists/index.ts +14 -0
  131. package/src/tokens/lists/jupiter.ts +29606 -0
  132. package/src/tokens/lists/mayachain.ts +513 -0
  133. package/src/tokens/lists/oneinch.ts +14238 -0
  134. package/src/tokens/lists/openocean_v2.ts +11514 -0
  135. package/src/tokens/lists/pancakeswap.ts +4296 -0
  136. package/src/tokens/lists/pangolin_v1.ts +175 -0
  137. package/src/tokens/lists/sushiswap_v2.ts +965 -0
  138. package/src/tokens/lists/thorchain.ts +669 -0
  139. package/src/tokens/lists/traderjoe_v2.ts +1384 -0
  140. package/src/tokens/lists/uniswap_v2.ts +5596 -0
  141. package/src/tokens/lists/uniswap_v3.ts +5946 -0
  142. package/src/types/chains.ts +48 -51
  143. package/src/types/commonTypes.ts +3 -90
  144. package/src/types/derivationPath.ts +11 -24
  145. package/src/types/index.ts +3 -4
  146. package/src/types/quotes.ts +0 -1
  147. package/src/types/radix.ts +2 -2
  148. package/src/types/sdk.ts +0 -40
  149. package/src/types/tokens.ts +14 -17
  150. package/src/types/wallet.ts +70 -33
  151. package/src/{helpers → utils}/__tests__/asset.test.ts +0 -27
  152. package/src/utils/asset.ts +235 -0
  153. package/src/{helpers → utils}/derivationPath.ts +31 -9
  154. package/src/{helpers → utils}/memo.ts +0 -82
  155. package/src/{helpers → utils}/others.ts +12 -5
  156. package/src/utils/plugin.ts +13 -0
  157. package/src/utils/tokens.ts +73 -0
  158. package/src/{helpers → utils}/validators.ts +7 -5
  159. package/src/{helpers/web3wallets.ts → utils/wallets.ts} +189 -145
  160. package/src/helpers/asset.ts +0 -324
  161. package/src/modules/walletUtils.ts +0 -30
  162. package/src/types/network.ts +0 -49
  163. /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
  164. /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
  165. /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
  166. /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
  167. /package/src/{helpers → utils}/liquidity.ts +0 -0
@@ -1,14 +1,16 @@
1
- import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
2
- import type { EVMWallets } from "@swapkit/toolbox-evm";
3
- import type { RadixWallets } from "@swapkit/toolbox-radix";
4
- import type { SolanaWallets } from "@swapkit/toolbox-solana";
5
- import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
6
- import type { UTXOWallets } from "@swapkit/toolbox-utxo";
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 type { Chain } from "./chains";
11
- import type { ConnectWalletParams } from "./commonTypes";
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
- CosmosWallets,
21
- ThorchainWallets,
22
- EVMWallets,
23
- SubstrateWallets,
24
- UTXOWallets,
25
- SolanaWallets,
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
- EVMWallets &
79
- UTXOWallets &
102
+ EVMToolboxes &
103
+ UTXOToolboxes &
80
104
  CosmosWallets &
81
105
  ThorchainWallets &
82
- SubstrateWallets &
83
- SolanaWallets &
84
- RadixWallets
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 const updatedLastIndex = (path: DerivationPathArray, index: number) => {
17
- const newPath = [...path.slice(0, path.length - 1), index];
18
- return newPath as DerivationPathArray;
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
- if (type === "ledgerLive") return [44, 60, index, 0, addressIndex] as DerivationPathArray;
35
- return updatedLastIndex(NetworkDerivationPath[chain], index);
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 updatedLastIndex(NetworkDerivationPath[chain], index);
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 updatedLastIndex(NetworkDerivationPath[chain], index);
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, getRPCUrl } from "../types";
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 (!skipWarnings && condition) {
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
- `${chainId !== ChainId.THORChain ? getRPCUrl(StagenetChain.THORChain) : getRPCUrl(Chain.THORChain)}/status`,
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
+ }