File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed
Sources/TronWebSwift/Contracts Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -107,5 +107,17 @@ public struct TronContract: TronContractProtocol {
107
107
guard case . function( _) = function else { return nil }
108
108
return function. decodeReturnData ( data)
109
109
}
110
+
111
+ public func decodeMethodData( _ data: Data ) -> ( method: String , inputs: [ String : Any ] ? ) ? {
112
+ guard data. count >= 4 else { return nil }
113
+ let methods = methods. compactMap { ( _, value) in
114
+ if case . function( let f) = value, f. methodEncoding == data. subdata ( in: 0 ..< 4 ) {
115
+ return ( f. signature, value)
116
+ }
117
+ return nil
118
+ }
119
+ guard let method = methods. first else { return nil }
120
+ return ( method. 0 , method. 1 . decodeInputData ( data) ?? [ : ] )
121
+ }
110
122
}
111
123
Original file line number Diff line number Diff line change @@ -204,15 +204,10 @@ class TronWebTests: XCTestCase {
204
204
debugPrint ( human)
205
205
}
206
206
207
- func testDecodeInputDataExample ( ) throws {
207
+ func testDecodeMethodDataExample ( ) throws {
208
208
guard let tronContract = TronContract ( TronWeb . Utils. trc20ABI, at: nil ) else { return }
209
- let method = " transfer "
210
- guard let function = tronContract. methods [ method] else { return }
211
- guard case . function( _) = function else { return }
212
- // a9059cbb
213
- // 00000000000000000000004141a768c9797b8b1a1d41d25ba603f68326fcfc40
214
- // 00000000000000000000000000000000000000000000000000000000000186a0
215
- let returns = function. decodeInputData ( Data ( hex: " a9059cbb00000000000000000000004141a768c9797b8b1a1d41d25ba603f68326fcfc4000000000000000000000000000000000000000000000000000000000000186a0 " ) )
216
- debugPrint ( returns ?? [ : ] )
209
+ let returns = tronContract. decodeMethodData ( Data ( hex: " a9059cbb00000000000000000000004141a768c9797b8b1a1d41d25ba603f68326fcfc4000000000000000000000000000000000000000000000000000000000000186a0 " ) )
210
+ XCTAssertTrue ( returns? . method == " transfer(address,uint256) " )
211
+ XCTAssertTrue ( returns? . inputs ? [ " _value " ] as? BigUInt == BigUInt ( " 100000 " ) )
217
212
}
218
213
}
You can’t perform that action at this time.
0 commit comments