@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/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.es.js +320 -316
- package/dist/index.es.js.map +1 -1
- package/package.json +9 -9
- package/src/client/index.ts +115 -108
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.
|
|
5
|
-
"@swapkit/types": "1.0.0-rc.
|
|
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.
|
|
15
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
16
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
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.
|
|
21
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
22
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
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.
|
|
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",
|
package/src/client/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
135
|
-
|
|
133
|
+
const transaction = streamSwap ? route?.streamingSwap?.transaction : route?.transaction;
|
|
134
|
+
if (!transaction) throw new SwapKitError('core_swap_route_transaction_not_found');
|
|
136
135
|
|
|
137
|
-
|
|
136
|
+
const { data, from, to, value } = route.transaction;
|
|
138
137
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
148
|
-
|
|
146
|
+
return walletMethods.sendTransaction(params, feeOptionKey) as Promise<string>;
|
|
147
|
+
}
|
|
149
148
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
162
|
-
|
|
160
|
+
const assetValue = asset.add(SwapKitNumber.fromBigInt(BigInt(amountIn), asset.decimal));
|
|
161
|
+
const swapMemo = (streamSwap ? memoStreamingSwap || memo : memo) as string;
|
|
163
162
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
163
|
+
return this.deposit({
|
|
164
|
+
expiration,
|
|
165
|
+
assetValue,
|
|
166
|
+
memo: swapMemo,
|
|
167
|
+
feeOptionKey,
|
|
168
|
+
router,
|
|
169
|
+
recipient,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
173
172
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
179
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
180
|
+
if (!walletMethods?.sendTransaction || !from) {
|
|
181
|
+
throw new SwapKitError('core_wallet_connection_not_found');
|
|
182
|
+
}
|
|
187
183
|
|
|
188
|
-
|
|
184
|
+
const { getProvider, toChecksumAddress } = await import('@swapkit/toolbox-evm');
|
|
185
|
+
const provider = getProvider(evmChain);
|
|
186
|
+
const abi = lowercasedContractAbiMapping[contractAddress.toLowerCase()];
|
|
189
187
|
|
|
190
|
-
|
|
188
|
+
if (!abi) throw new SwapKitError('core_swap_contract_not_supported', { contractAddress });
|
|
191
189
|
|
|
192
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
:
|
|
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
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
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
|
-
|
|
675
|
-
|
|
679
|
+
if (!chainAddressData) throw new SwapKitError('core_inbound_data_not_found');
|
|
680
|
+
if (chainAddressData?.halted) throw new SwapKitError('core_chain_halted');
|
|
676
681
|
|
|
677
|
-
|
|
682
|
+
return chainAddressData;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
678
685
|
};
|
|
679
686
|
|
|
680
687
|
#addConnectedChain = ({ chain, wallet, walletMethods }: AddChainWalletParams) => {
|