@@ -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 , getInitStep , getSwapAltToken , getSwapStep , getXcmStep , 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,94 @@ 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 firstSwapDes = findSwapDestinations ( this . chainService , fromTokenInfo ) ;
143
+ const firstXcmDes = findXcmDestinations ( this . chainService , fromTokenInfo ) ;
144
+
145
+ if ( ! firstSwapDes . length && ! firstXcmDes . length ) {
146
+ return [ ] ;
147
+ }
148
+
149
+ const steps = [ getInitStep ( fromToken ) ] ;
150
+
151
+ // todo: try find swap
152
+ if ( fromChain === toChain ) {
153
+ if ( firstSwapDes . includes ( toTokenInfo ) ) {
154
+ steps . push ( getSwapStep ( toToken ) ) ;
155
+
156
+ return steps ;
157
+ }
158
+ }
159
+
160
+ // todo: try find xcm
161
+ if ( _getMultiChainAsset ( fromTokenInfo ) === _getMultiChainAsset ( toTokenInfo ) || _getAssetPriceId ( fromTokenInfo ) === _getAssetPriceId ( toTokenInfo ) || _getAssetSymbol ( fromTokenInfo ) === _getAssetSymbol ( toTokenInfo ) ) {
162
+ if ( firstXcmDes . includes ( toTokenInfo ) ) {
163
+ steps . push ( getXcmStep ( toToken ) ) ;
164
+
165
+ return steps ;
166
+ }
167
+ }
168
+
169
+ // todo: brute force find process
170
+ for ( const xcmAsset of firstXcmDes ) {
171
+ const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
172
+
173
+ if ( ! swapDesChild . length ) {
174
+ /* empty */
175
+ } else if ( swapDesChild . includes ( toTokenInfo ) ) {
176
+ steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
177
+ steps . push ( getSwapStep ( toToken ) ) ;
178
+
179
+ return steps ;
180
+ } else {
181
+ for ( const swapAsset of swapDesChild ) {
182
+ const xcmDesChild = findXcmDestinations ( this . chainService , swapAsset ) ;
183
+
184
+ if ( xcmDesChild . includes ( toTokenInfo ) ) {
185
+ steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
186
+ steps . push ( getSwapStep ( swapAsset . slug ) ) ;
187
+ steps . push ( getXcmStep ( toToken ) ) ;
188
+
189
+ return steps ;
190
+ }
191
+ }
192
+ }
193
+ }
194
+
195
+ for ( const swapAsset of firstSwapDes ) {
196
+ const xcmDesChild = findXcmDestinations ( this . chainService , swapAsset ) ;
197
+
198
+ if ( ! xcmDesChild . length ) {
199
+ /* empty */
200
+ } else if ( xcmDesChild . includes ( toTokenInfo ) ) {
201
+ steps . push ( getSwapStep ( swapAsset . slug ) ) ;
202
+ steps . push ( getXcmStep ( toToken ) ) ;
203
+
204
+ return steps ;
205
+ } else {
206
+ for ( const xcmAsset of xcmDesChild ) {
207
+ const swapDesChild = findSwapDestinations ( this . chainService , xcmAsset ) ;
208
+
209
+ if ( swapDesChild . includes ( toTokenInfo ) ) {
210
+ steps . push ( getSwapStep ( swapAsset . slug ) ) ;
211
+ steps . push ( getXcmStep ( xcmAsset . slug ) ) ;
212
+ steps . push ( getSwapStep ( toToken ) ) ;
213
+
214
+ return steps ;
215
+ }
216
+ }
217
+ }
218
+ }
219
+
220
+ return [ ] ;
221
+ }
222
+
134
223
public async getLatestQuotes ( request : SwapRequest ) : Promise < SwapQuoteResponse > {
135
224
request . pair . metadata = this . getSwapPairMetadata ( request . pair . slug ) ; // todo: improve this
136
225
const quoteAskResponses = await this . askProvidersForQuote ( request ) ;
0 commit comments