@@ -32,7 +32,7 @@ function deepCopy(target) {
32
32
const newTarget = _isArray ( target ) ? [ ] : { } ;
33
33
34
34
Object . keys ( target ) . forEach ( key =>
35
- newTarget [ key ] = target [ key ] instanceof Object ? deepCopy ( target [ key ] ) : target [ key ]
35
+ newTarget [ key ] = target [ key ] instanceof Object && ! target [ key ] . _isBigNumber ? deepCopy ( target [ key ] ) : target [ key ]
36
36
) ;
37
37
38
38
return newTarget ;
@@ -83,110 +83,16 @@ export function encodeParams(types, values) {
83
83
return abiCoder . encode ( types , values ) ;
84
84
}
85
85
86
- export function encodeParamsV2 ( parameters ) {
87
- const formatParams = ( _params ) => {
88
- const types = [ ] ;
89
- const values = [ ] ;
90
- const getBaseType = ( type ) => {
91
- if ( ! type || ! _isString ( type ) || ! type . length )
92
- throw new Error ( "Invalid parameter type provided: " + type ) ;
93
-
94
- return type . match ( / ^ ( [ ^ \x5b ] * ) ( \x5b | $ ) / ) [ 0 ] ;
95
- }
96
-
97
- const getTypes = ( _obj ) => {
98
- if ( _isArray ( _obj ) ) {
99
- return _obj . map ( function ( _ ) {
100
- return getTypes ( _ ) ;
101
- } ) ;
102
- }
103
-
104
- let { type, value } = _obj ;
105
- const baseType = getBaseType ( type ) ;
106
- switch ( baseType ) {
107
- case "trcToken" :
108
- case "trcToken[" :
109
- type = type . replace ( / t r c T o k e n / , "uint256" ) ;
110
- break ;
111
- case "tuple" :
112
- type = `tuple(${ getTypes ( value ) . join ( "," ) } )` ;
113
- break ;
114
- case "tuple[" :
115
- type = `tuple(${ getTypes ( value [ 0 ] ) . join (
116
- ","
117
- ) } )${ type . replace ( / t u p l e / , "" ) } `;
118
- break ;
119
- default :
120
- break ;
121
- }
122
-
123
- return type ;
124
- }
125
-
126
- const getValues = ( _obj ) => {
127
- if ( _isArray ( _obj ) ) {
128
- return _obj . map ( function ( _ ) {
129
- return getValues ( _ ) ;
130
- } ) ;
131
- }
132
-
133
- const getHexAddress = ( _addr ) => {
134
- if ( _isArray ( _addr ) ) {
135
- return _addr . map ( ( _ ) => {
136
- return getHexAddress ( _ ) ;
137
- } ) ;
138
- }
139
-
140
- return TronWeb . address . toHex ( _addr ) . replace ( ADDRESS_PREFIX_REGEX , "0x" ) ;
141
- }
142
-
143
- const getTupleValue = ( _value ) => {
144
- if ( _isArray ( _value ) ) {
145
- return _value . map ( ( _ ) => {
146
- return getTupleValue ( _ ) ;
147
- } ) ;
148
- }
149
-
150
- return getValues ( _value ) ;
151
- }
152
-
153
- let { type, value } = _obj ;
154
-
155
- const baseType = getBaseType ( type ) ;
156
- switch ( baseType ) {
157
- case "address" :
158
- case "address[" :
159
- value = getHexAddress ( value ) ;
160
- break ;
161
- case "tuple" :
162
- case "tuple[" :
163
- value = getTupleValue ( value ) ;
164
- break ;
165
- default :
166
- break ;
167
- }
168
-
169
- return value ;
170
- }
171
-
172
- _params . forEach ( ( _ ) => {
173
- types . push ( getTypes ( _ ) ) ;
174
- values . push ( getValues ( _ ) ) ;
175
- } ) ;
176
-
177
- return { types, values } ;
178
- }
179
-
180
- const { types, values } = formatParams ( parameters ) ;
181
- console . log ( JSON . stringify ( types ) , JSON . stringify ( values ) , abiCoder . encode ( types , values ) . replace ( / ^ ( 0 x ) / , "" ) , 'result' )
182
- return abiCoder . encode ( types , values ) . replace ( / ^ ( 0 x ) / , "" ) ;
183
- }
184
-
185
86
function extractSize ( type ) {
186
87
const size = type . match ( / ( [ a - z A - Z 0 - 9 ] ) ( \[ .* \] ) / ) ;
187
88
return size ? size [ 2 ] : '' ;
188
89
}
189
90
91
+ function extractSizeDimensions ( type ) {
92
+ const size = extractSize ( type )
93
+ return ( size . match ( / \] \[ / g) || [ ] ) . length + 1 ;
94
+ }
95
+
190
96
export function encodeParamsV2ByABI ( funABI , args ) {
191
97
const types = [ ] ;
192
98
@@ -210,6 +116,33 @@ export function encodeParamsV2ByABI(funABI, args) {
210
116
}
211
117
}
212
118
119
+ const convertAddresses = addrArr => {
120
+ if ( Array . isArray ( addrArr ) ) {
121
+ addrArr . forEach ( ( addrs , i ) => {
122
+ addrArr [ i ] = convertAddresses ( addrs ) ;
123
+ } ) ;
124
+ return addrArr ;
125
+ } else {
126
+ return _addressToHex ( addrArr )
127
+ }
128
+ } ;
129
+
130
+ const mapTuple = ( components , args , dimension ) => {
131
+ if ( dimension > 1 ) {
132
+ if ( args . length ) {
133
+ args . forEach ( arg => {
134
+ mapTuple ( components , arg , dimension - 1 ) ;
135
+ } ) ;
136
+ }
137
+ } else {
138
+ if ( args . length && dimension ) {
139
+ args . forEach ( arg => {
140
+ encodeArgs ( components , arg ) ;
141
+ } ) ;
142
+ }
143
+ }
144
+ } ;
145
+
213
146
const encodeArgs = ( inputs = [ ] , args ) => {
214
147
if ( inputs . length )
215
148
inputs . forEach ( ( input , i ) => {
@@ -218,13 +151,11 @@ export function encodeParamsV2ByABI(funABI, args) {
218
151
if ( args [ i ] )
219
152
if ( type === 'address' ) args [ i ] = _addressToHex ( args [ i ] ) ;
220
153
else if ( type . match ( / ^ ( [ ^ \x5b ] * ) ( \x5b | $ ) / ) [ 0 ] === 'address[' )
221
- args [ i ] = args [ i ] . map ( v => _addressToHex ( v ) ) ;
154
+ convertAddresses ( args [ i ] )
222
155
else if ( type . indexOf ( 'tuple' ) === 0 )
223
156
if ( extractSize ( type ) ) {
224
- if ( args [ i ] . length )
225
- args [ i ] . forEach ( arg => {
226
- encodeArgs ( input . components , arg ) ;
227
- } ) ;
157
+ const dimension = extractSizeDimensions ( type ) ;
158
+ mapTuple ( input . components , args [ i ] , dimension ) ;
228
159
} else encodeArgs ( input . components , args [ i ] ) ;
229
160
} ) ;
230
161
} ;
@@ -250,7 +181,6 @@ export function encodeParamsV2ByABI(funABI, args) {
250
181
return abiCoder . encode ( types , args ) ;
251
182
}
252
183
253
-
254
184
export function decodeParamsV2ByABI ( funABI , data ) {
255
185
const convertTypeNames = ( types ) => {
256
186
for ( let i = 0 ; i < types . length ; i ++ ) {
@@ -260,6 +190,33 @@ export function decodeParamsV2ByABI(funABI, data) {
260
190
}
261
191
}
262
192
193
+ const convertAddresses = addrArr => {
194
+ if ( Array . isArray ( addrArr ) ) {
195
+ addrArr . forEach ( ( addrs , i ) => {
196
+ addrArr [ i ] = convertAddresses ( addrs ) ;
197
+ } ) ;
198
+ return addrArr ;
199
+ } else {
200
+ return TronWeb . address . toHex ( addrArr )
201
+ }
202
+ } ;
203
+
204
+ const mapTuple = ( components , args , dimension ) => {
205
+ if ( dimension > 1 ) {
206
+ if ( args . length ) {
207
+ args . forEach ( arg => {
208
+ mapTuple ( components , arg , dimension - 1 ) ;
209
+ } ) ;
210
+ }
211
+ } else {
212
+ if ( args . length && dimension ) {
213
+ args . forEach ( arg => {
214
+ decodeResult ( components , arg ) ;
215
+ } ) ;
216
+ }
217
+ }
218
+ } ;
219
+
263
220
const buildFullTypeNameDefinition = ( typeDef ) => {
264
221
if ( typeDef && typeDef . type . indexOf ( 'tuple' ) === 0 && typeDef . components ) {
265
222
const innerTypes = typeDef . components . map ( ( innerType ) => { return buildFullTypeNameDefinition ( innerType ) } ) ;
@@ -283,15 +240,13 @@ export function decodeParamsV2ByABI(funABI, data) {
283
240
if ( name ) result [ name ] = TronWeb . address . toHex ( result [ name ] ) ;
284
241
}
285
242
else if ( type . match ( / ^ ( [ ^ \x5b ] * ) ( \x5b | $ ) / ) [ 0 ] === 'address[' ) {
286
- result [ i ] = result [ i ] . map ( v => TronWeb . address . toHex ( v ) ) ;
287
- if ( name ) result [ name ] = result [ name ] . map ( v => TronWeb . address . toHex ( v ) ) ;
243
+ convertAddresses ( result [ i ] )
244
+ if ( name ) convertAddresses ( result [ name ] )
288
245
}
289
246
else if ( type . indexOf ( 'tuple' ) === 0 )
290
247
if ( extractSize ( type ) ) {
291
- if ( result [ i ] . length )
292
- result [ i ] . forEach ( res => {
293
- decodeResult ( output . components , res ) ;
294
- } ) ;
248
+ const dimension = extractSizeDimensions ( type ) ;
249
+ mapTuple ( output . components , result [ i ] , dimension ) ;
295
250
} else decodeResult ( output . components , result [ i ] ) ;
296
251
} ) ;
297
252
} ;
0 commit comments