diff --git a/Sources/Web3Core/EthereumABI/ABIEncoding.swift b/Sources/Web3Core/EthereumABI/ABIEncoding.swift index f62177ec8..6005214a0 100755 --- a/Sources/Web3Core/EthereumABI/ABIEncoding.swift +++ b/Sources/Web3Core/EthereumABI/ABIEncoding.swift @@ -218,19 +218,11 @@ public struct ABIEncoder { public static func encodeSingleType(type: ABI.Element.ParameterType, value: AnyObject) -> Data? { switch type { case .uint: - if let biguint = convertToBigUInt(value) { - return biguint.abiEncode(bits: 256) - } - if let bigint = convertToBigInt(value) { - return bigint.abiEncode(bits: 256) - } + let biguint = convertToBigUInt(value) + return biguint == nil ? nil : biguint!.abiEncode(bits: 256) case .int: - if let biguint = convertToBigUInt(value) { - return biguint.abiEncode(bits: 256) - } - if let bigint = convertToBigInt(value) { - return bigint.abiEncode(bits: 256) - } + let bigint = convertToBigInt(value) + return bigint == nil ? nil : bigint!.abiEncode(bits: 256) case .address: if let string = value as? String { guard let address = EthereumAddress(string) else {return nil} diff --git a/Tests/web3swiftTests/localTests/ABIEncoderTest.swift b/Tests/web3swiftTests/localTests/ABIEncoderTest.swift index 3f69fb2db..73fabe701 100644 --- a/Tests/web3swiftTests/localTests/ABIEncoderTest.swift +++ b/Tests/web3swiftTests/localTests/ABIEncoderTest.swift @@ -14,6 +14,16 @@ import BigInt class ABIEncoderTest: XCTestCase { + func testEncodeInt() { + XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: -10 as AnyObject)?.toHexString(), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6") + XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: 10 as AnyObject)?.toHexString(), "000000000000000000000000000000000000000000000000000000000000000a") + } + + func testEncodeUInt() { + XCTAssertEqual(ABIEncoder.encodeSingleType(type: .uint(bits: 32), value: -10 as AnyObject), nil) + XCTAssertEqual(ABIEncoder.encodeSingleType(type: .uint(bits: 32), value: 10 as AnyObject)?.toHexString(), "000000000000000000000000000000000000000000000000000000000000000a") + } + func testSoliditySha3() throws { var hex = try ABIEncoder.soliditySha3(true).toHexString().addHexPrefix() assert(hex == "0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2")