@@ -6,13 +6,12 @@ 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' ;
10
9
import { EventService } from '@subwallet/extension-base/services/event-service' ;
11
10
import { AssetHubSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/asset-hub' ;
12
11
import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler' ;
13
12
import { ChainflipSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler' ;
14
13
import { HydradxHandler } from '@subwallet/extension-base/services/swap-service/handler/hydradx-handler' ;
15
- import { _PROVIDER_TO_SUPPORTED_PAIR_MAP , findSwapDestinations , findXcmDestinations , getInitStep , getSwapAltToken , getSwapStep , getXcmStep , SWAP_QUOTE_TIMEOUT_MAP } from '@subwallet/extension-base/services/swap-service/utils' ;
14
+ import { _PROVIDER_TO_SUPPORTED_PAIR_MAP , DynamicSwapAction , findSwapDestinations , findXcmDestinations , getInitStep , getSwapAltToken , getSwapStep , getXcmStep , isEquiValentAsset , SWAP_QUOTE_TIMEOUT_MAP } from '@subwallet/extension-base/services/swap-service/utils' ;
16
15
import { BasicTxErrorType , SwapRequestV2 } from '@subwallet/extension-base/types' ;
17
16
import { CommonOptimalPath , DEFAULT_FIRST_STEP , MOCK_STEP_FEE } from '@subwallet/extension-base/types/service-base' ;
18
17
import { _SUPPORTED_SWAP_PROVIDERS , OptimalSwapPathParams , QuoteAskResponse , SwapErrorType , SwapPair , SwapProviderId , SwapQuote , SwapQuoteResponse , SwapRequest , SwapRequestResult , SwapStepType , SwapSubmitParams , SwapSubmitStepData , ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap' ;
@@ -132,7 +131,7 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
132
131
} as SwapRequestResult ;
133
132
}
134
133
135
- public handleSwapRequestV2 ( request : SwapRequestV2 ) {
134
+ public handleSwapRequestV2 ( request : SwapRequestV2 ) : DynamicSwapAction [ ] [ ] {
136
135
const { fromToken, toToken } = request ;
137
136
const fromTokenInfo = this . chainService . getAssetBySlug ( fromToken ) ;
138
137
const toTokenInfo = this . chainService . getAssetBySlug ( toToken ) ;
@@ -146,47 +145,54 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
146
145
return [ ] ;
147
146
}
148
147
149
- const steps = [ getInitStep ( fromToken ) ] ;
148
+ const routes : DynamicSwapAction [ ] [ ] = [ ] ;
149
+ let steps : DynamicSwapAction [ ] = [ ] ;
150
150
151
- // todo: try find swap
151
+ // try find swap
152
152
if ( fromChain === toChain ) {
153
153
if ( firstSwapDes . includes ( toTokenInfo ) ) {
154
+ steps . push ( getInitStep ( fromToken ) ) ;
154
155
steps . push ( getSwapStep ( toToken ) ) ;
156
+ routes . push ( steps ) ;
155
157
156
- return steps ;
158
+ return routes ;
157
159
}
158
160
}
159
161
160
- // todo: try find xcm
161
- if ( _getMultiChainAsset ( fromTokenInfo ) === _getMultiChainAsset ( toTokenInfo ) || _getAssetPriceId ( fromTokenInfo ) === _getAssetPriceId ( toTokenInfo ) || _getAssetSymbol ( fromTokenInfo ) === _getAssetSymbol ( toTokenInfo ) ) {
162
+ // try find xcm
163
+ if ( isEquiValentAsset ( fromTokenInfo , toTokenInfo ) ) {
162
164
if ( firstXcmDes . includes ( toTokenInfo ) ) {
165
+ steps . push ( getInitStep ( fromToken ) ) ;
163
166
steps . push ( getXcmStep ( toToken ) ) ;
167
+ routes . push ( steps ) ;
164
168
165
- return steps ;
169
+ return routes ;
166
170
}
167
171
}
168
172
169
- // todo: brute force find process
173
+ // brute force to find nested routes
170
174
for ( const xcmAsset of firstXcmDes ) {
171
175
const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
172
176
173
177
if ( ! swapDesChild . length ) {
174
178
/* empty */
175
179
} else if ( swapDesChild . includes ( toTokenInfo ) ) {
180
+ steps = [ ] ;
181
+ steps . push ( getInitStep ( fromToken ) ) ;
176
182
steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
177
183
steps . push ( getSwapStep ( toToken ) ) ;
178
-
179
- return steps ;
184
+ routes . push ( steps ) ;
180
185
} else {
181
186
for ( const swapAsset of swapDesChild ) {
182
187
const xcmDesChild = findXcmDestinations ( this . chainService , swapAsset ) ;
183
188
184
189
if ( xcmDesChild . includes ( toTokenInfo ) ) {
190
+ steps = [ ] ;
191
+ steps . push ( getInitStep ( fromToken ) ) ;
185
192
steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
186
193
steps . push ( getSwapStep ( swapAsset . slug ) ) ;
187
194
steps . push ( getXcmStep ( toToken ) ) ;
188
-
189
- return steps ;
195
+ routes . push ( steps ) ;
190
196
}
191
197
}
192
198
}
@@ -198,26 +204,28 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
198
204
if ( ! xcmDesChild . length ) {
199
205
/* empty */
200
206
} else if ( xcmDesChild . includes ( toTokenInfo ) ) {
207
+ steps = [ ] ;
208
+ steps . push ( getInitStep ( fromToken ) ) ;
201
209
steps . push ( getSwapStep ( swapAsset . slug ) ) ;
202
210
steps . push ( getXcmStep ( toToken ) ) ;
203
-
204
- return steps ;
211
+ routes . push ( steps ) ;
205
212
} else {
206
213
for ( const xcmAsset of xcmDesChild ) {
207
214
const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
208
215
209
216
if ( swapDesChild . includes ( toTokenInfo ) ) {
217
+ steps = [ ] ;
218
+ steps . push ( getInitStep ( fromToken ) ) ;
210
219
steps . push ( getSwapStep ( swapAsset . slug ) ) ;
211
220
steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
212
221
steps . push ( getSwapStep ( toToken ) ) ;
213
-
214
- return steps ;
222
+ routes . push ( steps ) ;
215
223
}
216
224
}
217
225
}
218
226
}
219
227
220
- return [ ] ;
228
+ return routes ;
221
229
}
222
230
223
231
public async getLatestQuotes ( request : SwapRequest ) : Promise < SwapQuoteResponse > {
0 commit comments