File tree Expand file tree Collapse file tree 4 files changed +33
-2
lines changed
web3swift/Contract/Classes Expand file tree Collapse file tree 4 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import BigInt
11
11
12
12
@available ( * , deprecated)
13
13
public struct Contract : ContractProtocol {
14
+
14
15
public var allEvents : [ String ] {
15
16
return events. keys. flatMap ( { ( s) -> String in
16
17
return s
@@ -210,4 +211,8 @@ public struct Contract:ContractProtocol {
210
211
public func decodeInputData( _ method: String , data: Data ) -> [ String : Any ] ? {
211
212
return nil
212
213
}
214
+
215
+ public func decodeInputData( _ data: Data ) -> [ String : Any ] ? {
216
+ return nil
217
+ }
213
218
}
Original file line number Diff line number Diff line change @@ -232,7 +232,7 @@ public struct ContractV2:ContractProtocol {
232
232
233
233
public func decodeInputData( _ method: String , data: Data ) -> [ String : Any ] ? {
234
234
if method == " fallback " {
235
- return [ String : Any ] ( )
235
+ return nil
236
236
}
237
237
guard let function = methods [ method] else { return nil }
238
238
switch function {
@@ -241,7 +241,25 @@ public struct ContractV2:ContractProtocol {
241
241
case . constructor( _) :
242
242
return function. decodeInputData ( data)
243
243
default :
244
- return [ String: Any] ( )
244
+ return nil
245
+ }
246
+ }
247
+
248
+ public func decodeInputData( _ data: Data ) -> [ String : Any ] ? {
249
+ guard data. count % 32 == 4 else { return nil }
250
+ let methodSignature = data [ 0 ..< 4 ]
251
+ let foundFunction = self . _abi. filter { ( m) -> Bool in
252
+ switch m {
253
+ case . function( let function) :
254
+ return function. methodEncoding == methodSignature
255
+ default :
256
+ return false
257
+ }
258
+ }
259
+ guard foundFunction. count == 1 else {
260
+ return nil
245
261
}
262
+ let function = foundFunction [ 0 ]
263
+ return function. decodeInputData ( Data ( data [ 4 ..< data. count] ) )
246
264
}
247
265
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ public protocol ContractProtocol {
19
19
init ? ( _ abiString: String , at: EthereumAddress ? )
20
20
func decodeReturnData( _ method: String , data: Data ) -> [ String : Any ] ?
21
21
func decodeInputData( _ method: String , data: Data ) -> [ String : Any ] ?
22
+ func decodeInputData( _ data: Data ) -> [ String : Any ] ?
22
23
func parseEvent( _ eventLog: EventLog ) -> ( eventName: String ? , eventData: [ String : Any ] ? )
23
24
func testBloomForEventPrecence( eventName: String , bloom: EthereumBloomFilter ) -> Bool ?
24
25
// func allEvents() -> [String: [String: Any]?]
Original file line number Diff line number Diff line change @@ -2148,6 +2148,13 @@ class web3swiftTests: XCTestCase {
2148
2148
XCTAssert ( decoded![ " _to " ] as? EthereumAddress == EthereumAddress ( " 0xcdd45864e794fe5e3e1b0045b77e62f4c43b8bd9 " ) )
2149
2149
}
2150
2150
2151
+ func testDecodeInputDataWithoutMethodName( ) {
2152
+ let contract = ContractV2 . init ( Web3 . Utils. erc20ABI) !
2153
+ let dataToDecode = Data . fromHex ( " 0xa9059cbb000000000000000000000000cdd45864e794fe5e3e1b0045b77e62f4c43b8bd9000000000000000000000000000000000000000000000224b5f018c3e30142d5 " ) !
2154
+ let decoded = contract. decodeInputData ( dataToDecode)
2155
+ XCTAssert ( decoded![ " _to " ] as? EthereumAddress == EthereumAddress ( " 0xcdd45864e794fe5e3e1b0045b77e62f4c43b8bd9 " ) )
2156
+ }
2157
+
2151
2158
func testPerformanceExample( ) {
2152
2159
// This is an example of a performance test case.
2153
2160
self . measure {
You can’t perform that action at this time.
0 commit comments