@@ -121,23 +121,41 @@ export function getChainflipBroker (isTestnet: boolean) { // noted: currently no
121
121
}
122
122
123
123
export function generateSwapPairs ( substrateApi : _SubstrateApi , chainService : ChainService , fromAsset : _ChainAsset , maxPathLength = 1 ) {
124
+ if ( maxPathLength < 1 ) {
125
+ return [ ] ;
126
+ }
127
+
124
128
let currentTargets : _ChainAsset [ ] = [ ] ;
125
129
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 ;
129
146
}
130
147
131
- const xcmTargets = findXcmDestinations ( chainService , fromAsset ) ;
132
- const swapTargets = findSwapDestinations ( chainService , fromAsset ) ;
148
+ return currentTargets ;
149
+ }
133
150
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 ) ;
136
154
137
- return currentTargets ;
155
+ return mergeWithoutDuplicate < _ChainAsset > ( xcmTargets , swapTargets ) ;
138
156
}
139
157
140
- export function findXcmDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
158
+ function findXcmDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
141
159
const xcmTargets : _ChainAsset [ ] = [ ] ;
142
160
const multichainAssetSlug = _getMultiChainAsset ( chainAsset ) ;
143
161
@@ -156,9 +174,9 @@ export function findXcmDestinations (chainService: ChainService, chainAsset: _Ch
156
174
return xcmTargets ;
157
175
}
158
176
159
- export function findSwapDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
160
- const swapTargets : _ChainAsset [ ] = [ ] ;
177
+ function findSwapDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
161
178
const chain = chainAsset . originChain ;
179
+ const swapTargets : _ChainAsset [ ] = [ ] ;
162
180
163
181
const availableChains = Object . values ( _PROVIDER_TO_SUPPORTED_PAIR_MAP ) . reduce ( ( remainChains , currentChains ) => {
164
182
if ( currentChains . includes ( chain ) ) {
@@ -179,6 +197,34 @@ export function findSwapDestinations (chainService: ChainService, chainAsset: _C
179
197
return swapTargets ;
180
198
}
181
199
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 ) {
183
225
return true ;
184
226
}
227
+
228
+ function mergeWithoutDuplicate < T > ( arr1 : T [ ] , arr2 : T [ ] ) : T [ ] {
229
+ return Array . from ( new Set ( [ ...arr1 , ...arr2 ] ) ) ;
230
+ }
0 commit comments