Skip to content

Commit d486b15

Browse files
committed
[Issue-4079] feat: handle n-length path
1 parent 65cb1f9 commit d486b15

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ export default class KoniExtension {
13261326
const warnings: TransactionWarning[] = [];
13271327
const chainInfo = this.#koniState.getChainInfo(chain);
13281328

1329-
const test = generateSwapPairs(this.#koniState.getSubstrateApi(chain), this.#koniState.chainService, transferTokenInfo).map((asset) => asset.slug);
1329+
const test = generateSwapPairs(this.#koniState.getSubstrateApi(chain), this.#koniState.chainService, transferTokenInfo, 3).map((asset) => asset.slug);
13301330

13311331
console.log('test', test);
13321332

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

+58-12
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,41 @@ export function getChainflipBroker (isTestnet: boolean) { // noted: currently no
121121
}
122122

123123
export function generateSwapPairs (substrateApi: _SubstrateApi, chainService: ChainService, fromAsset: _ChainAsset, maxPathLength = 1) {
124+
if (maxPathLength < 1) {
125+
return [];
126+
}
127+
124128
let currentTargets: _ChainAsset[] = [];
125129
let newTargets: _ChainAsset[] = [];
126-
127-
if (maxPathLength < 1) {
128-
return currentTargets;
130+
let currentStep = 1;
131+
132+
// step 1:
133+
newTargets = findDestinations(chainService, fromAsset);
134+
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
135+
currentStep += 1;
136+
137+
// step 2 - n
138+
while (currentStep <= maxPathLength) { // todo: improve by stop when nothing new
139+
newTargets = Array.from(newTargets.reduce((farChildTargets, currentTarget) => {
140+
const farChildTargetsLocal = findDestinations(chainService, currentTarget);
141+
142+
return new Set([...farChildTargets, ...farChildTargetsLocal]);
143+
}, new Set<_ChainAsset>()));
144+
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
145+
currentStep += 1;
129146
}
130147

131-
const xcmTargets = findXcmDestinations(chainService, fromAsset);
132-
const swapTargets = findSwapDestinations(chainService, fromAsset);
148+
return currentTargets;
149+
}
133150

134-
newTargets = Array.from(new Set([...xcmTargets, ...swapTargets]));
135-
currentTargets = Array.from(new Set([...currentTargets, ...newTargets]));
151+
function findDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
152+
const xcmTargets = findXcmDestinations(chainService, chainAsset);
153+
const swapTargets = findSwapDestinations(chainService, chainAsset);
136154

137-
return currentTargets;
155+
return mergeWithoutDuplicate<_ChainAsset>(xcmTargets, swapTargets);
138156
}
139157

140-
export function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
158+
function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
141159
const xcmTargets: _ChainAsset[] = [];
142160
const multichainAssetSlug = _getMultiChainAsset(chainAsset);
143161

@@ -156,9 +174,9 @@ export function findXcmDestinations (chainService: ChainService, chainAsset: _Ch
156174
return xcmTargets;
157175
}
158176

159-
export function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
160-
const swapTargets: _ChainAsset[] = [];
177+
function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
161178
const chain = chainAsset.originChain;
179+
const swapTargets: _ChainAsset[] = [];
162180

163181
const availableChains = Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP).reduce((remainChains, currentChains) => {
164182
if (currentChains.includes(chain)) {
@@ -179,6 +197,34 @@ export function findSwapDestinations (chainService: ChainService, chainAsset: _C
179197
return swapTargets;
180198
}
181199

182-
export function isHasChannel (subtrateApi: _SubstrateApi, fromChain: _ChainAsset, toChain: _ChainAsset) {
200+
export function findSwapDestinationsV2 (chainService: ChainService, chainAsset: _ChainAsset) {
201+
const chain = chainAsset.originChain;
202+
const swapTargets: _ChainAsset[] = [];
203+
204+
// Convert to Set once at the start
205+
const availableChains = new Set<string>(
206+
Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP)
207+
.filter((chains) => chains.includes(chain))
208+
.flat()
209+
);
210+
211+
// Use Set for O(1) lookup instead of includes()
212+
for (const candidate of availableChains) {
213+
const assets = chainService.getAssetByChainAndType(
214+
candidate,
215+
_getFungibleAssetType()
216+
);
217+
218+
swapTargets.push(...Object.values(assets));
219+
}
220+
221+
return swapTargets;
222+
}
223+
224+
function isHasChannel (subtrateApi: _SubstrateApi, fromChain: _ChainAsset, toChain: _ChainAsset) {
183225
return true;
184226
}
227+
228+
function mergeWithoutDuplicate<T> (arr1: T[], arr2: T[]): T[] {
229+
return Array.from(new Set([...arr1, ...arr2]));
230+
}

0 commit comments

Comments
 (0)