@swapkit/core 1.0.0-rc.89 → 1.0.0-rc.90

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/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "author": "swapkit-oss-team",
3
3
  "dependencies": {
4
- "@swapkit/helpers": "1.0.0-rc.64",
5
- "@swapkit/types": "1.0.0-rc.33"
4
+ "@swapkit/helpers": "1.0.0-rc.65",
5
+ "@swapkit/types": "1.0.0-rc.34"
6
6
  },
7
7
  "description": "SwapKit Lib core",
8
8
  "devDependencies": {
@@ -11,15 +11,15 @@
11
11
  "vitest": "1.2.1",
12
12
  "@internal/config": "0.0.2-rc.0",
13
13
  "@swapkit/tokens": "1.0.0-rc.33",
14
- "@swapkit/toolbox-cosmos": "1.0.0-rc.72",
15
- "@swapkit/toolbox-evm": "1.0.0-rc.70",
16
- "@swapkit/toolbox-utxo": "1.0.0-rc.69"
14
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.73",
15
+ "@swapkit/toolbox-evm": "1.0.0-rc.71",
16
+ "@swapkit/toolbox-utxo": "1.0.0-rc.70"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "@swapkit/tokens": "1.0.0-rc.33",
20
- "@swapkit/toolbox-cosmos": "1.0.0-rc.72",
21
- "@swapkit/toolbox-evm": "1.0.0-rc.70",
22
- "@swapkit/toolbox-utxo": "1.0.0-rc.69"
20
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.73",
21
+ "@swapkit/toolbox-evm": "1.0.0-rc.71",
22
+ "@swapkit/toolbox-utxo": "1.0.0-rc.70"
23
23
  },
