@subwallet/extension-base 1.3.62-0 → 1.3.63-0
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/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/transfer/smart-contract.js +14 -10
- package/cjs/services/balance-service/transfer/xcm/acrossBridge/index.js +0 -6
- package/cjs/services/balance-service/transfer/xcm/utils.js +2 -0
- package/cjs/services/chain-service/constants.js +16 -0
- package/cjs/services/chain-service/utils/index.js +24 -4
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/earning-service/constants/chains.js +2 -1
- package/cjs/services/earning-service/handlers/native-staking/base-para.js +6 -3
- package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +4 -1
- package/cjs/services/earning-service/handlers/native-staking/tanssi.js +496 -0
- package/cjs/services/earning-service/service.js +4 -0
- package/cjs/services/earning-service/utils/index.js +2 -0
- package/cjs/services/swap-service/handler/base-handler.js +4 -1
- package/cjs/services/swap-service/handler/chainflip-handler.js +1 -17
- package/cjs/services/swap-service/handler/optimex-handler.js +421 -0
- package/cjs/services/swap-service/handler/simpleswap-handler.js +4 -2
- package/cjs/services/swap-service/index.js +38 -140
- package/cjs/services/swap-service/utils.js +16 -157
- package/cjs/services/transaction-service/helpers/index.js +2 -1
- package/cjs/types/service-base.js +0 -1
- package/cjs/types/swap/index.js +5 -8
- package/cjs/utils/account/common.js +1 -2
- package/package.json +16 -6
- package/packageInfo.js +1 -1
- package/services/balance-service/transfer/smart-contract.d.ts +3 -2
- package/services/balance-service/transfer/smart-contract.js +35 -29
- package/services/balance-service/transfer/xcm/acrossBridge/index.d.ts +0 -4
- package/services/balance-service/transfer/xcm/acrossBridge/index.js +0 -4
- package/services/balance-service/transfer/xcm/utils.js +2 -0
- package/services/chain-service/constants.js +16 -0
- package/services/chain-service/utils/index.d.ts +3 -2
- package/services/chain-service/utils/index.js +20 -1
- package/services/chain-service/utils/patch.d.ts +1 -1
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/constants/chains.d.ts +1 -0
- package/services/earning-service/constants/chains.js +2 -1
- package/services/earning-service/handlers/native-staking/base-para.js +6 -3
- package/services/earning-service/handlers/native-staking/relay-chain.js +4 -1
- package/services/earning-service/handlers/native-staking/tanssi.d.ts +16 -0
- package/services/earning-service/handlers/native-staking/tanssi.js +478 -0
- package/services/earning-service/service.js +4 -0
- package/services/earning-service/utils/index.js +2 -0
- package/services/swap-service/handler/base-handler.js +6 -3
- package/services/swap-service/handler/chainflip-handler.d.ts +0 -2
- package/services/swap-service/handler/chainflip-handler.js +2 -18
- package/services/swap-service/handler/optimex-handler.d.ts +43 -0
- package/services/swap-service/handler/optimex-handler.js +410 -0
- package/services/swap-service/handler/simpleswap-handler.js +5 -3
- package/services/swap-service/index.d.ts +0 -1
- package/services/swap-service/index.js +21 -123
- package/services/swap-service/utils.d.ts +6 -12
- package/services/swap-service/utils.js +8 -138
- package/services/transaction-service/helpers/index.js +2 -1
- package/types/service-base.d.ts +3 -4
- package/types/service-base.js +0 -2
- package/types/swap/index.d.ts +3 -1
- package/types/swap/index.js +7 -6
- package/types/yield/info/account/info.d.ts +5 -0
- package/utils/account/common.js +2 -3
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.OptimexHandler = void 0;
|
|
8
|
+
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
9
|
+
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
10
|
+
var _web = require("@subwallet/extension-base/koni/api/contract-handler/evm/web3");
|
|
11
|
+
var _bitcoinTransfer = require("@subwallet/extension-base/services/balance-service/transfer/bitcoin-transfer");
|
|
12
|
+
var _smartContract = require("@subwallet/extension-base/services/balance-service/transfer/smart-contract");
|
|
13
|
+
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
14
|
+
var _baseHandler = require("@subwallet/extension-base/services/swap-service/handler/base-handler");
|
|
15
|
+
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
16
|
+
var _types = require("@subwallet/extension-base/types");
|
|
17
|
+
var _utils3 = require("@subwallet/extension-base/utils");
|
|
18
|
+
var _getId = require("@subwallet/extension-base/utils/getId");
|
|
19
|
+
var _uiKeyring = _interopRequireDefault(require("@subwallet/ui-keyring"));
|
|
20
|
+
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
21
|
+
var bitcoin = _interopRequireWildcard(require("bitcoinjs-lib"));
|
|
22
|
+
var _util = require("@polkadot/util");
|
|
23
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
24
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
25
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
26
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
27
|
+
|
|
28
|
+
const OptimexBaseUrl = {
|
|
29
|
+
mainnet: 'https://subwallet-provider.optimex.xyz',
|
|
30
|
+
testnet: 'https://provider-stg.bitdex.xyz'
|
|
31
|
+
};
|
|
32
|
+
class OptimexHandler {
|
|
33
|
+
constructor(chainService, balanceService, feeService) {
|
|
34
|
+
let isTestnet = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
|
35
|
+
this.swapBaseHandler = new _baseHandler.SwapBaseHandler({
|
|
36
|
+
chainService,
|
|
37
|
+
balanceService,
|
|
38
|
+
feeService,
|
|
39
|
+
providerName: isTestnet ? 'Optimex Testnet' : 'Optimex',
|
|
40
|
+
providerSlug: isTestnet ? _types.SwapProviderId.OPTIMEX_TESTNET : _types.SwapProviderId.OPTIMEX
|
|
41
|
+
});
|
|
42
|
+
this.providerSlug = isTestnet ? _types.SwapProviderId.OPTIMEX_TESTNET : _types.SwapProviderId.OPTIMEX;
|
|
43
|
+
this.baseUrl = isTestnet ? OptimexBaseUrl.testnet : OptimexBaseUrl.mainnet;
|
|
44
|
+
this.isTestnet = isTestnet;
|
|
45
|
+
}
|
|
46
|
+
get chainService() {
|
|
47
|
+
return this.swapBaseHandler.chainService;
|
|
48
|
+
}
|
|
49
|
+
get providerInfo() {
|
|
50
|
+
return this.swapBaseHandler.providerInfo;
|
|
51
|
+
}
|
|
52
|
+
get name() {
|
|
53
|
+
return this.swapBaseHandler.name;
|
|
54
|
+
}
|
|
55
|
+
get slug() {
|
|
56
|
+
return this.swapBaseHandler.slug;
|
|
57
|
+
}
|
|
58
|
+
async generateOptimalProcessV2(params) {
|
|
59
|
+
const tradeMetadata = await this.initTrade(params);
|
|
60
|
+
if (!tradeMetadata) {
|
|
61
|
+
throw new Error('Error generating optimal process: Cannot init Optimex trade');
|
|
62
|
+
}
|
|
63
|
+
const isNeedApprove = tradeMetadata.need_approve;
|
|
64
|
+
this.currentTradeMetadata = tradeMetadata;
|
|
65
|
+
if (!isNeedApprove) {
|
|
66
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, [this.getSubmitStep.bind(this)]);
|
|
67
|
+
}
|
|
68
|
+
if (!tradeMetadata.approve_address || !tradeMetadata.approve_payload) {
|
|
69
|
+
throw new Error('Error generating optimal process: Lack of approve info');
|
|
70
|
+
}
|
|
71
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, [this.getApprovalStep.bind(this), this.getSubmitStep.bind(this)]);
|
|
72
|
+
}
|
|
73
|
+
async initTrade(request) {
|
|
74
|
+
var _request$selectedQuot, _request$selectedQuot2;
|
|
75
|
+
const pair = request.request.pair;
|
|
76
|
+
const fromAsset = this.chainService.getAssetBySlug(pair.from);
|
|
77
|
+
const fromChain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
78
|
+
const fromChainType = (0, _utils._chainInfoToChainType)(fromChain);
|
|
79
|
+
const sender = request.request.address;
|
|
80
|
+
const receiver = request.request.recipient;
|
|
81
|
+
const sendingValue = request.request.fromAmount;
|
|
82
|
+
const slippage = request.request.slippage;
|
|
83
|
+
const metadata = (_request$selectedQuot = request.selectedQuote) === null || _request$selectedQuot === void 0 ? void 0 : _request$selectedQuot.metadata;
|
|
84
|
+
const walletFeeInfo = (_request$selectedQuot2 = request.selectedQuote) === null || _request$selectedQuot2 === void 0 ? void 0 : _request$selectedQuot2.feeInfo.feeComponent.find(fee => fee.feeType === _types.SwapFeeType.WALLET_FEE);
|
|
85
|
+
if (!metadata || !walletFeeInfo) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
let initTradeRequest;
|
|
89
|
+
const swAffiliate = walletFeeInfo.metadata;
|
|
90
|
+
if (fromChainType === _KoniTypes.ChainType.EVM) {
|
|
91
|
+
initTradeRequest = {
|
|
92
|
+
session_id: metadata.session_id,
|
|
93
|
+
amount_in: sendingValue,
|
|
94
|
+
from_user_address: sender,
|
|
95
|
+
// compressPublicKey for BTC and SOLANA, address for EVM
|
|
96
|
+
to_user_address: receiver || '',
|
|
97
|
+
// Receiving address
|
|
98
|
+
user_refund_address: sender,
|
|
99
|
+
// Refund address if trade fails
|
|
100
|
+
user_refund_pubkey: sender,
|
|
101
|
+
// Refund pubkey if trade fails, in btc is pubkey and in evm is address
|
|
102
|
+
creator_public_key: sender,
|
|
103
|
+
// Compressed public key, in btc is pubkey and in evm is address
|
|
104
|
+
from_wallet_address: sender,
|
|
105
|
+
// Creator address
|
|
106
|
+
min_amount_out: (0, _utils2.getAmountAfterSlippage)(metadata.best_quote_after_fees, slippage),
|
|
107
|
+
affiliate_info: [swAffiliate]
|
|
108
|
+
};
|
|
109
|
+
} else if (fromChainType === _KoniTypes.ChainType.BITCOIN) {
|
|
110
|
+
const fromPublicKey = (0, _util.hexStripPrefix)((0, _util.u8aToHex)(_uiKeyring.default.getPair(sender).publicKey));
|
|
111
|
+
initTradeRequest = {
|
|
112
|
+
session_id: metadata.session_id,
|
|
113
|
+
amount_in: sendingValue,
|
|
114
|
+
from_user_address: fromPublicKey,
|
|
115
|
+
to_user_address: receiver || '',
|
|
116
|
+
user_refund_address: sender,
|
|
117
|
+
user_refund_pubkey: fromPublicKey,
|
|
118
|
+
creator_public_key: fromPublicKey,
|
|
119
|
+
from_wallet_address: sender,
|
|
120
|
+
min_amount_out: (0, _utils2.getAmountAfterSlippage)(metadata.best_quote_after_fees, slippage),
|
|
121
|
+
affiliate_info: [swAffiliate]
|
|
122
|
+
};
|
|
123
|
+
} else {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
let tradeInfo;
|
|
127
|
+
try {
|
|
128
|
+
const rawResponse = await fetch(`${this.baseUrl}/v1/trades/initiate`, {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
body: JSON.stringify(initTradeRequest),
|
|
131
|
+
headers: {
|
|
132
|
+
'Content-Type': 'application/json'
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
if (!rawResponse.ok) {
|
|
136
|
+
console.log('Error bad request while init quote');
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const response = await rawResponse.json();
|
|
140
|
+
tradeInfo = response.data;
|
|
141
|
+
} catch (e) {
|
|
142
|
+
console.log('Error while init quote');
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
return tradeInfo;
|
|
146
|
+
}
|
|
147
|
+
async getApprovalStep(params) {
|
|
148
|
+
// todo: handle this when support route has approve step
|
|
149
|
+
// const selectedQuote = params.selectedQuote;
|
|
150
|
+
//
|
|
151
|
+
// if (selectedQuote) {
|
|
152
|
+
// const metadata = selectedQuote.metadata as OptimexMetadata;
|
|
153
|
+
// }
|
|
154
|
+
|
|
155
|
+
return Promise.resolve(undefined);
|
|
156
|
+
}
|
|
157
|
+
async getSubmitStep(params, stepIndex) {
|
|
158
|
+
var _this$currentTradeMet;
|
|
159
|
+
if (!params.selectedQuote) {
|
|
160
|
+
return Promise.resolve(undefined);
|
|
161
|
+
}
|
|
162
|
+
const originTokenInfo = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
163
|
+
const originChain = this.chainService.getChainInfoByKey(originTokenInfo.originChain);
|
|
164
|
+
const destinationTokenInfo = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
165
|
+
const destinationChain = this.chainService.getChainInfoByKey(destinationTokenInfo.originChain);
|
|
166
|
+
const originChainType = (0, _utils._chainInfoToChainType)(originChain);
|
|
167
|
+
const originChainNativeTokenSlug = (0, _utils._getChainNativeTokenSlug)(originChain);
|
|
168
|
+
|
|
169
|
+
// Optimex do not return fee in quote. Need calculate network fee manually from client side
|
|
170
|
+
let networkFeeAmount;
|
|
171
|
+
const depositAddress = (_this$currentTradeMet = this.currentTradeMetadata) === null || _this$currentTradeMet === void 0 ? void 0 : _this$currentTradeMet.deposit_address;
|
|
172
|
+
if (!depositAddress) {
|
|
173
|
+
console.log('Optimex Trade metadata is undefined, request for new quote');
|
|
174
|
+
return Promise.resolve(undefined);
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
if (originChainType === _KoniTypes.ChainType.EVM) {
|
|
178
|
+
const evmApi = this.chainService.getEvmApi(originChain.slug);
|
|
179
|
+
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originChain.slug, 'evm');
|
|
180
|
+
let transactionConfig;
|
|
181
|
+
if ((0, _utils._isNativeToken)(originTokenInfo)) {
|
|
182
|
+
var _this$currentTradeMet2;
|
|
183
|
+
[transactionConfig] = await (0, _smartContract.getEVMTransactionObject)({
|
|
184
|
+
chain: originChain.slug,
|
|
185
|
+
evmApi,
|
|
186
|
+
from: params.request.address,
|
|
187
|
+
to: depositAddress,
|
|
188
|
+
value: params.request.fromAmount,
|
|
189
|
+
feeInfo,
|
|
190
|
+
transferAll: false,
|
|
191
|
+
fallbackFee: true,
|
|
192
|
+
data: (_this$currentTradeMet2 = this.currentTradeMetadata) === null || _this$currentTradeMet2 === void 0 ? void 0 : _this$currentTradeMet2.payload
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
[transactionConfig] = await (0, _smartContract.getERC20TransactionObject)({
|
|
196
|
+
assetAddress: (0, _utils._getContractAddressOfToken)(originTokenInfo),
|
|
197
|
+
chain: originChain.slug,
|
|
198
|
+
evmApi: this.chainService.getEvmApi(originChain.slug),
|
|
199
|
+
from: params.request.address,
|
|
200
|
+
to: depositAddress,
|
|
201
|
+
value: params.request.fromAmount,
|
|
202
|
+
feeInfo,
|
|
203
|
+
transferAll: false
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
networkFeeAmount = await (0, _web.estimateTxFee)(transactionConfig, evmApi, feeInfo);
|
|
207
|
+
} else if (originChainType === _KoniTypes.ChainType.BITCOIN) {
|
|
208
|
+
const bitcoinApi = this.chainService.getBitcoinApi(originChain.slug);
|
|
209
|
+
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originChain.slug, 'bitcoin');
|
|
210
|
+
const network = originChain.isTestnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
|
|
211
|
+
const [transaction] = await (0, _bitcoinTransfer.createBitcoinTransaction)({
|
|
212
|
+
bitcoinApi,
|
|
213
|
+
chain: originChain.slug,
|
|
214
|
+
from: params.request.address,
|
|
215
|
+
feeInfo,
|
|
216
|
+
to: depositAddress,
|
|
217
|
+
transferAll: false,
|
|
218
|
+
value: params.request.fromAmount,
|
|
219
|
+
network
|
|
220
|
+
});
|
|
221
|
+
const feeCombine = (0, _utils3.combineBitcoinFee)(feeInfo, undefined, undefined); // todo: recheck when implement custom fee
|
|
222
|
+
|
|
223
|
+
const recipients = [];
|
|
224
|
+
for (const txOutput of transaction.txOutputs) {
|
|
225
|
+
txOutput.address && recipients.push(txOutput.address);
|
|
226
|
+
}
|
|
227
|
+
const sizeInfo = (0, _utils3.getSizeInfo)({
|
|
228
|
+
inputLength: transaction.inputCount,
|
|
229
|
+
recipients: recipients,
|
|
230
|
+
sender: params.request.address
|
|
231
|
+
});
|
|
232
|
+
networkFeeAmount = Math.ceil(feeCombine.feeRate * sizeInfo.txVBytes).toString();
|
|
233
|
+
} else {
|
|
234
|
+
console.log('Unsupported swap from this chain type', originChainType);
|
|
235
|
+
return Promise.resolve(undefined);
|
|
236
|
+
}
|
|
237
|
+
} catch (e) {
|
|
238
|
+
throw new Error(e.message);
|
|
239
|
+
}
|
|
240
|
+
const networkFee = {
|
|
241
|
+
amount: networkFeeAmount || '0',
|
|
242
|
+
feeType: _types.SwapFeeType.NETWORK_FEE,
|
|
243
|
+
tokenSlug: originChainNativeTokenSlug
|
|
244
|
+
};
|
|
245
|
+
const submitStep = {
|
|
246
|
+
name: 'Swap',
|
|
247
|
+
type: _types.SwapStepType.SWAP,
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
metadata: {
|
|
250
|
+
sendingValue: params.request.fromAmount.toString(),
|
|
251
|
+
expectedReceive: params.selectedQuote.toAmount,
|
|
252
|
+
originTokenInfo,
|
|
253
|
+
destinationTokenInfo,
|
|
254
|
+
sender: (0, _utils3._reformatAddressWithChain)(params.request.address, originChain),
|
|
255
|
+
receiver: (0, _utils3._reformatAddressWithChain)(params.request.recipient || params.request.address, destinationChain),
|
|
256
|
+
version: 2
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
const feeInfo = {
|
|
260
|
+
defaultFeeToken: params.selectedQuote.feeInfo.defaultFeeToken,
|
|
261
|
+
feeComponent: [...params.selectedQuote.feeInfo.feeComponent, networkFee],
|
|
262
|
+
feeOptions: params.selectedQuote.feeInfo.feeOptions
|
|
263
|
+
};
|
|
264
|
+
return Promise.resolve([submitStep, feeInfo]);
|
|
265
|
+
}
|
|
266
|
+
async validateSwapProcessV2(params) {
|
|
267
|
+
const {
|
|
268
|
+
process,
|
|
269
|
+
selectedQuote
|
|
270
|
+
} = params;
|
|
271
|
+
if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
|
|
272
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
273
|
+
}
|
|
274
|
+
const actionList = JSON.stringify(process.path.map(step => step.action));
|
|
275
|
+
const swap = actionList === JSON.stringify([_types.DynamicSwapType.SWAP]);
|
|
276
|
+
const swapXcm = actionList === JSON.stringify([_types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
277
|
+
const xcmSwap = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP]);
|
|
278
|
+
const xcmSwapXcm = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
279
|
+
const swapIndex = params.process.steps.findIndex(step => step.type === _types.SwapStepType.SWAP);
|
|
280
|
+
if (swapIndex <= -1) {
|
|
281
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
282
|
+
}
|
|
283
|
+
if (swap) {
|
|
284
|
+
return this.swapBaseHandler.validateSwapOnlyProcess(params, swapIndex);
|
|
285
|
+
}
|
|
286
|
+
if (swapXcm) {
|
|
287
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
288
|
+
}
|
|
289
|
+
if (xcmSwap) {
|
|
290
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
291
|
+
}
|
|
292
|
+
if (xcmSwapXcm) {
|
|
293
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
294
|
+
}
|
|
295
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
296
|
+
}
|
|
297
|
+
async handleSwapProcess(params) {
|
|
298
|
+
const {
|
|
299
|
+
currentStep,
|
|
300
|
+
process
|
|
301
|
+
} = params;
|
|
302
|
+
const type = process.steps[currentStep].type;
|
|
303
|
+
switch (type) {
|
|
304
|
+
case _types.CommonStepType.DEFAULT:
|
|
305
|
+
return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
|
|
306
|
+
case _types.CommonStepType.TOKEN_APPROVAL:
|
|
307
|
+
return this.handleApproveStep(params);
|
|
308
|
+
case _types.SwapStepType.SWAP:
|
|
309
|
+
return this.handleSubmitStep(params);
|
|
310
|
+
default:
|
|
311
|
+
return this.handleSubmitStep(params);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
handleApproveStep(params) {
|
|
315
|
+
// todo: handle this when support route has approve step
|
|
316
|
+
const fromAsset = this.chainService.getAssetBySlug(params.quote.pair.from);
|
|
317
|
+
return {
|
|
318
|
+
txChain: fromAsset.originChain,
|
|
319
|
+
txData: '',
|
|
320
|
+
extrinsic: {},
|
|
321
|
+
extrinsicType: _KoniTypes.ExtrinsicType.TOKEN_SPENDING_APPROVAL,
|
|
322
|
+
transferNativeAmount: '0',
|
|
323
|
+
chainType: _KoniTypes.ChainType.EVM
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
async handleSubmitStep(params) {
|
|
327
|
+
var _this$currentTradeMet3, _this$currentTradeMet4, _this$currentTradeMet5;
|
|
328
|
+
const {
|
|
329
|
+
address,
|
|
330
|
+
process,
|
|
331
|
+
quote,
|
|
332
|
+
recipient,
|
|
333
|
+
slippage
|
|
334
|
+
} = params;
|
|
335
|
+
const pair = quote.pair;
|
|
336
|
+
const fromAsset = this.chainService.getAssetBySlug(pair.from);
|
|
337
|
+
const chainInfo = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
338
|
+
const chainType = (0, _utils._chainInfoToChainType)(chainInfo);
|
|
339
|
+
const depositAddress = (_this$currentTradeMet3 = this.currentTradeMetadata) === null || _this$currentTradeMet3 === void 0 ? void 0 : _this$currentTradeMet3.deposit_address;
|
|
340
|
+
const tradeId = (_this$currentTradeMet4 = this.currentTradeMetadata) === null || _this$currentTradeMet4 === void 0 ? void 0 : _this$currentTradeMet4.trade_id;
|
|
341
|
+
const payload = (_this$currentTradeMet5 = this.currentTradeMetadata) === null || _this$currentTradeMet5 === void 0 ? void 0 : _this$currentTradeMet5.payload; // undefined in case swap from btc
|
|
342
|
+
|
|
343
|
+
if (!depositAddress || !tradeId) {
|
|
344
|
+
throw new Error('Optimex Trade metadata is undefined, request for new quote');
|
|
345
|
+
}
|
|
346
|
+
const txData = {
|
|
347
|
+
address,
|
|
348
|
+
provider: this.providerInfo,
|
|
349
|
+
quote,
|
|
350
|
+
slippage,
|
|
351
|
+
recipient,
|
|
352
|
+
process
|
|
353
|
+
};
|
|
354
|
+
let extrinsic;
|
|
355
|
+
|
|
356
|
+
// dont remove this log
|
|
357
|
+
console.log('Optimex Trade metadata:', this.currentTradeMetadata);
|
|
358
|
+
console.log('Optimex Trade channel:', this.isTestnet ? `https://provider-api-docs.vercel.app/swap/${tradeId}` : `https://provider-api-docs.vercel.app/swap/${tradeId}?env=sub_wallet`);
|
|
359
|
+
if (chainType === _KoniTypes.ChainType.BITCOIN) {
|
|
360
|
+
const bitcoinApi = this.chainService.getBitcoinApi(chainInfo.slug);
|
|
361
|
+
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), chainInfo.slug, 'bitcoin');
|
|
362
|
+
const network = chainInfo.isTestnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
|
|
363
|
+
const [transaction] = await (0, _bitcoinTransfer.createBitcoinTransaction)({
|
|
364
|
+
bitcoinApi,
|
|
365
|
+
chain: chainInfo.slug,
|
|
366
|
+
from: address,
|
|
367
|
+
feeInfo,
|
|
368
|
+
to: depositAddress,
|
|
369
|
+
transferAll: false,
|
|
370
|
+
value: quote.fromAmount,
|
|
371
|
+
network
|
|
372
|
+
});
|
|
373
|
+
extrinsic = transaction;
|
|
374
|
+
} else if (chainType === _KoniTypes.ChainType.EVM) {
|
|
375
|
+
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), chainInfo.slug, 'evm');
|
|
376
|
+
if ((0, _utils._isNativeToken)(fromAsset)) {
|
|
377
|
+
const [transactionConfig] = await (0, _smartContract.getEVMTransactionObject)({
|
|
378
|
+
chain: chainInfo.slug,
|
|
379
|
+
evmApi: this.chainService.getEvmApi(chainInfo.slug),
|
|
380
|
+
from: address,
|
|
381
|
+
to: depositAddress,
|
|
382
|
+
value: quote.fromAmount,
|
|
383
|
+
feeInfo,
|
|
384
|
+
transferAll: false,
|
|
385
|
+
data: payload
|
|
386
|
+
});
|
|
387
|
+
extrinsic = {
|
|
388
|
+
...transactionConfig,
|
|
389
|
+
data: payload
|
|
390
|
+
};
|
|
391
|
+
} else {
|
|
392
|
+
const [transactionConfig] = await (0, _smartContract.getERC20TransactionObject)({
|
|
393
|
+
assetAddress: (0, _utils._getContractAddressOfToken)(fromAsset),
|
|
394
|
+
chain: chainInfo.slug,
|
|
395
|
+
evmApi: this.chainService.getEvmApi(chainInfo.slug),
|
|
396
|
+
from: address,
|
|
397
|
+
to: depositAddress,
|
|
398
|
+
value: quote.fromAmount,
|
|
399
|
+
feeInfo,
|
|
400
|
+
transferAll: false
|
|
401
|
+
});
|
|
402
|
+
extrinsic = transactionConfig;
|
|
403
|
+
}
|
|
404
|
+
} else {
|
|
405
|
+
throw new Error('Unknown swap chain type');
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// reset tradeMetadata after use // todo: review to check if need this clear
|
|
409
|
+
// this.currentTradeMetadata = undefined;
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
txChain: fromAsset.originChain,
|
|
413
|
+
txData,
|
|
414
|
+
extrinsic,
|
|
415
|
+
transferNativeAmount: (0, _utils._isNativeToken)(fromAsset) ? quote.fromAmount : '0',
|
|
416
|
+
extrinsicType: _KoniTypes.ExtrinsicType.SWAP,
|
|
417
|
+
chainType
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
exports.OptimexHandler = OptimexHandler;
|
|
@@ -167,7 +167,7 @@ class SimpleSwapHandler {
|
|
|
167
167
|
const toAsset = this.chainService.getAssetBySlug(pair.to);
|
|
168
168
|
const chainInfo = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
169
169
|
const toChainInfo = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
170
|
-
const chainType = (0, _utils.
|
|
170
|
+
const chainType = (0, _utils._chainInfoToChainType)(chainInfo);
|
|
171
171
|
const sender = (0, _utils2._reformatAddressWithChain)(address, chainInfo);
|
|
172
172
|
const receiver = (0, _utils2._reformatAddressWithChain)(recipient !== null && recipient !== void 0 ? recipient : sender, toChainInfo);
|
|
173
173
|
const fromSymbol = (0, _utils._getAssetSymbol)(fromAsset).toLowerCase();
|
|
@@ -230,7 +230,7 @@ class SimpleSwapHandler {
|
|
|
230
230
|
value: quote.fromAmount
|
|
231
231
|
});
|
|
232
232
|
extrinsic = submittableExtrinsic;
|
|
233
|
-
} else {
|
|
233
|
+
} else if (chainType === _KoniTypes.ChainType.EVM) {
|
|
234
234
|
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), chainInfo.slug, 'evm');
|
|
235
235
|
if ((0, _utils._isNativeToken)(fromAsset)) {
|
|
236
236
|
const [transactionConfig] = await (0, _smartContract.getEVMTransactionObject)({
|
|
@@ -256,6 +256,8 @@ class SimpleSwapHandler {
|
|
|
256
256
|
});
|
|
257
257
|
extrinsic = transactionConfig;
|
|
258
258
|
}
|
|
259
|
+
} else {
|
|
260
|
+
throw new Error('Unknown swap chain type');
|
|
259
261
|
}
|
|
260
262
|
return {
|
|
261
263
|
txChain: fromAsset.originChain,
|