@@ -2,7 +2,8 @@ import { DateUtil } from '@reown/appkit-common-react-native';
2
2
import type {
3
3
TransactionTransfer ,
4
4
Transaction ,
5
- TransactionImage
5
+ TransactionImage ,
6
+ TransactionMetadata
6
7
} from '@reown/appkit-common-react-native' ;
7
8
import type { TransactionType } from './TypesUtil' ;
8
9
import { UiUtil } from './UiUtil' ;
@@ -24,20 +25,22 @@ export const TransactionUtil = {
24
25
} ,
25
26
26
27
getTransactionImages ( transfers : TransactionTransfer [ ] ) : TransactionImage [ ] {
27
- const [ transfer , secondTransfer ] = transfers ;
28
- const isAllNFT = Boolean ( transfer ) && transfers ?. every ( item => Boolean ( item . nft_info ) ) ;
28
+ const isAllNFT = Boolean ( transfers [ 0 ] ) && transfers ?. every ( item => Boolean ( item . nft_info ) ) ;
29
29
const haveMultipleTransfers = transfers ?. length > 1 ;
30
30
const haveTwoTransfers = transfers ?. length === 2 ;
31
31
32
32
if ( haveTwoTransfers && ! isAllNFT ) {
33
- return [ this . getTransactionImage ( transfer ) , this . getTransactionImage ( secondTransfer ) ] ;
33
+ const first = transfers . find ( t => t ?. direction === 'out' ) ;
34
+ const second = transfers . find ( t => t ?. direction === 'in' ) ;
35
+
36
+ return [ this . getTransactionImage ( first ) , this . getTransactionImage ( second ) ] ;
34
37
}
35
38
36
39
if ( haveMultipleTransfers ) {
37
40
return transfers . map ( item => this . getTransactionImage ( item ) ) ;
38
41
}
39
42
40
- return [ this . getTransactionImage ( transfer ) ] ;
43
+ return [ this . getTransactionImage ( transfers [ 0 ] ) ] ;
41
44
} ,
42
45
43
46
getTransactionImage ( transfer ?: TransactionTransfer ) : TransactionImage {
@@ -72,66 +75,83 @@ export const TransactionUtil = {
72
75
} ,
73
76
74
77
getTransactionDescriptions ( transaction : Transaction ) {
75
- const type = transaction ?. metadata ?. operationType as TransactionType ;
78
+ if ( ! transaction . metadata ) {
79
+ return [ 'Unknown transaction' ] ;
80
+ }
76
81
82
+ const type = transaction ?. metadata ?. operationType as TransactionType ;
77
83
const transfers = transaction ?. transfers ;
78
- const haveTransfer = transaction ?. transfers ?. length > 0 ;
79
- const haveMultipleTransfers = transaction ?. transfers ?. length > 1 ;
80
- const isSendOrReceive = type === 'send' || type === 'receive' ;
81
- const isFungible =
82
- haveTransfer && transfers ?. every ( transfer => Boolean ( transfer ?. fungible_info ) ) ;
83
- const [ firstTransfer , secondTransfer ] = transfers ;
84
-
85
- let firstDescription = this . getTransferDescription ( firstTransfer ) ;
86
- let secondDescription = this . getTransferDescription ( secondTransfer ) ;
87
-
88
- if ( ! haveTransfer ) {
89
- if ( isSendOrReceive && isFungible ) {
90
- firstDescription = UiUtil . getTruncateString ( {
91
- string : transaction ?. metadata . sentFrom ,
92
- charsStart : 4 ,
93
- charsEnd : 6 ,
94
- truncate : 'middle'
95
- } ) ;
96
- secondDescription = UiUtil . getTruncateString ( {
97
- string : transaction ?. metadata . sentTo ,
98
- charsStart : 4 ,
99
- charsEnd : 6 ,
100
- truncate : 'middle'
101
- } ) ;
102
-
103
- return [ firstDescription , secondDescription ] ;
104
- }
105
-
106
- return [ transaction . metadata . status ] ;
84
+
85
+ // Early return for trade transactions
86
+ if ( type === 'trade' ) {
87
+ return this . getTradeDescriptions ( transfers ) ;
107
88
}
108
89
109
- if ( haveMultipleTransfers ) {
110
- return transfers . map ( item => this . getTransferDescription ( item ) ) ;
90
+ // Handle multiple transfers
91
+ if ( transfers . length > 1 ) {
92
+ return transfers . map ( transfer => this . getTransferDescription ( transfer ) ) ;
111
93
}
112
94
113
- let prefix = '' ;
114
- if ( plusTypes . includes ( type ) ) {
115
- prefix = '+' ;
116
- } else if ( minusTypes . includes ( type ) ) {
117
- prefix = '-' ;
95
+ // Handle single transfer
96
+ if ( transfers . length === 1 ) {
97
+ return [ this . formatSingleTransfer ( transfers [ 0 ] ! , type , transaction . metadata ) ] ;
118
98
}
119
99
120
- firstDescription = prefix . concat ( firstDescription ) ;
100
+ return [ transaction . metadata . status ] ;
101
+ } ,
102
+
103
+ isSendReceiveTransaction ( type : TransactionType ) : boolean {
104
+ return type === 'send' || type === 'receive' ;
105
+ } ,
106
+
107
+ hasFungibleTransfers ( transfers : TransactionTransfer [ ] ) : boolean {
108
+ return transfers . every ( transfer => Boolean ( transfer ?. fungible_info ) ) ;
109
+ } ,
110
+
111
+ getSendReceiveDescriptions ( metadata : TransactionMetadata ) : string [ ] {
112
+ return [ this . truncateAddress ( metadata . sentFrom ) , this . truncateAddress ( metadata . sentTo ) ] ;
113
+ } ,
114
+
115
+ truncateAddress ( address : string ) : string {
116
+ return UiUtil . getTruncateString ( {
117
+ string : address ,
118
+ charsStart : 4 ,
119
+ charsEnd : 6 ,
120
+ truncate : 'middle'
121
+ } ) ;
122
+ } ,
121
123
122
- if ( isSendOrReceive ) {
124
+ formatSingleTransfer (
125
+ transfer : TransactionTransfer ,
126
+ type : TransactionType ,
127
+ metadata : TransactionMetadata
128
+ ) : string {
129
+ const prefix = this . getPrefix ( type ) ;
130
+ let description = prefix . concat ( this . getTransferDescription ( transfer ) ) ;
131
+
132
+ if ( this . isSendReceiveTransaction ( type ) ) {
123
133
const isSend = type === 'send' ;
124
- const address = UiUtil . getTruncateString ( {
125
- string : isSend ? transaction . metadata . sentTo : transaction . metadata . sentFrom ,
126
- charsStart : 4 ,
127
- charsEnd : 4 ,
128
- truncate : 'middle'
129
- } ) ;
134
+
135
+ const address = this . truncateAddress ( isSend ? metadata . sentTo : metadata . sentFrom ) ;
130
136
const arrow = isSend ? '→' : '←' ;
131
- firstDescription = firstDescription . concat ( ` ${ arrow } ${ address } ` ) ;
137
+ description = description . concat ( ` ${ arrow } ${ address } ` ) ;
132
138
}
133
139
134
- return [ firstDescription ] ;
140
+ return description ;
141
+ } ,
142
+
143
+ getPrefix ( type : TransactionType ) : string {
144
+ if ( plusTypes . includes ( type ) ) return '+' ;
145
+ if ( minusTypes . includes ( type ) ) return '-' ;
146
+
147
+ return '' ;
148
+ } ,
149
+
150
+ getTradeDescriptions ( transfers : TransactionTransfer [ ] ) : string [ ] {
151
+ const outTransfer = transfers . find ( transfer => transfer ?. direction === 'out' ) ;
152
+ const inTransfer = transfers . find ( transfer => transfer ?. direction === 'in' ) ;
153
+
154
+ return [ this . getTransferDescription ( outTransfer ) , this . getTransferDescription ( inTransfer ) ] ;
135
155
} ,
136
156
137
157
getTransferDescription ( transfer ?: TransactionTransfer ) {
0 commit comments