Skip to content

Commit 19dbef0

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

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
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, 7).map((asset) => asset.slug);
13301330

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

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

+52-9
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,36 @@ 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) return [];
125+
124126
let currentTargets: _ChainAsset[] = [];
125127
let newTargets: _ChainAsset[] = [];
126-
127-
if (maxPathLength < 1) {
128-
return currentTargets;
128+
let currentStep = 1;
129+
130+
// step 1:
131+
newTargets = findDestinations(chainService, fromAsset);
132+
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
133+
currentStep += 1;
134+
135+
// step 2 - n
136+
while (currentStep <= maxPathLength) { // todo: improve by stop when nothing new
137+
newTargets = Array.from(newTargets.reduce((farChildTargets, currentTarget) => {
138+
const farChildTargetsLocal = findDestinations(chainService, currentTarget);
139+
140+
return new Set([...farChildTargets, ...farChildTargetsLocal]);
141+
}, new Set<_ChainAsset>()));
142+
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
143+
currentStep += 1;
129144
}
130145

131-
const xcmTargets = findXcmDestinations(chainService, fromAsset);
132-
const swapTargets = findSwapDestinations(chainService, fromAsset);
146+
return currentTargets;
147+
}
133148

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

137-
return currentTargets;
153+
return mergeWithoutDuplicate<_ChainAsset>(xcmTargets, swapTargets);
138154
}
139155

140156
export function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
@@ -157,8 +173,8 @@ export function findXcmDestinations (chainService: ChainService, chainAsset: _Ch
157173
}
158174

159175
export function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
160-
const swapTargets: _ChainAsset[] = [];
161176
const chain = chainAsset.originChain;
177+
const swapTargets: _ChainAsset[] = [];
162178

163179
const availableChains = Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP).reduce((remainChains, currentChains) => {
164180
if (currentChains.includes(chain)) {
@@ -179,6 +195,33 @@ export function findSwapDestinations (chainService: ChainService, chainAsset: _C
179195
return swapTargets;
180196
}
181197

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

0 commit comments

Comments
 (0)