@@ -6,13 +6,14 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
6
6
import KoniState from '@subwallet/extension-base/koni/background/handlers/State' ;
7
7
import { ServiceStatus , ServiceWithProcessInterface , StoppableServiceInterface } from '@subwallet/extension-base/services/base/types' ;
8
8
import { ChainService } from '@subwallet/extension-base/services/chain-service' ;
9
+ import { _getAssetPriceId , _getAssetSymbol , _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils' ;
9
10
import { EventService } from '@subwallet/extension-base/services/event-service' ;
10
11
import { AssetHubSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/asset-hub' ;
11
12
import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler' ;
12
13
import { ChainflipSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler' ;
13
14
import { HydradxHandler } from '@subwallet/extension-base/services/swap-service/handler/hydradx-handler' ;
14
- import { _PROVIDER_TO_SUPPORTED_PAIR_MAP , getSwapAltToken , SWAP_QUOTE_TIMEOUT_MAP } from '@subwallet/extension-base/services/swap-service/utils' ;
15
- import { BasicTxErrorType } from '@subwallet/extension-base/types' ;
15
+ import { _PROVIDER_TO_SUPPORTED_PAIR_MAP , findSwapDestinations , findXcmDestinations , getSwapAltToken , SWAP_QUOTE_TIMEOUT_MAP } from '@subwallet/extension-base/services/swap-service/utils' ;
16
+ import { BasicTxErrorType , SwapRequestV2 } from '@subwallet/extension-base/types' ;
16
17
import { CommonOptimalPath , DEFAULT_FIRST_STEP , MOCK_STEP_FEE } from '@subwallet/extension-base/types/service-base' ;
17
18
import { _SUPPORTED_SWAP_PROVIDERS , OptimalSwapPathParams , QuoteAskResponse , SwapErrorType , SwapPair , SwapProviderId , SwapQuote , SwapQuoteResponse , SwapRequest , SwapRequestResult , SwapStepType , SwapSubmitParams , SwapSubmitStepData , ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap' ;
18
19
import { createPromiseHandler , PromiseHandler } from '@subwallet/extension-base/utils' ;
@@ -131,6 +132,139 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
131
132
} as SwapRequestResult ;
132
133
}
133
134
135
+ public handleSwapRequestV2 ( request : SwapRequestV2 ) {
136
+ const { fromToken, toToken } = request ;
137
+ const fromTokenInfo = this . chainService . getAssetBySlug ( fromToken ) ;
138
+ const toTokenInfo = this . chainService . getAssetBySlug ( toToken ) ;
139
+ const fromChain = fromTokenInfo . originChain ;
140
+ const toChain = toTokenInfo . originChain ;
141
+
142
+ const steps = [ { // init step
143
+ action : 'INIT' ,
144
+ toToken : fromToken
145
+ } ] ;
146
+
147
+ const swapDes = findSwapDestinations ( this . chainService , fromTokenInfo ) ;
148
+ const xcmDes = findXcmDestinations ( this . chainService , fromTokenInfo ) ;
149
+
150
+ if ( ! swapDes . length && ! xcmDes . length ) {
151
+ return [ ] ;
152
+ }
153
+
154
+ // todo: try find swap
155
+ if ( fromChain === toChain ) {
156
+ if ( swapDes . includes ( toTokenInfo ) ) {
157
+ steps . push ( {
158
+ action : 'SWAP' ,
159
+ toToken : toToken
160
+ } ) ;
161
+
162
+ return steps ;
163
+ }
164
+ }
165
+
166
+ // todo: try find xcm
167
+ if ( _getMultiChainAsset ( fromTokenInfo ) === _getMultiChainAsset ( toTokenInfo ) || _getAssetPriceId ( fromTokenInfo ) === _getAssetPriceId ( toTokenInfo ) || _getAssetSymbol ( fromTokenInfo ) === _getAssetSymbol ( toTokenInfo ) ) {
168
+ if ( xcmDes . includes ( toTokenInfo ) ) {
169
+ steps . push ( {
170
+ action : 'XCM' ,
171
+ toToken : toToken
172
+ } ) ;
173
+
174
+ return steps ;
175
+ }
176
+ }
177
+
178
+ // todo: brute force find process
179
+ for ( const xcmAsset of xcmDes ) {
180
+ const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
181
+
182
+ if ( ! swapDesChild . length ) {
183
+ /* empty */
184
+ } else if ( swapDesChild . includes ( toTokenInfo ) ) {
185
+ steps . push ( {
186
+ action : 'XCM' ,
187
+ toToken : xcmAsset . slug
188
+ } ) ;
189
+
190
+ steps . push ( {
191
+ action : 'SWAP' ,
192
+ toToken : toToken
193
+ } ) ;
194
+
195
+ return steps ;
196
+ } else {
197
+ for ( const swapAsset of swapDesChild ) {
198
+ const xcmDesChild = findXcmDestinations ( this . chainService , swapAsset ) ;
199
+
200
+ if ( xcmDesChild . includes ( toTokenInfo ) ) {
201
+ steps . push ( {
202
+ action : 'XCM' ,
203
+ toToken : xcmAsset . slug
204
+ } ) ;
205
+
206
+ steps . push ( {
207
+ action : 'SWAP' ,
208
+ toToken : swapAsset . slug
209
+ } ) ;
210
+
211
+ steps . push ( {
212
+ action : 'XCM' ,
213
+ toToken : toToken
214
+ } ) ;
215
+
216
+ return steps ;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ for ( const swapAsset of swapDes ) {
223
+ const xcmDesChild = findXcmDestinations ( this . chainService , swapAsset ) ;
224
+
225
+ if ( ! xcmDesChild . length ) {
226
+ /* empty */
227
+ } else if ( xcmDesChild . includes ( toTokenInfo ) ) {
228
+ steps . push ( {
229
+ action : 'SWAP' ,
230
+ toToken : swapAsset . slug
231
+ } ) ;
232
+
233
+ steps . push ( {
234
+ action : 'XCM' ,
235
+ toToken : toToken
236
+ } ) ;
237
+
238
+ return steps ;
239
+ } else {
240
+ for ( const xcmAsset of xcmDesChild ) {
241
+ const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
242
+
243
+ if ( swapDesChild . includes ( toTokenInfo ) ) {
244
+ steps . push ( {
245
+ action : 'SWAP' ,
246
+ toToken : swapAsset . slug
247
+ } ) ;
248
+
249
+ steps . push ( {
250
+ action : 'XCM' ,
251
+ toToken : xcmAsset . slug
252
+ } ) ;
253
+
254
+ steps . push ( {
255
+ action : 'SWAP' ,
256
+ toToken : toToken
257
+ } ) ;
258
+
259
+ return steps ;
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ return steps ;
266
+ }
267
+
134
268
public async getLatestQuotes ( request : SwapRequest ) : Promise < SwapQuoteResponse > {
135
269
request . pair . metadata = this . getSwapPairMetadata ( request . pair . slug ) ; // todo: improve this
136
270
const quoteAskResponses = await this . askProvidersForQuote ( request ) ;
0 commit comments