Skip to content

Commit 4d4c21d

Browse files
committed
[Issue-4078] feat: find first found process
1 parent 2d4d197 commit 4d4c21d

File tree

4 files changed

+159
-7
lines changed

4 files changed

+159
-7
lines changed

packages/extension-base/src/koni/background/handlers/Extension.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/reques
4848
import { AuthUrls } from '@subwallet/extension-base/services/request-service/types';
4949
import { DEFAULT_AUTO_LOCK_TIME } from '@subwallet/extension-base/services/setting-service/constants';
5050
import { checkLiquidityForPool, estimateTokensForPool, getReserveForPool } from '@subwallet/extension-base/services/swap-service/handler/asset-hub/utils';
51-
import { SWPermitTransaction, SWTransaction, SWTransactionInput, SWTransactionResponse, SWTransactionResult, TransactionEmitter, TransactionEventResponse, ValidateTransactionResponseInput } from '@subwallet/extension-base/services/transaction-service/types';
5251
import { generateSwapPairs } from '@subwallet/extension-base/services/swap-service/utils';
52+
import { SWPermitTransaction, SWTransaction, SWTransactionInput, SWTransactionResponse, SWTransactionResult, TransactionEmitter, TransactionEventResponse, ValidateTransactionResponseInput } from '@subwallet/extension-base/services/transaction-service/types';
5353
import { isProposalExpired, isSupportWalletConnectChain, isSupportWalletConnectNamespace } from '@subwallet/extension-base/services/wallet-connect-service/helpers';
5454
import { ResultApproveWalletConnectSession, WalletConnectNotSupportRequest, WalletConnectSessionRequest } from '@subwallet/extension-base/services/wallet-connect-service/types';
5555
import { SWStorage } from '@subwallet/extension-base/storage';
@@ -1345,6 +1345,7 @@ export default class KoniExtension {
13451345
const warnings: TransactionWarning[] = [];
13461346
const chainInfo = this.#koniState.getChainInfo(chain);
13471347

1348+
// todo: remove test:
13481349
const test = generateSwapPairs(this.#koniState.getSubstrateApi(chain), this.#koniState.chainService, transferTokenInfo, 3).map((asset) => asset.slug);
13491350

13501351
console.log('test', test);
@@ -4069,6 +4070,16 @@ export default class KoniExtension {
40694070
}
40704071

40714072
private async handleSwapRequest (request: SwapRequest): Promise<SwapRequestResult> {
4073+
// todo: remove test:
4074+
const testSwapRequest = this.#koniState.swapService.handleSwapRequestV2({
4075+
address: '1BzDB5n2rfSJwvuCW9deKY9XnUyys8Gy44SoX8tRNDCFBhx',
4076+
fromAmount: '1',
4077+
fromToken: 'hydradx_main-LOCAL-PINK',
4078+
toToken: 'ethereum-NATIVE-ETH'
4079+
});
4080+
4081+
console.log('testSwapRequest', testSwapRequest);
4082+
40724083
return this.#koniState.swapService.handleSwapRequest(request);
40734084
}
40744085

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

+136-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ 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';
910
import { EventService } from '@subwallet/extension-base/services/event-service';
1011
import { AssetHubSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/asset-hub';
1112
import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
1213
import { ChainflipSwapHandler } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
1314
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';
1617
import { CommonOptimalPath, DEFAULT_FIRST_STEP, MOCK_STEP_FEE } from '@subwallet/extension-base/types/service-base';
1718
import { _SUPPORTED_SWAP_PROVIDERS, OptimalSwapPathParams, QuoteAskResponse, SwapErrorType, SwapPair, SwapProviderId, SwapQuote, SwapQuoteResponse, SwapRequest, SwapRequestResult, SwapStepType, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
1819
import { createPromiseHandler, PromiseHandler } from '@subwallet/extension-base/utils';
@@ -131,6 +132,139 @@ export class SwapService implements ServiceWithProcessInterface, StoppableServic
131132
} as SwapRequestResult;
132133
}
133134

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+
134268
public async getLatestQuotes (request: SwapRequest): Promise<SwapQuoteResponse> {
135269
request.pair.metadata = this.getSwapPairMetadata(request.pair.slug); // todo: improve this
136270
const quoteAskResponses = await this.askProvidersForQuote(request);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function findDestinations (chainService: ChainService, chainAsset: _ChainAsset)
138138
return mergeWithoutDuplicate<_ChainAsset>(xcmTargets, swapTargets);
139139
}
140140

141-
function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
141+
export function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
142142
const xcmTargets: _ChainAsset[] = [];
143143
const multichainAssetSlug = _getMultiChainAsset(chainAsset);
144144

@@ -154,10 +154,10 @@ function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsse
154154
}
155155
}
156156

157-
return xcmTargets;
157+
return xcmTargets.filter((candidate) => candidate.slug !== chainAsset.slug);
158158
}
159159

160-
function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
160+
export function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
161161
const chain = chainAsset.originChain;
162162
const swapTargets: _ChainAsset[] = [];
163163

@@ -177,7 +177,7 @@ function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAss
177177
swapTargets.push(...Object.values(assets));
178178
});
179179

180-
return swapTargets;
180+
return swapTargets.filter((candidate) => candidate.slug !== chainAsset.slug);
181181
}
182182

183183
// @ts-ignore

packages/extension-base/src/types/swap/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ export interface SwapRequest {
169169
currentQuote?: SwapProvider
170170
}
171171

172+
export interface SwapRequestV2 {
173+
address: string;
174+
fromAmount: string;
175+
fromToken: string;
176+
toToken: string;
177+
}
178+
172179
export interface SwapRequestResult {
173180
process: CommonOptimalPath;
174181
quote: SwapQuoteResponse;

0 commit comments

Comments
 (0)