@@ -121,20 +121,36 @@ 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 ) return [ ] ;
125
+
124
126
let currentTargets : _ChainAsset [ ] = [ ] ;
125
127
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 ;
129
144
}
130
145
131
- const xcmTargets = findXcmDestinations ( chainService , fromAsset ) ;
132
- const swapTargets = findSwapDestinations ( chainService , fromAsset ) ;
146
+ return currentTargets ;
147
+ }
133
148
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 ) ;
136
152
137
- return currentTargets ;
153
+ return mergeWithoutDuplicate < _ChainAsset > ( xcmTargets , swapTargets ) ;
138
154
}
139
155
140
156
export function findXcmDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
@@ -157,8 +173,8 @@ export function findXcmDestinations (chainService: ChainService, chainAsset: _Ch
157
173
}
158
174
159
175
export function findSwapDestinations ( chainService : ChainService , chainAsset : _ChainAsset ) {
160
- const swapTargets : _ChainAsset [ ] = [ ] ;
161
176
const chain = chainAsset . originChain ;
177
+ const swapTargets : _ChainAsset [ ] = [ ] ;
162
178
163
179
const availableChains = Object . values ( _PROVIDER_TO_SUPPORTED_PAIR_MAP ) . reduce ( ( remainChains , currentChains ) => {
164
180
if ( currentChains . includes ( chain ) ) {
@@ -179,6 +195,33 @@ export function findSwapDestinations (chainService: ChainService, chainAsset: _C
179
195
return swapTargets ;
180
196
}
181
197
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
+
182
221
export function isHasChannel ( subtrateApi : _SubstrateApi , fromChain : _ChainAsset , toChain : _ChainAsset ) {
183
222
return true ;
184
223
}
224
+
225
+ function mergeWithoutDuplicate < T > ( arr1 : T [ ] , arr2 : T [ ] ) : T [ ] {
226
+ return Array . from ( new Set ( [ ...arr1 , ...arr2 ] ) ) ;
227
+ }
0 commit comments