2
2
3
3
var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
4
4
5
+ function _toConsumableArray ( arr ) { if ( Array . isArray ( arr ) ) { for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) { arr2 [ i ] = arr [ i ] ; } return arr2 ; } else { return Array . from ( arr ) ; } }
6
+
5
7
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
6
8
7
9
var fs = require ( 'fs' ) ;
@@ -98,16 +100,18 @@ var InputDataDecoder = function () {
98
100
if ( obj . type === 'event' ) return acc ;
99
101
var method = obj . name || null ;
100
102
var types = obj . inputs ? obj . inputs . map ( function ( x ) {
101
- if ( x . type === 'tuple[]' ) {
103
+ if ( x . type . includes ( 'tuple' ) ) {
102
104
return x ;
103
105
} else {
104
106
return x . type ;
105
107
}
106
108
} ) : [ ] ;
107
109
108
110
var names = obj . inputs ? obj . inputs . map ( function ( x ) {
109
- if ( x . type === 'tuple[]' ) {
110
- return '' ;
111
+ if ( x . type . includes ( 'tuple' ) ) {
112
+ return [ x . name , x . components . map ( function ( a ) {
113
+ return a . name ;
114
+ } ) ] ;
111
115
} else {
112
116
return x . name ;
113
117
}
@@ -122,15 +126,44 @@ var InputDataDecoder = function () {
122
126
inputsBuf = normalizeAddresses ( types , inputsBuf ) ;
123
127
inputs = ethabi . rawDecode ( types , inputsBuf ) ;
124
128
} catch ( err ) {
125
- // TODO: normalize addresses for tuples
126
129
inputs = ethers . utils . defaultAbiCoder . decode ( types , inputsBuf ) ;
127
-
128
- inputs = inputs [ 0 ] ;
130
+ // defaultAbiCoder attaches some unwanted properties to the list object
131
+ inputs = deepRemoveUnwantedArrayProperties ( inputs ) ;
132
+
133
+ // TODO: do this normalization into normalizeAddresses
134
+ inputs = inputs . map ( function ( input , i ) {
135
+ if ( types [ i ] . components ) {
136
+ var tupleTypes = types [ i ] . components ;
137
+ return deepStripTupleAddresses ( input , tupleTypes ) ;
138
+ }
139
+ if ( types [ i ] === 'address' ) {
140
+ return input . split ( '0x' ) [ 1 ] ;
141
+ }
142
+ if ( types [ i ] === 'address[]' ) {
143
+ return input . map ( function ( address ) {
144
+ return address . split ( '0x' ) [ 1 ] ;
145
+ } ) ;
146
+ }
147
+ return input ;
148
+ } ) ;
129
149
}
130
150
151
+ // Map any tuple types into arrays
152
+ var typesToReturn = types . map ( function ( t ) {
153
+ if ( t . components ) {
154
+ var arr = t . components . reduce ( function ( acc , cur ) {
155
+ return [ ] . concat ( _toConsumableArray ( acc ) , [ cur . type ] ) ;
156
+ } , [ ] ) ;
157
+ var tupleStr = '(' + arr . join ( ',' ) + ')' ;
158
+ if ( t . type === 'tuple[]' ) return tupleStr + '[]' ;
159
+ return tupleStr ;
160
+ }
161
+ return t ;
162
+ } ) ;
163
+
131
164
return {
132
165
method : method ,
133
- types : types ,
166
+ types : typesToReturn ,
134
167
inputs : inputs ,
135
168
names : names
136
169
} ;
@@ -155,6 +188,33 @@ var InputDataDecoder = function () {
155
188
return InputDataDecoder ;
156
189
} ( ) ;
157
190
191
+ // remove 0x from addresses
192
+
193
+
194
+ function deepStripTupleAddresses ( input , tupleTypes ) {
195
+ return input . map ( function ( item , i ) {
196
+ var type = tupleTypes [ i ] . type ;
197
+ if ( type === 'address' ) {
198
+ console . log ( item ) ;
199
+ return item . split ( '0x' ) [ 1 ] ;
200
+ }
201
+ if ( type === 'address[]' ) {
202
+ return item . map ( function ( a ) {
203
+ return a . split ( '0x' ) [ 1 ] ;
204
+ } ) ;
205
+ }
206
+ return item ;
207
+ } ) ;
208
+ console . log ( { input : input , tupleTypes : tupleTypes } ) ;
209
+ }
210
+
211
+ function deepRemoveUnwantedArrayProperties ( arr ) {
212
+ return [ ] . concat ( _toConsumableArray ( arr . map ( function ( item ) {
213
+ if ( Array . isArray ( item ) ) return deepRemoveUnwantedArrayProperties ( item ) ;
214
+ return item ;
215
+ } ) ) ) ;
216
+ }
217
+
158
218
function normalizeAddresses ( types , input ) {
159
219
var offset = 0 ;
160
220
for ( var i = 0 ; i < types . length ; i ++ ) {
@@ -190,11 +250,9 @@ function isArray(type) {
190
250
return type . lastIndexOf ( ']' ) === type . length - 1 ;
191
251
}
192
252
193
- function handleInputs ( input ) {
194
- var tupleArray = false ;
253
+ function handleInputs ( input , tupleArray ) {
195
254
if ( input instanceof Object && input . components ) {
196
255
input = input . components ;
197
- tupleArray = true ;
198
256
}
199
257
200
258
if ( ! Array . isArray ( input ) ) {
@@ -219,11 +277,13 @@ function handleInputs(input) {
219
277
if ( tupleArray ) {
220
278
return ret + '[]' ;
221
279
}
280
+
281
+ return ret ;
222
282
}
223
283
224
284
function genMethodId ( methodName , types ) {
225
285
var input = methodName + '(' + types . reduce ( function ( acc , x ) {
226
- acc . push ( handleInputs ( x ) ) ;
286
+ acc . push ( handleInputs ( x , x . type === 'tuple[]' ) ) ;
227
287
return acc ;
228
288
} , [ ] ) . join ( ',' ) + ')' ;
229
289
0 commit comments