@@ -11,10 +11,13 @@ import {
11
11
ReceiptType ,
12
12
type TransactionResultReceipt ,
13
13
} from 'fuels' ;
14
- import type { ContractCallMetadata , SimplifiedOperation } from '../types' ;
14
+ import type {
15
+ BidirectionalInfo ,
16
+ ContractCallMetadata ,
17
+ SimplifiedOperation ,
18
+ } from '../types' ;
15
19
import { TxCategory } from '../types' ;
16
- import type { CategorizedOperations } from '../types' ;
17
- import type { SimplifiedTransaction } from '../types.tsx' ;
20
+ import type { CategorizedOperations , SimplifiedTransaction } from '../types' ;
18
21
19
22
type TransactionRequestWithOrigin = TransactionRequest & {
20
23
origin ?: string ;
@@ -116,6 +119,33 @@ function transformOperation(
116
119
return baseOperation ;
117
120
}
118
121
122
+ function calculateBidirectionalInfo (
123
+ operations : SimplifiedOperation [ ] ,
124
+ currentIndex : number
125
+ ) : BidirectionalInfo {
126
+ const current = operations [ currentIndex ] ;
127
+ const next = operations [ currentIndex + 1 ] ;
128
+ const previous = operations [ currentIndex - 1 ] ;
129
+
130
+ if (
131
+ next &&
132
+ next . from . address === current . to . address &&
133
+ next . to . address === current . from . address
134
+ ) {
135
+ return 'atob' ;
136
+ }
137
+
138
+ if (
139
+ previous &&
140
+ previous . from . address === current . to . address &&
141
+ previous . to . address === current . from . address
142
+ ) {
143
+ return 'btoa' ;
144
+ }
145
+
146
+ return null ;
147
+ }
148
+
119
149
export function transformOperations (
120
150
summary : TransactionSummary ,
121
151
currentAccount ?: string
@@ -155,18 +185,11 @@ export function transformOperations(
155
185
( a , b ) => ( a . metadata ?. depth || 0 ) - ( b . metadata ?. depth || 0 )
156
186
) ;
157
187
158
- return operations ;
159
- }
160
-
161
- // Helper to create a unique key for identical operations
162
- function getOperationKey ( op : SimplifiedOperation ) {
163
- return JSON . stringify ( {
164
- type : op . type ,
165
- from : op . from . address ,
166
- to : op . to . address ,
167
- // Sort assets to ensure consistent key regardless of array order
168
- assets : ( op . assets || [ ] ) . map ( ( a ) => `${ a . assetId } -${ a . amount } ` ) . sort ( ) ,
169
- } ) ;
188
+ // Calculate bidirectional info for each operation
189
+ return operations . map ( ( op , index ) => ( {
190
+ ...op ,
191
+ bidirectionalInfo : calculateBidirectionalInfo ( operations , index ) ,
192
+ } ) ) ;
170
193
}
171
194
172
195
function groupSimilarOperations (
@@ -187,7 +210,7 @@ function groupSimilarOperations(
187
210
groupedAssets : { } ,
188
211
childOperations : [ op ] ,
189
212
// New: track identical operations within group
190
- identicalOps : new Map ( ) ,
213
+ identicalOps : [ ] ,
191
214
} ,
192
215
} ;
193
216
} else {
@@ -196,17 +219,18 @@ function groupSimilarOperations(
196
219
acc [ key ] . metadata . childOperations ! . push ( op ) ;
197
220
198
221
// Group identical operations
199
- const identicalKey = getOperationKey ( op ) ;
200
- const identicalGroup = acc [ key ] . metadata ?. identicalOps ?. get (
201
- identicalKey
222
+ const identicalGroup = acc [ key ] . metadata ?. identicalOps ?. find (
223
+ ( g ) =>
224
+ g . operation . from . address === op . from . address &&
225
+ g . operation . to . address === op . to . address
202
226
) || {
203
227
operation : op ,
204
228
count : 0 ,
205
229
instances : [ ] ,
206
230
} ;
207
231
identicalGroup . count += 1 ;
208
232
identicalGroup . instances . push ( op ) ;
209
- acc [ key ] . metadata ?. identicalOps ?. set ( identicalKey , identicalGroup ) ;
233
+ acc [ key ] . metadata ?. identicalOps ?. push ( identicalGroup ) ;
210
234
211
235
// Combine assets as before
212
236
for ( const asset of op . assets || [ ] ) {
@@ -232,7 +256,7 @@ function groupSimilarOperations(
232
256
metadata : {
233
257
...group . metadata ,
234
258
// Only include groups with multiple identical operations
235
- identicalOps : Array . from ( group . metadata . identicalOps . values ( ) ) . filter (
259
+ identicalOps : Array . from ( group . metadata ? .identicalOps || [ ] ) . filter (
236
260
( g ) => g . count > 1
237
261
) ,
238
262
} ,
0 commit comments