24
24
  "eslintConfig": {
25
25
  "extends": "../../../internal/eslint-config"
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "type": "module",
49
49
  "types": "./dist/index.d.ts",
50
- "version": "1.0.0-rc.89",
50
+ "version": "1.0.0-rc.90",
51
51
  "scripts": {
52
52
  "build": "NODE_OPTIONS=--max_old_space_size=16384 vite build",
53
53
  "clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
@@ -1,4 +1,4 @@
1
- import type { Keys, ThornameRegisterParam } from '@swapkit/helpers';
1
+ import type { ErrorKeys, ThornameRegisterParam } from '@swapkit/helpers';
2
2
  import {
3
3
  AssetValue,
4
4
  gasFeeMultiplier,
@@ -101,112 +101,109 @@ export class SwapKitCore<T = ''> {
101
101
 
102
102
  if (!route.complete) throw new SwapKitError('core_swap_route_not_complete');
103
103
 
104
- try {
105
- // TODO enable when BE is ready
106
- // if (contractCallParams && evmChain) {
107
- // const walletMethods = this.connectedWallets[evmChain];
108
-
109
- // if (!walletMethods?.call) {
110
- // throw new SwapKitError('core_wallet_connection_not_found');
111
- // }
112
-
113
- // const { contractAddress, contractMethod, contractParams, contractParamsStreaming } =
114
- // contractCallParams;
115
-
116
- // if (!(streamSwap ? contractParamsStreaming : contractParams)) {
117
- // throw new SwapKitError('core_swap_route_transaction_not_found');
118
- // }
119
-
120
- // return await walletMethods.call<string>({
121
- // contractAddress,
122
- // abi: lowercasedContractAbiMapping[contractAddress.toLowerCase()],
123
- // funcName: contractMethod,
124
- // funcParams: streamSwap ? contractParamsStreaming : contractParams,
125
- // });
126
- // }
127
-
128
- if (AGG_SWAP.includes(quoteMode) && evmChain) {
129
- const walletMethods = this.connectedWallets[evmChain];
130
- if (!walletMethods?.sendTransaction) {
131
- throw new SwapKitError('core_wallet_connection_not_found');
132
- }
104
+ // TODO enable when BE is ready
105
+ // if (contractCallParams && evmChain) {
106
+ // const walletMethods = this.connectedWallets[evmChain];
107
+
108
+ // if (!walletMethods?.call) {
109
+ // throw new SwapKitError('core_wallet_connection_not_found');
110
+ // }
111
+
112
+ // const { contractAddress, contractMethod, contractParams, contractParamsStreaming } =
113
+ // contractCallParams;
114
+
115
+ // if (!(streamSwap ? contractParamsStreaming : contractParams)) {
116
+ // throw new SwapKitError('core_swap_route_transaction_not_found');
117
+ // }
118
+
119
+ // return await walletMethods.call<string>({
120
+ // contractAddress,
121
+ // abi: lowercasedContractAbiMapping[contractAddress.toLowerCase()],
122
+ // funcName: contractMethod,
123
+ // funcParams: streamSwap ? contractParamsStreaming : contractParams,
124
+ // });
125
+ // }
126
+
127
+ if (AGG_SWAP.includes(quoteMode) && evmChain) {
128
+ const walletMethods = this.connectedWallets[evmChain];
129
+ if (!walletMethods?.sendTransaction) {
130
+ throw new SwapKitError('core_wallet_connection_not_found');
131
+ }
133
132
 
134
- const transaction = streamSwap ? route?.streamingSwap?.transaction : route?.transaction;
135
- if (!transaction) throw new SwapKitError('core_swap_route_transaction_not_found');
133
+ const transaction = streamSwap ? route?.streamingSwap?.transaction : route?.transaction;
134
+ if (!transaction) throw new SwapKitError('core_swap_route_transaction_not_found');
136
135
 
137
- const { data, from, to, value } = route.transaction;
136
+ const { data, from, to, value } = route.transaction;
138
137
 
139
- const params = {
140
- data,
141
- from,
142
- to: to.toLowerCase(),
143
- chainId: BigInt(ChainToChainId[evmChain]),
144
- value: value ? BigInt(value) : 0n,
145
- };
138
+ const params = {
139
+ data,
140
+ from,
141
+ to: to.toLowerCase(),
142
+ chainId: BigInt(ChainToChainId[evmChain]),
143
+ value: value ? BigInt(value) : 0n,
144
+ };
146
145
 
147
- return walletMethods.sendTransaction(params, feeOptionKey) as Promise<string>;
148
- }
146
+ return walletMethods.sendTransaction(params, feeOptionKey) as Promise<string>;
147
+ }
149
148
 
150
- if (SWAP_OUT.includes(quoteMode)) {
151
- if (!route.calldata.fromAsset) throw new SwapKitError('core_swap_asset_not_recognized');
152
- const asset = await AssetValue.fromString(route.calldata.fromAsset);
153
- if (!asset) throw new SwapKitError('core_swap_asset_not_recognized');
149
+ if (SWAP_OUT.includes(quoteMode)) {
150
+ if (!route.calldata.fromAsset) throw new SwapKitError('core_swap_asset_not_recognized');
151
+ const asset = await AssetValue.fromString(route.calldata.fromAsset);
152
+ if (!asset) throw new SwapKitError('core_swap_asset_not_recognized');
154
153
 
155
- const { address: recipient } = await this.#getInboundDataByChain(asset.chain);
156
- const {
157
- contract: router,
158
- calldata: { expiration, amountIn, memo, memoStreamingSwap },
159
- } = route;
154
+ const { address: recipient } = await this.#getInboundDataByChain(asset.chain);
155
+ const {
156
+ contract: router,
157
+ calldata: { expiration, amountIn, memo, memoStreamingSwap },
158
+ } = route;
160
159
 
161
- const assetValue = asset.add(SwapKitNumber.fromBigInt(BigInt(amountIn), asset.decimal));
162
- const swapMemo = (streamSwap ? memoStreamingSwap || memo : memo) as string;
160
+ const assetValue = asset.add(SwapKitNumber.fromBigInt(BigInt(amountIn), asset.decimal));
161
+ const swapMemo = (streamSwap ? memoStreamingSwap || memo : memo) as string;
163
162
 
164
- return this.deposit({
165
- expiration,
166
- assetValue,
167
- memo: swapMemo,
168
- feeOptionKey,
169
- router,
170
- recipient,
171
- });
172
- }
163
+ return this.deposit({
164
+ expiration,
165
+ assetValue,
166
+ memo: swapMemo,
167
+ feeOptionKey,
168
+ router,
169
+ recipient,
170
+ });
171
+ }
173
172
 
174
- if (SWAP_IN.includes(quoteMode) && evmChain) {
175
- const { calldata, contract: contractAddress } = route;
176
- if (!contractAddress) throw new SwapKitError('core_swap_contract_not_found');
173
+ if (SWAP_IN.includes(quoteMode) && evmChain) {
174
+ const { calldata, contract: contractAddress } = route;
175
+ if (!contractAddress) throw new SwapKitError('core_swap_contract_not_found');
177
176
 
178
- const walletMethods = this.connectedWallets[evmChain];
179
- const from = this.getAddress(evmChain);
180
- if (!walletMethods?.sendTransaction || !from) {
181
- throw new SwapKitError('core_wallet_connection_not_found');
182
- }
177
+ const walletMethods = this.connectedWallets[evmChain];
178
+ const from = this.getAddress(evmChain);
183
179
 
184
- const { getProvider, toChecksumAddress } = await import('@swapkit/toolbox-evm');
185
- const provider = getProvider(evmChain);
186
- const abi = lowercasedContractAbiMapping[contractAddress.toLowerCase()];
180
+ if (!walletMethods?.sendTransaction || !from) {
181
+ throw new SwapKitError('core_wallet_connection_not_found');
182
+ }
187
183
 
188
- if (!abi) throw new SwapKitError('core_swap_contract_not_supported', { contractAddress });
184
+ const { getProvider, toChecksumAddress } = await import('@swapkit/toolbox-evm');
185
+ const provider = getProvider(evmChain);
186
+ const abi = lowercasedContractAbiMapping[contractAddress.toLowerCase()];
189
187
 
190
- const contract = await walletMethods.createContract?.(contractAddress, abi, provider);
188
+ if (!abi) throw new SwapKitError('core_swap_contract_not_supported', { contractAddress });
191
189
 
192
- const tx = await contract.getFunction('swapIn').populateTransaction(
193
- ...getSwapInParams({
194
- streamSwap,
195
- toChecksumAddress,
196
- contractAddress: contractAddress as AGG_CONTRACT_ADDRESS,
197
- recipient,
198
- calldata,
199
- }),
200
- { from },
201
- );
190
+ const contract = await walletMethods.createContract?.(contractAddress, abi, provider);
202
191
 
203
- return walletMethods.sendTransaction(tx, feeOptionKey) as Promise<string>;
204
- }
192
+ const tx = await contract.getFunction('swapIn').populateTransaction(
193
+ ...getSwapInParams({
194
+ streamSwap,
195
+ toChecksumAddress,
196
+ contractAddress: contractAddress as AGG_CONTRACT_ADDRESS,
197
+ recipient,
198
+ calldata,
199
+ }),
200
+ { from },
201
+ );
205
202
 
206
- throw new SwapKitError('core_swap_quote_mode_not_supported', { quoteMode });
207
- } catch (error) {
208
- throw new SwapKitError('core_swap_transaction_error', error);
203
+ return walletMethods.sendTransaction(tx, feeOptionKey) as Promise<string>;
209
204
  }
205
+
206
+ throw new SwapKitError('core_swap_quote_mode_not_supported', { quoteMode });
210
207
  };
211
208
 
212
209
  getWalletByChain = async (chain: Chain, potentialScamFilter?: boolean) => {
@@ -260,15 +257,23 @@ export class SwapKitCore<T = ''> {
260
257
  }: CoreTxParams & { router?: string }) => {
261
258
  const { chain, symbol, ticker } = assetValue;
262
259
  const walletInstance = this.connectedWallets[chain];
263
- if (!(await validateAddressType({ address: await walletInstance?.getAddress(), chain })))
260
+ const isAddressValidated = await validateAddressType({
261
+ address: await walletInstance?.getAddress(),
262
+ chain,
263
+ });
264
+
265
+ if (!isAddressValidated) {
264
266
  throw new SwapKitError('core_transaction_invalid_sender_address');
267
+ }
268
+
265
269
  if (!walletInstance) throw new SwapKitError('core_wallet_connection_not_found');
266
270
 
267
271
  const params = this.#prepareTxParams({ assetValue, recipient, router, ...rest });
268
272
 
269
273
  try {
270
274
  switch (chain) {
271
- case Chain.THORChain: {
275
+ case Chain.THORChain:
276
+ case Chain.Maya: {
272
277
  const wallet = walletInstance as ThorchainWallet;
273
278
  return await (recipient === '' ? wallet.deposit(params) : wallet.transfer(params));
274
279
  }
@@ -317,13 +322,16 @@ export class SwapKitCore<T = ''> {
317
322
  const isInsufficientFunds = errorMessage?.includes('insufficient funds');
318
323
  const isGas = errorMessage?.includes('gas');
319
324
  const isServer = errorMessage?.includes('server');
320
- const errorKey: Keys = isInsufficientFunds
325
+ const isUserRejected = errorMessage?.includes('user rejected');
326
+ const errorKey: ErrorKeys = isInsufficientFunds
321
327
  ? 'core_transaction_deposit_insufficient_funds_error'
322
328
  : isGas
323
329
  ? 'core_transaction_deposit_gas_error'
324
330
  : isServer
325
331
  ? 'core_transaction_deposit_server_error'
326
- : 'core_transaction_deposit_error';
332
+ : isUserRejected
333
+ ? 'core_transaction_user_rejected'
334
+ : 'core_transaction_deposit_error';
327
335
 
328
336
  throw new SwapKitError(errorKey, error);
329
337
  }
@@ -659,22 +667,21 @@ export class SwapKitCore<T = ''> {
659
667
  };
660
668
 
661
669
  #getInboundDataByChain = async (chain: Chain) => {
662
- if (chain === Chain.THORChain) {
663
- return {
664
- gas_rate: '0',
665
- router: '0',
666
- address: '',
667
- halted: false,
668
- chain: Chain.THORChain,
669
- };
670
- }
671
- const inboundData = await getInboundData(this.stagenet);
672
- const chainAddressData = inboundData.find((item) => item.chain === chain);
670
+ switch (chain) {
671
+ case Chain.Maya:
672
+ case Chain.THORChain:
673
+ return { gas_rate: '0', router: '', address: '', halted: false, chain };
674
+
675
+ default: {
676
+ const inboundData = await getInboundData(this.stagenet);
677
+ const chainAddressData = inboundData.find((item) => item.chain === chain);
673
678
 
674
- if (!chainAddressData) throw new SwapKitError('core_inbound_data_not_found');
675
- if (chainAddressData?.halted) throw new SwapKitError('core_chain_halted');
679
+ if (!chainAddressData) throw new SwapKitError('core_inbound_data_not_found');
680
+ if (chainAddressData?.halted) throw new SwapKitError('core_chain_halted');
676
681
 
677
- return chainAddressData;
682
+ return chainAddressData;
683
+ }
684
+ }
678
685
  };
679
686
 
680
687
  #addConnectedChain = ({ chain, wallet, walletMethods }: AddChainWalletParams) => {