Skip to content

Commit 5191a9f

Browse files
committed
[Issue-4078] feat: find all routes
1 parent 9310af6 commit 5191a9f

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

packages/extension-base/src/services/swap-service/index.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
66
import KoniState from '@subwallet/extension-base/koni/background/handlers/State';
77
import { ServiceStatus, ServiceWithProcessInterface, StoppableServiceInterface } from '@subwallet/extension-base/services/base/types';
88
import { ChainService } from '@subwallet/extension-base/services/chain-service';
9-
import { _getAssetPriceId, _getAssetSymbol, _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils';
109
import { EventService } from '@subwallet/extension-base/services/event-service';
1110
import { AssetHubSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/asset-hub';
1211
import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
1312
import { ChainflipSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
1413
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';
1615
import { BasicTxErrorType, SwapRequestV2 } from '@subwallet/extension-base/types';
1716
import { CommonOptimalPath, DEFAULT_FIRST_STEP, MOCK_STEP_FEE } from '@subwallet/extension-base/types/service-base';
1817
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
132131
} as SwapRequestResult;
133132
}
134133

135-
public handleSwapRequestV2 (request: SwapRequestV2) {
134+
public handleSwapRequestV2 (request: SwapRequestV2): DynamicSwapAction[][] {
136135
const { fromToken, toToken } = request;
137136
const fromTokenInfo = this.chainService.getAssetBySlug(fromToken);
138137
const toTokenInfo = this.chainService.getAssetBySlug(toToken);
@@ -146,47 +145,54 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
146145
return [];
147146
}
148147

149-
const steps = [getInitStep(fromToken)];
148+
const routes: DynamicSwapAction[][] = [];
149+
let steps: DynamicSwapAction[] = [];
150150

151-
// todo: try find swap
151+
// try find swap
152152
if (fromChain === toChain) {
153153
if (firstSwapDes.includes(toTokenInfo)) {
154+
steps.push(getInitStep(fromToken));
154155
steps.push(getSwapStep(toToken));
156+
routes.push(steps);
155157

156-
return steps;
158+
return routes;
157159
}
158160
}
159161

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)) {
162164
if (firstXcmDes.includes(toTokenInfo)) {
165+
steps.push(getInitStep(fromToken));
163166
steps.push(getXcmStep(toToken));
167+
routes.push(steps);
164168

165-
return steps;
169+
return routes;
166170
}
167171
}
168172

169-
// todo: brute force find process
173+
// brute force to find nested routes
170174
for (const xcmAsset of firstXcmDes) {
171175
const swapDesChild = findSwapDestinations(this.chainService, xcmAsset);
172176

173177
if (!swapDesChild.length) {
174178
/* empty */
175179
} else if (swapDesChild.includes(toTokenInfo)) {
180+
steps = [];
181+
steps.push(getInitStep(fromToken));
176182
steps.push(getXcmStep(xcmAsset.slug));
177183
steps.push(getSwapStep(toToken));
178-
179-
return steps;
184+
routes.push(steps);
180185
} else {
181186
for (const swapAsset of swapDesChild) {
182187
const xcmDesChild = findXcmDestinations(this.chainService, swapAsset);
183188

184189
if (xcmDesChild.includes(toTokenInfo)) {
190+
steps = [];
191+
steps.push(getInitStep(fromToken));
185192
steps.push(getXcmStep(xcmAsset.slug));
186193
steps.push(getSwapStep(swapAsset.slug));
187194
steps.push(getXcmStep(toToken));
188-
189-
return steps;
195+
routes.push(steps);
190196
}
191197
}
192198
}
@@ -198,26 +204,28 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
198204
if (!xcmDesChild.length) {
199205
/* empty */
200206
} else if (xcmDesChild.includes(toTokenInfo)) {
207+
steps = [];
208+
steps.push(getInitStep(fromToken));
201209
steps.push(getSwapStep(swapAsset.slug));
202210
steps.push(getXcmStep(toToken));
203-
204-
return steps;
211+
routes.push(steps);
205212
} else {
206213
for (const xcmAsset of xcmDesChild) {
207214
const swapDesChild = findSwapDestinations(this.chainService, xcmAsset);
208215

209216
if (swapDesChild.includes(toTokenInfo)) {
217+
steps = [];
218+
steps.push(getInitStep(fromToken));
210219
steps.push(getSwapStep(swapAsset.slug));
211220
steps.push(getXcmStep(xcmAsset.slug));
212221
steps.push(getSwapStep(toToken));
213-
214-
return steps;
222+
routes.push(steps);
215223
}
216224
}
217225
}
218226
}
219227

220-
return [];
228+
return routes;
221229
}
222230

223231
public async getLatestQuotes (request: SwapRequest): Promise<SwapQuoteResponse> {

packages/extension-base/src/services/swap-service/utils.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { COMMON_ASSETS, COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
55
import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
66
import { ChainService } from '@subwallet/extension-base/services/chain-service';
77
import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
8-
import { _getAssetDecimals, _getFungibleAssetType, _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils';
8+
import { _getAssetDecimals, _getAssetPriceId, _getAssetSymbol, _getFungibleAssetType, _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils';
99
import { CHAINFLIP_BROKER_API } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
1010
import { SwapPair, SwapProviderId } from '@subwallet/extension-base/types/swap';
1111
import BigN from 'bignumber.js';
@@ -262,3 +262,11 @@ export function getSwapStep (tokenSlug: string): DynamicSwapAction {
262262
toToken: tokenSlug
263263
};
264264
}
265+
266+
export function isEquiValentAsset (from: _ChainAsset, to: _ChainAsset) {
267+
return (
268+
_getMultiChainAsset(from) === _getMultiChainAsset(to) ||
269+
_getAssetPriceId(from) === _getAssetPriceId(to) ||
270+
_getAssetSymbol(from) === _getAssetSymbol(to)
271+
);
272+
}

0 commit comments

Comments
 (0)