Skip to content

Commit 6806275

Browse files
committed
decode method
1 parent 04d79b6 commit 6806275

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Sources/TronWebSwift/Contracts/TronContract.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,17 @@ public struct TronContract: TronContractProtocol {
107107
guard case .function(_) = function else {return nil}
108108
return function.decodeReturnData(data)
109109
}
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+
}
110122
}
111123

Tests/TronWebSwiftTests/TronWebTests.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,10 @@ class TronWebTests: XCTestCase {
204204
debugPrint(human)
205205
}
206206

207-
func testDecodeInputDataExample() throws {
207+
func testDecodeMethodDataExample() throws {
208208
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"))
217212
}
218213
}

0 commit comments

Comments
 (0)