@subwallet/extension-base 1.3.29-0 → 1.3.30-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/background/KoniTypes.d.ts +4 -4
- package/background/errors/SwapError.js +1 -1
- package/cjs/background/errors/SwapError.js +1 -1
- package/cjs/constants/blocked-actions.js +2 -2
- package/cjs/constants/remind-notification-time.js +3 -3
- package/cjs/core/logic-validation/swap.js +63 -4
- package/cjs/core/utils.js +9 -3
- package/cjs/koni/background/handlers/Extension.js +5 -82
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/transfer/xcm/availBridge.js +6 -6
- package/cjs/services/chain-service/constants.js +1 -1
- package/cjs/services/earning-service/handlers/base.js +6 -3
- package/cjs/services/earning-service/handlers/native-staking/base.js +4 -1
- package/cjs/services/earning-service/handlers/native-staking/dtao.js +68 -50
- package/cjs/services/earning-service/handlers/native-staking/tao.js +12 -2
- package/cjs/services/earning-service/service.js +2 -1
- package/cjs/services/fee-service/utils/index.js +16 -4
- package/cjs/services/inapp-notification-service/index.js +19 -13
- package/cjs/services/swap-service/handler/asset-hub/handler.js +61 -314
- package/cjs/services/swap-service/handler/base-handler.js +393 -231
- package/cjs/services/swap-service/handler/chainflip-handler.js +18 -40
- package/cjs/services/swap-service/handler/hydradx-handler.js +77 -269
- package/cjs/services/swap-service/handler/simpleswap-handler.js +27 -48
- package/cjs/services/swap-service/handler/uniswap-handler.js +33 -54
- package/cjs/services/swap-service/index.js +154 -143
- package/cjs/services/swap-service/utils.js +107 -17
- package/cjs/services/transaction-service/index.js +1 -1
- package/cjs/types/swap/index.js +13 -1
- package/cjs/utils/swap.js +5 -1
- package/constants/blocked-actions.d.ts +1 -1
- package/constants/blocked-actions.js +1 -1
- package/constants/remind-notification-time.d.ts +1 -1
- package/constants/remind-notification-time.js +1 -1
- package/core/logic-validation/swap.d.ts +15 -0
- package/core/logic-validation/swap.js +60 -4
- package/core/utils.js +9 -3
- package/koni/background/handlers/Extension.d.ts +0 -1
- package/koni/background/handlers/Extension.js +6 -83
- package/package.json +6 -12
- package/packageInfo.js +1 -1
- package/services/balance-service/transfer/xcm/availBridge.js +6 -6
- package/services/base/types.d.ts +0 -4
- package/services/chain-service/constants.js +1 -1
- package/services/earning-service/handlers/base.d.ts +4 -3
- package/services/earning-service/handlers/base.js +6 -4
- package/services/earning-service/handlers/native-staking/base.js +4 -1
- package/services/earning-service/handlers/native-staking/dtao.d.ts +9 -6
- package/services/earning-service/handlers/native-staking/dtao.js +69 -48
- package/services/earning-service/handlers/native-staking/tao.js +12 -2
- package/services/earning-service/service.d.ts +2 -1
- package/services/earning-service/service.js +2 -1
- package/services/fee-service/utils/index.d.ts +1 -0
- package/services/fee-service/utils/index.js +14 -4
- package/services/inapp-notification-service/index.js +13 -7
- package/services/swap-service/handler/asset-hub/handler.d.ts +2 -9
- package/services/swap-service/handler/asset-hub/handler.js +64 -317
- package/services/swap-service/handler/base-handler.d.ts +6 -9
- package/services/swap-service/handler/base-handler.js +391 -229
- package/services/swap-service/handler/chainflip-handler.d.ts +2 -4
- package/services/swap-service/handler/chainflip-handler.js +15 -37
- package/services/swap-service/handler/hydradx-handler.d.ts +3 -10
- package/services/swap-service/handler/hydradx-handler.js +78 -270
- package/services/swap-service/handler/simpleswap-handler.d.ts +2 -4
- package/services/swap-service/handler/simpleswap-handler.js +24 -45
- package/services/swap-service/handler/uniswap-handler.d.ts +4 -6
- package/services/swap-service/handler/uniswap-handler.js +25 -46
- package/services/swap-service/index.d.ts +8 -14
- package/services/swap-service/index.js +141 -129
- package/services/swap-service/utils.d.ts +11 -3
- package/services/swap-service/utils.js +96 -15
- package/services/transaction-service/index.js +2 -2
- package/types/service-base.d.ts +2 -3
- package/types/swap/index.d.ts +25 -9
- package/types/swap/index.js +10 -0
- package/types/transaction/process.d.ts +19 -0
- package/types/yield/actions/join/submit.d.ts +4 -1
- package/types/yield/actions/others.d.ts +2 -0
- package/utils/swap.d.ts +3 -0
- package/utils/swap.js +3 -0
- package/cjs/services/swap-service/interface.js +0 -14
- package/services/swap-service/interface.d.ts +0 -9
- package/services/swap-service/interface.js +0 -8
|
@@ -8,14 +8,10 @@ exports.AssetHubSwapHandler = void 0;
|
|
|
8
8
|
var _SwapError = require("@subwallet/extension-base/background/errors/SwapError");
|
|
9
9
|
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
10
10
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
11
|
-
var _constants = require("@subwallet/extension-base/constants");
|
|
12
|
-
var _swap = require("@subwallet/extension-base/core/logic-validation/swap");
|
|
13
|
-
var _xcm = require("@subwallet/extension-base/services/balance-service/transfer/xcm");
|
|
14
11
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
15
|
-
var _interface = require("@subwallet/extension-base/services/swap-service/interface");
|
|
16
12
|
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
17
13
|
var _types = require("@subwallet/extension-base/types");
|
|
18
|
-
var
|
|
14
|
+
var _utils3 = require("@subwallet/extension-base/utils");
|
|
19
15
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
20
16
|
var _baseHandler = require("../base-handler");
|
|
21
17
|
var _router = require("./router");
|
|
@@ -68,235 +64,70 @@ class AssetHubSwapHandler {
|
|
|
68
64
|
if (!chainState.active) {
|
|
69
65
|
await this.chainService.enableChain(this.chain);
|
|
70
66
|
}
|
|
71
|
-
const substrateApi = this.chainService.getSubstrateApi(this.chain);
|
|
72
|
-
await substrateApi.api.isReady;
|
|
73
67
|
this.router = new _router.AssetHubRouter(this.chain, this.chainService);
|
|
74
68
|
this.isReady = true;
|
|
75
69
|
}
|
|
76
|
-
async
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (!alternativeAssetSlug) {
|
|
87
|
-
return undefined;
|
|
88
|
-
}
|
|
89
|
-
const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
90
|
-
const alternativeAssetBalance = await this.balanceService.getTransferableBalance(params.request.address, alternativeAsset.originChain, alternativeAsset.slug);
|
|
91
|
-
const bnAlternativeAssetBalance = new _bignumber.default(alternativeAssetBalance.value);
|
|
92
|
-
if (bnAlternativeAssetBalance.lte(0)) {
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
try {
|
|
96
|
-
const alternativeChainInfo = this.chainService.getChainInfoByKey(alternativeAsset.originChain);
|
|
97
|
-
const originalChainInfo = this.chainService.getChainInfoByKey(this.chain);
|
|
98
|
-
const xcmOriginSubstrateApi = await this.chainService.getSubstrateApi(alternativeAsset.originChain).isReady;
|
|
99
|
-
const id = (0, _getId.getId)();
|
|
100
|
-
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(id, alternativeChainInfo.slug, 'substrate');
|
|
101
|
-
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
102
|
-
originTokenInfo: alternativeAsset,
|
|
103
|
-
destinationTokenInfo: fromAsset,
|
|
104
|
-
// Mock sending value to get payment info
|
|
105
|
-
sendingValue: bnAmount.toString(),
|
|
106
|
-
recipient: params.request.address,
|
|
107
|
-
sender: params.request.address,
|
|
108
|
-
feeInfo: feeInfo,
|
|
109
|
-
substrateApi: xcmOriginSubstrateApi,
|
|
110
|
-
destinationChain: originalChainInfo,
|
|
111
|
-
originChain: alternativeChainInfo
|
|
112
|
-
});
|
|
113
|
-
const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
|
|
114
|
-
const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
|
|
115
|
-
const fee = {
|
|
116
|
-
feeComponent: [{
|
|
117
|
-
feeType: _types.SwapFeeType.NETWORK_FEE,
|
|
118
|
-
amount: Math.round(xcmFeeInfo.partialFee * _constants.XCM_MIN_AMOUNT_RATIO).toString(),
|
|
119
|
-
tokenSlug: (0, _utils._getChainNativeTokenSlug)(alternativeChainInfo)
|
|
120
|
-
}],
|
|
121
|
-
defaultFeeToken: (0, _utils._getChainNativeTokenSlug)(alternativeChainInfo),
|
|
122
|
-
feeOptions: [(0, _utils._getChainNativeTokenSlug)(alternativeChainInfo)]
|
|
123
|
-
};
|
|
124
|
-
let bnTransferAmount = bnAmount.minus(bnFromAssetBalance);
|
|
125
|
-
if ((0, _utils._isNativeToken)(alternativeAsset)) {
|
|
126
|
-
const bnXcmFee = new _bignumber.default(fee.feeComponent[0].amount); // xcm fee is paid in native token but swap token is not always native token
|
|
127
|
-
|
|
128
|
-
bnTransferAmount = bnTransferAmount.plus(bnXcmFee);
|
|
129
|
-
}
|
|
130
|
-
const step = {
|
|
131
|
-
metadata: {
|
|
132
|
-
sendingValue: bnTransferAmount.toString(),
|
|
133
|
-
originTokenInfo: alternativeAsset,
|
|
134
|
-
destinationTokenInfo: fromAsset
|
|
135
|
-
},
|
|
136
|
-
name: `Transfer ${alternativeAsset.symbol} from ${alternativeChainInfo.name}`,
|
|
137
|
-
type: _types.CommonStepType.XCM
|
|
138
|
-
};
|
|
139
|
-
return [step, fee];
|
|
140
|
-
} catch (e) {
|
|
141
|
-
console.error('Error creating xcm step', e);
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
async getSubmitStep(params) {
|
|
146
|
-
if (params.selectedQuote) {
|
|
147
|
-
const submitStep = {
|
|
148
|
-
name: 'Swap',
|
|
149
|
-
type: _types.SwapStepType.SWAP
|
|
150
|
-
};
|
|
151
|
-
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
152
|
-
}
|
|
153
|
-
return Promise.resolve(undefined);
|
|
154
|
-
}
|
|
155
|
-
async getXcmStepV2(params) {
|
|
156
|
-
var _params$path$find;
|
|
157
|
-
// todo: improve this function for Round 2
|
|
158
|
-
|
|
159
|
-
const xcmPairInfo = (_params$path$find = params.path.find((step, i) => i === 0 && step.action === _interface.DynamicSwapType.BRIDGE)) === null || _params$path$find === void 0 ? void 0 : _params$path$find.pair;
|
|
160
|
-
if (!xcmPairInfo) {
|
|
161
|
-
return undefined;
|
|
162
|
-
}
|
|
163
|
-
const fromTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.from);
|
|
164
|
-
const toTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.to);
|
|
165
|
-
const fromChainInfo = this.chainService.getChainInfoByKey(fromTokenInfo.originChain);
|
|
166
|
-
const toChainInfo = this.chainService.getChainInfoByKey(toTokenInfo.originChain);
|
|
167
|
-
const substrateApi = await this.chainService.getSubstrateApi(fromTokenInfo.originChain).isReady;
|
|
168
|
-
if (!fromChainInfo || !toChainInfo || !fromChainInfo || !toChainInfo) {
|
|
169
|
-
throw Error('Token and chain not found');
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
const id = (0, _getId.getId)();
|
|
173
|
-
const [feeInfo, toTokenBalance] = await Promise.all([this.swapBaseHandler.feeService.subscribeChainFee(id, fromTokenInfo.originChain, 'substrate'), this.balanceService.getTotalBalance(params.request.address, toTokenInfo.originChain, toTokenInfo.slug, _KoniTypes.ExtrinsicType.TRANSFER_BALANCE)]);
|
|
174
|
-
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
175
|
-
originTokenInfo: fromTokenInfo,
|
|
176
|
-
destinationTokenInfo: toTokenInfo,
|
|
177
|
-
// Mock sending value to get payment info
|
|
178
|
-
sendingValue: params.request.fromAmount,
|
|
179
|
-
recipient: params.request.address,
|
|
180
|
-
substrateApi: substrateApi,
|
|
181
|
-
sender: params.request.address,
|
|
182
|
-
originChain: fromChainInfo,
|
|
183
|
-
destinationChain: toChainInfo,
|
|
184
|
-
feeInfo
|
|
185
|
-
});
|
|
186
|
-
const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
|
|
187
|
-
const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
|
|
188
|
-
const fee = {
|
|
189
|
-
feeComponent: [{
|
|
190
|
-
feeType: _types.SwapFeeType.NETWORK_FEE,
|
|
191
|
-
amount: Math.ceil(xcmFeeInfo.partialFee * _utils2.FEE_RATE_MULTIPLIER.high).toString(),
|
|
192
|
-
tokenSlug: (0, _utils._getChainNativeTokenSlug)(fromChainInfo)
|
|
193
|
-
}],
|
|
194
|
-
defaultFeeToken: (0, _utils._getChainNativeTokenSlug)(fromChainInfo),
|
|
195
|
-
feeOptions: [(0, _utils._getChainNativeTokenSlug)(fromChainInfo)]
|
|
196
|
-
};
|
|
197
|
-
let bnTransferAmount = new _bignumber.default(params.request.fromAmount);
|
|
198
|
-
if ((0, _utils._isNativeToken)(fromTokenInfo)) {
|
|
199
|
-
// xcm fee is paid in native token but swap token is not always native token
|
|
200
|
-
// add amount of fee into sending value to ensure has enough token to swap
|
|
201
|
-
const bnXcmFee = new _bignumber.default(fee.feeComponent[0].amount);
|
|
202
|
-
bnTransferAmount = bnTransferAmount.plus(bnXcmFee);
|
|
203
|
-
} else {
|
|
204
|
-
bnTransferAmount = bnTransferAmount.plus((0, _bignumber.default)((0, _utils._getTokenMinAmount)(toTokenInfo)).multipliedBy(_utils2.FEE_RATE_MULTIPLIER.medium));
|
|
205
|
-
}
|
|
206
|
-
if ((0, _bignumber.default)(toTokenBalance.value).lte(0)) {
|
|
207
|
-
bnTransferAmount = bnTransferAmount.plus((0, _utils._getTokenMinAmount)(toTokenInfo));
|
|
208
|
-
}
|
|
209
|
-
const step = {
|
|
210
|
-
metadata: {
|
|
211
|
-
sendingValue: bnTransferAmount.toString(),
|
|
212
|
-
originTokenInfo: fromTokenInfo,
|
|
213
|
-
destinationTokenInfo: toTokenInfo
|
|
214
|
-
},
|
|
215
|
-
name: `Transfer ${fromTokenInfo.symbol} from ${fromChainInfo.name}`,
|
|
216
|
-
type: _types.CommonStepType.XCM
|
|
217
|
-
};
|
|
218
|
-
return [step, fee];
|
|
219
|
-
} catch (e) {
|
|
220
|
-
console.error('Error creating xcm step', e);
|
|
221
|
-
return undefined;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
async getSwapStepV2(params) {
|
|
225
|
-
var _params$path$find2;
|
|
226
|
-
const swapPairInfo = (_params$path$find2 = params.path.find(step => step.action === _interface.DynamicSwapType.SWAP)) === null || _params$path$find2 === void 0 ? void 0 : _params$path$find2.pair;
|
|
227
|
-
if (!swapPairInfo) {
|
|
70
|
+
async getSubmitStep(params, stepIndex) {
|
|
71
|
+
const {
|
|
72
|
+
path,
|
|
73
|
+
request: {
|
|
74
|
+
fromAmount
|
|
75
|
+
},
|
|
76
|
+
selectedQuote
|
|
77
|
+
} = params;
|
|
78
|
+
const stepData = path[stepIndex];
|
|
79
|
+
if (stepData.action !== _types.DynamicSwapType.SWAP) {
|
|
228
80
|
return Promise.resolve(undefined);
|
|
229
81
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
type: _types.SwapStepType.SWAP,
|
|
234
|
-
metadata: {
|
|
235
|
-
sendingValue: params.request.fromAmount.toString(),
|
|
236
|
-
originTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.from),
|
|
237
|
-
destinationTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.to)
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
82
|
+
const swapPairInfo = stepData.pair;
|
|
83
|
+
if (!swapPairInfo || !selectedQuote) {
|
|
84
|
+
return Promise.resolve(undefined);
|
|
241
85
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
86
|
+
const originTokenInfo = this.chainService.getAssetBySlug(swapPairInfo.from);
|
|
87
|
+
const destinationTokenInfo = this.chainService.getAssetBySlug(swapPairInfo.to);
|
|
88
|
+
const originChain = this.chainService.getChainInfoByKey(originTokenInfo.originChain);
|
|
89
|
+
const destinationChain = this.chainService.getChainInfoByKey(destinationTokenInfo.originChain);
|
|
90
|
+
const actionList = JSON.stringify(path.map(step => step.action));
|
|
91
|
+
const xcmSwapXcm = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
92
|
+
const swapXcm = actionList === JSON.stringify([_types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
93
|
+
const needModifyData = swapXcm || xcmSwapXcm;
|
|
94
|
+
let bnSendingValue = (0, _bignumber.default)(fromAmount);
|
|
95
|
+
let bnExpectedReceive = (0, _bignumber.default)(selectedQuote.toAmount);
|
|
96
|
+
const sender = (0, _utils3._reformatAddressWithChain)(params.request.address, originChain);
|
|
97
|
+
let receiver = (0, _utils3._reformatAddressWithChain)(params.request.recipient || params.request.address, destinationChain);
|
|
98
|
+
if (needModifyData) {
|
|
99
|
+
bnSendingValue = bnSendingValue.multipliedBy(_utils2.DEFAULT_EXCESS_AMOUNT_WEIGHT);
|
|
100
|
+
bnExpectedReceive = bnExpectedReceive.multipliedBy(_utils2.DEFAULT_EXCESS_AMOUNT_WEIGHT);
|
|
101
|
+
receiver = (0, _utils3._reformatAddressWithChain)(params.request.address, destinationChain);
|
|
102
|
+
}
|
|
103
|
+
const submitStep = {
|
|
104
|
+
name: 'Swap',
|
|
105
|
+
type: _types.SwapStepType.SWAP,
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
metadata: {
|
|
108
|
+
sendingValue: bnSendingValue.toFixed(0, 1),
|
|
109
|
+
expectedReceive: bnExpectedReceive.toFixed(0, 1),
|
|
110
|
+
originTokenInfo,
|
|
111
|
+
destinationTokenInfo,
|
|
112
|
+
sender,
|
|
113
|
+
receiver,
|
|
114
|
+
version: 2
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
return Promise.resolve([submitStep, selectedQuote.feeInfo]);
|
|
246
118
|
}
|
|
247
119
|
generateOptimalProcessV2(params) {
|
|
248
120
|
const stepFuncList = params.path.map(step => {
|
|
249
|
-
if (step.action ===
|
|
250
|
-
return this.
|
|
121
|
+
if (step.action === _types.DynamicSwapType.SWAP) {
|
|
122
|
+
return this.getSubmitStep.bind(this);
|
|
251
123
|
}
|
|
252
|
-
if (step.action ===
|
|
253
|
-
return this.
|
|
124
|
+
if (step.action === _types.DynamicSwapType.BRIDGE) {
|
|
125
|
+
return this.swapBaseHandler.getBridgeStep.bind(this.swapBaseHandler);
|
|
254
126
|
}
|
|
255
127
|
throw new Error(`Error generating optimal process: Action ${step.action} is not supported`);
|
|
256
128
|
});
|
|
257
129
|
return this.swapBaseHandler.generateOptimalProcessV2(params, stepFuncList);
|
|
258
130
|
}
|
|
259
|
-
async handleXcmStep(params) {
|
|
260
|
-
const briefXcmStep = params.process.steps[params.currentStep].metadata;
|
|
261
|
-
if (!briefXcmStep || !briefXcmStep.originTokenInfo || !briefXcmStep.destinationTokenInfo || !briefXcmStep.sendingValue) {
|
|
262
|
-
throw new Error('XCM metadata error');
|
|
263
|
-
}
|
|
264
|
-
const originAsset = briefXcmStep.originTokenInfo;
|
|
265
|
-
const destinationAsset = briefXcmStep.destinationTokenInfo;
|
|
266
|
-
const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
|
|
267
|
-
const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
|
|
268
|
-
const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
|
|
269
|
-
const chainApi = await substrateApi.isReady;
|
|
270
|
-
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originAsset.originChain, 'substrate');
|
|
271
|
-
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
272
|
-
originTokenInfo: originAsset,
|
|
273
|
-
destinationTokenInfo: destinationAsset,
|
|
274
|
-
sendingValue: briefXcmStep.sendingValue,
|
|
275
|
-
recipient: params.address,
|
|
276
|
-
substrateApi: chainApi,
|
|
277
|
-
sender: params.address,
|
|
278
|
-
destinationChain,
|
|
279
|
-
originChain,
|
|
280
|
-
feeInfo
|
|
281
|
-
});
|
|
282
|
-
const xcmData = {
|
|
283
|
-
originNetworkKey: originAsset.originChain,
|
|
284
|
-
destinationNetworkKey: destinationAsset.originChain,
|
|
285
|
-
from: params.address,
|
|
286
|
-
to: params.address,
|
|
287
|
-
value: briefXcmStep.sendingValue,
|
|
288
|
-
tokenSlug: originAsset.slug,
|
|
289
|
-
showExtraWarning: true
|
|
290
|
-
};
|
|
291
|
-
return {
|
|
292
|
-
txChain: originAsset.originChain,
|
|
293
|
-
extrinsic: xcmTransfer,
|
|
294
|
-
transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ? briefXcmStep.sendingValue : '0',
|
|
295
|
-
extrinsicType: _KoniTypes.ExtrinsicType.TRANSFER_XCM,
|
|
296
|
-
chainType: _KoniTypes.ChainType.SUBSTRATE,
|
|
297
|
-
txData: xcmData
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
131
|
async handleSubmitStep(params) {
|
|
301
132
|
var _this$router;
|
|
302
133
|
const metadata = params.process.steps[params.currentStep].metadata;
|
|
@@ -312,17 +143,11 @@ class AssetHubSwapHandler {
|
|
|
312
143
|
process: params.process
|
|
313
144
|
};
|
|
314
145
|
const paths = params.quote.route.path.map(slug => this.chainService.getAssetBySlug(slug));
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
toAmount
|
|
318
|
-
} = params.quote;
|
|
319
|
-
|
|
320
|
-
// todo: move to gen process
|
|
321
|
-
const minReceive = new _bignumber.default(1 - params.slippage).times(toAmount).integerValue(_bignumber.default.ROUND_DOWN);
|
|
322
|
-
if (!params.address || !paths || !fromAmount || !minReceive) {
|
|
146
|
+
const minReceive = (0, _bignumber.default)((0, _utils2.getAmountAfterSlippage)(metadata.expectedReceive, params.slippage));
|
|
147
|
+
if (!params.address || !paths || !minReceive) {
|
|
323
148
|
throw new _SwapError.SwapError(_types.SwapErrorType.UNKNOWN);
|
|
324
149
|
}
|
|
325
|
-
const extrinsic = await ((_this$router = this.router) === null || _this$router === void 0 ? void 0 : _this$router.buildSwapExtrinsic(paths, params.address,
|
|
150
|
+
const extrinsic = await ((_this$router = this.router) === null || _this$router === void 0 ? void 0 : _this$router.buildSwapExtrinsic(paths, params.address, metadata.sendingValue, minReceive.toString()));
|
|
326
151
|
return {
|
|
327
152
|
txChain: fromAsset.originChain,
|
|
328
153
|
txData,
|
|
@@ -341,90 +166,13 @@ class AssetHubSwapHandler {
|
|
|
341
166
|
const type = process.steps[currentStep].type;
|
|
342
167
|
switch (type) {
|
|
343
168
|
case _types.CommonStepType.XCM:
|
|
344
|
-
return this.
|
|
169
|
+
return this.swapBaseHandler.handleBridgeStep(params);
|
|
345
170
|
case _types.SwapStepType.SWAP:
|
|
346
171
|
return this.handleSubmitStep(params);
|
|
347
172
|
default:
|
|
348
173
|
return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
|
|
349
174
|
}
|
|
350
175
|
}
|
|
351
|
-
async validateSwapStep(params, isXcmOk, stepIndex) {
|
|
352
|
-
// check swap quote timestamp
|
|
353
|
-
// check balance to pay transaction fee
|
|
354
|
-
// check balance against spending amount
|
|
355
|
-
if (!params.selectedQuote) {
|
|
356
|
-
return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
357
|
-
}
|
|
358
|
-
const selectedQuote = params.selectedQuote;
|
|
359
|
-
const currentTimestamp = +Date.now();
|
|
360
|
-
if (selectedQuote.aliveUntil <= currentTimestamp) {
|
|
361
|
-
return Promise.resolve([new _TransactionError.TransactionError(_types.SwapErrorType.QUOTE_TIMEOUT)]);
|
|
362
|
-
}
|
|
363
|
-
const stepFee = params.process.totalFee[stepIndex].feeComponent;
|
|
364
|
-
const networkFee = stepFee.find(fee => fee.feeType === _types.SwapFeeType.NETWORK_FEE);
|
|
365
|
-
if (!networkFee) {
|
|
366
|
-
return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
367
|
-
}
|
|
368
|
-
const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
369
|
-
const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
|
|
370
|
-
const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
|
|
371
|
-
const {
|
|
372
|
-
fromAmount,
|
|
373
|
-
minSwap
|
|
374
|
-
} = params.selectedQuote;
|
|
375
|
-
const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
376
|
-
const balanceError = (0, _swap._validateBalanceToSwapOnAssetHub)(fromAsset, feeTokenInfo, feeTokenChain, networkFee.amount, fromAssetBalance.value, feeTokenBalance.value, fromAmount, isXcmOk, minSwap);
|
|
377
|
-
if (balanceError) {
|
|
378
|
-
return Promise.resolve([balanceError]);
|
|
379
|
-
}
|
|
380
|
-
if (!params.recipient) {
|
|
381
|
-
return Promise.resolve([]);
|
|
382
|
-
}
|
|
383
|
-
const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
384
|
-
const toAssetChain = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
385
|
-
const recipientError = (0, _swap._validateSwapRecipient)(toAssetChain, params.recipient);
|
|
386
|
-
if (recipientError) {
|
|
387
|
-
return Promise.resolve([recipientError]);
|
|
388
|
-
}
|
|
389
|
-
return Promise.resolve([]);
|
|
390
|
-
}
|
|
391
|
-
async validateSwapProcess(params) {
|
|
392
|
-
const amount = params.selectedQuote.fromAmount;
|
|
393
|
-
const bnAmount = new _bignumber.default(amount);
|
|
394
|
-
if (bnAmount.lte(0)) {
|
|
395
|
-
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
396
|
-
}
|
|
397
|
-
const swapStep = params.process.steps.find(item => item.type === _types.SwapStepType.SWAP);
|
|
398
|
-
if (!swapStep) {
|
|
399
|
-
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR, 'Swap step not found')];
|
|
400
|
-
}
|
|
401
|
-
let isXcmOk = false;
|
|
402
|
-
const currentStep = params.currentStep;
|
|
403
|
-
for (const [index, step] of params.process.steps.entries()) {
|
|
404
|
-
if (currentStep > index) {
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
const getErrors = async () => {
|
|
408
|
-
switch (step.type) {
|
|
409
|
-
case _types.CommonStepType.DEFAULT:
|
|
410
|
-
return Promise.resolve([]);
|
|
411
|
-
case _types.CommonStepType.XCM:
|
|
412
|
-
return this.swapBaseHandler.validateXcmStepV2(params, index);
|
|
413
|
-
case _types.SwapStepType.SWAP:
|
|
414
|
-
return this.validateSwapStep(params, isXcmOk, index);
|
|
415
|
-
default:
|
|
416
|
-
return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
const errors = await getErrors();
|
|
420
|
-
if (errors.length) {
|
|
421
|
-
return errors;
|
|
422
|
-
} else if (step.type === _types.CommonStepType.XCM) {
|
|
423
|
-
isXcmOk = true;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
return [];
|
|
427
|
-
}
|
|
428
176
|
async validateSwapProcessV2(params) {
|
|
429
177
|
// todo: recheck address and recipient format in params
|
|
430
178
|
const {
|
|
@@ -437,24 +185,23 @@ class AssetHubSwapHandler {
|
|
|
437
185
|
if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
|
|
438
186
|
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
439
187
|
}
|
|
440
|
-
const actionList = process.
|
|
441
|
-
const
|
|
442
|
-
const
|
|
443
|
-
const
|
|
444
|
-
const
|
|
445
|
-
const xcmSwapXcm = firstStep === _types.CommonStepType.DEFAULT && secondStep === _types.CommonStepType.XCM && thirdStep === _types.SwapStepType.SWAP && fourthStep === _types.CommonStepType.XCM && !fifthStep;
|
|
188
|
+
const actionList = JSON.stringify(process.path.map(step => step.action));
|
|
189
|
+
const swap = actionList === JSON.stringify([_types.DynamicSwapType.SWAP]);
|
|
190
|
+
const swapXcm = actionList === JSON.stringify([_types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
191
|
+
const xcmSwap = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP]);
|
|
192
|
+
const xcmSwapXcm = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
|
|
446
193
|
if (swap) {
|
|
447
194
|
return this.swapBaseHandler.validateSwapOnlyProcess(params, 1); // todo: create interface for input request
|
|
448
195
|
}
|
|
449
196
|
|
|
450
197
|
if (swapXcm) {
|
|
451
|
-
return
|
|
198
|
+
return this.swapBaseHandler.validateSwapXcmProcess(params, 1, 2);
|
|
452
199
|
}
|
|
453
200
|
if (xcmSwap) {
|
|
454
201
|
return this.swapBaseHandler.validateXcmSwapProcess(params, 2, 1);
|
|
455
202
|
}
|
|
456
203
|
if (xcmSwapXcm) {
|
|
457
|
-
return
|
|
204
|
+
return this.swapBaseHandler.validateXcmSwapXcmProcess(params, 2, 1, 3);
|
|
458
205
|
}
|
|
459
206
|
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
460
207
|
}
|