@@ -17,8 +17,8 @@ class EthereumContractTest: LocalTestCase {
17
17
private static let overloadedFunctionsABI = " [{ \" inputs \" :[], \" stateMutability \" : \" nonpayable \" , \" type \" : \" constructor \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" uint256 \" , \" name \" : \" operation \" , \" type \" : \" uint256 \" },{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" contractAddress \" , \" type \" : \" address \" },{ \" indexed \" :true, \" internalType \" : \" uint256 \" , \" name \" : \" value \" , \" type \" : \" uint256 \" }], \" name \" : \" ContractCreated \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" bytes32 \" , \" name \" : \" dataKey \" , \" type \" : \" bytes32 \" }], \" name \" : \" DataChanged \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" uint256 \" , \" name \" : \" operation \" , \" type \" : \" uint256 \" },{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" to \" , \" type \" : \" address \" },{ \" indexed \" :true, \" internalType \" : \" uint256 \" , \" name \" : \" value \" , \" type \" : \" uint256 \" },{ \" indexed \" :false, \" internalType \" : \" bytes4 \" , \" name \" : \" selector \" , \" type \" : \" bytes4 \" }], \" name \" : \" Executed \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" previousOwner \" , \" type \" : \" address \" },{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" newOwner \" , \" type \" : \" address \" }], \" name \" : \" OwnershipTransferred \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" from \" , \" type \" : \" address \" },{ \" indexed \" :false, \" internalType \" : \" uint256 \" , \" name \" : \" value \" , \" type \" : \" uint256 \" },{ \" indexed \" :true, \" internalType \" : \" bytes32 \" , \" name \" : \" typeId \" , \" type \" : \" bytes32 \" },{ \" indexed \" :true, \" internalType \" : \" bytes \" , \" name \" : \" returnedValue \" , \" type \" : \" bytes \" },{ \" indexed \" :false, \" internalType \" : \" bytes \" , \" name \" : \" receivedData \" , \" type \" : \" bytes \" }], \" name \" : \" UniversalReceiver \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" internalType \" : \" address \" , \" name \" : \" sender \" , \" type \" : \" address \" },{ \" indexed \" :true, \" internalType \" : \" uint256 \" , \" name \" : \" value \" , \" type \" : \" uint256 \" }], \" name \" : \" ValueReceived \" , \" type \" : \" event \" },{ \" stateMutability \" : \" payable \" , \" type \" : \" fallback \" },{ \" inputs \" :[{ \" internalType \" : \" bytes32[] \" , \" name \" : \" dataKeys \" , \" type \" : \" bytes32[] \" }], \" name \" : \" getData \" , \" outputs \" :[{ \" internalType \" : \" bytes[] \" , \" name \" : \" dataValues \" , \" type \" : \" bytes[] \" }], \" stateMutability \" : \" view \" , \" type \" : \" function \" },{ \" inputs \" :[{ \" internalType \" : \" bytes32 \" , \" name \" : \" dataKey \" , \" type \" : \" bytes32 \" }], \" name \" : \" getData \" , \" outputs \" :[{ \" internalType \" : \" bytes \" , \" name \" : \" dataValue \" , \" type \" : \" bytes \" }], \" stateMutability \" : \" view \" , \" type \" : \" function \" },{ \" inputs \" :[], \" name \" : \" noInputFunction \" , \" outputs \" :[], \" stateMutability \" : \" nonpayable \" , \" type \" : \" function \" },{ \" inputs \" :[{ \" internalType \" : \" bytes32[] \" , \" name \" : \" dataKeys \" , \" type \" : \" bytes32[] \" },{ \" internalType \" : \" bytes[] \" , \" name \" : \" dataValues \" , \" type \" : \" bytes[] \" }], \" name \" : \" setData \" , \" outputs \" :[], \" stateMutability \" : \" nonpayable \" , \" type \" : \" function \" },{ \" inputs \" :[{ \" internalType \" : \" bytes32 \" , \" name \" : \" dataKey \" , \" type \" : \" bytes32 \" },{ \" internalType \" : \" bytes \" , \" name \" : \" dataValue \" , \" type \" : \" bytes \" }], \" name \" : \" setData \" , \" outputs \" :[], \" stateMutability \" : \" nonpayable \" , \" type \" : \" function \" }] "
18
18
19
19
func test_decodeEvents( ) async throws {
20
- let web3 = try ! await Web3 . new ( LocalTestCase . url)
21
- let contract = web3. contract ( EthereumContractTest . eventsOnlyABI) !
20
+ let web3 = try await Web3 . new ( LocalTestCase . url)
21
+ let contract = try XCTUnwrap ( web3. contract ( EthereumContractTest . eventsOnlyABI) )
22
22
23
23
XCTAssertTrue ( contract. contract. allEvents. count == 6 )
24
24
XCTAssertNotNil ( contract. contract. events [ " ContractCreated " ] )
@@ -30,8 +30,8 @@ class EthereumContractTest: LocalTestCase {
30
30
}
31
31
32
32
func test_decodedAllFunctions( ) async throws {
33
- let web3 = try ! await Web3 . new ( LocalTestCase . url)
34
- let contract = web3. contract ( EthereumContractTest . overloadedFunctionsABI) !
33
+ let web3 = try await Web3 . new ( LocalTestCase . url)
34
+ let contract = try XCTUnwrap ( web3. contract ( EthereumContractTest . overloadedFunctionsABI) )
35
35
36
36
XCTAssertTrue ( contract. contract. allMethods. count == 5 )
37
37
@@ -53,8 +53,8 @@ class EthereumContractTest: LocalTestCase {
53
53
}
54
54
55
55
func test_encodeMethodBasedOnNameWithParameters_mustFail( ) async throws {
56
- let web3 = try ! await Web3 . new ( LocalTestCase . url)
57
- let contract = web3. contract ( EthereumContractTest . overloadedFunctionsABI) !
56
+ let web3 = try await Web3 . new ( LocalTestCase . url)
57
+ let contract = try XCTUnwrap ( web3. contract ( EthereumContractTest . overloadedFunctionsABI) )
58
58
59
59
/// Encoding method that expects parameters but we are not giving any.
60
60
/// Result must be `nil`.
@@ -63,43 +63,41 @@ class EthereumContractTest: LocalTestCase {
63
63
}
64
64
65
65
func test_encodeMethodBasedOnNameWithParameters( ) async throws {
66
- let web3 = try ! await Web3 . new ( LocalTestCase . url)
67
- let contract = web3. contract ( EthereumContractTest . overloadedFunctionsABI,
68
- at: EthereumAddress ( " 0x6394b37Cf80A7358b38068f0CA4760ad49983a1B " ) !) !
66
+ let web3 = try await Web3 . new ( LocalTestCase . url)
67
+ let contract = try XCTUnwrap ( web3. contract ( EthereumContractTest . overloadedFunctionsABI, at: EthereumAddress ( " 0x6394b37Cf80A7358b38068f0CA4760ad49983a1B " ) ) )
69
68
let parameters : [ AnyObject ] = [
70
- [ Data . randomBytes ( length: 32 ) !,
71
- Data . randomBytes ( length: 32 ) !] ,
72
- [ Data . randomBytes ( length: 32 ) !,
73
- Data . randomBytes ( length: 32 ) !]
69
+ [ Data . randomBytes ( length: 32 ) , Data . randomBytes ( length: 32 ) ] ,
70
+ [ Data . randomBytes ( length: 32 ) , Data . randomBytes ( length: 32 ) ]
74
71
] as [ AnyObject ]
75
72
let functionNameWithParameters = " setData(bytes32[],bytes[]) "
76
73
let transaction = contract. createWriteOperation ( functionNameWithParameters, parameters: parameters)
77
74
XCTAssertNotNil ( transaction)
78
75
79
- func testDecoding( _ method: String ) {
80
- let decodedData = contract. contract. methods [ method] ? . first? . decodeInputData ( transaction!. transaction. data)
76
+ func testDecoding( _ method: String ) throws {
77
+ let data = try XCTUnwrap ( transaction? . transaction. data)
78
+ let decodedData = try XCTUnwrap ( contract. contract. methods [ method] ? . first? . decodeInputData ( data) )
81
79
XCTAssertNotNil ( decodedData)
82
- XCTAssertEqual ( decodedData! [ " 0 " ] as! [ Data ] , parameters [ 0 ] as! [ Data ] )
83
- XCTAssertEqual ( decodedData! [ " 1 " ] as! [ Data ] , parameters [ 1 ] as! [ Data ] )
80
+ XCTAssertEqual ( try XCTUnwrap ( decodedData [ " 0 " ] as? [ Data ] ) , try XCTUnwrap ( parameters [ 0 ] as? [ Data ] ) )
81
+ XCTAssertEqual ( try XCTUnwrap ( decodedData [ " 1 " ] as? [ Data ] ) , try XCTUnwrap ( parameters [ 1 ] as? [ Data ] ) )
84
82
}
85
83
86
- testDecoding ( functionNameWithParameters)
87
- testDecoding ( getFuncSignature ( functionNameWithParameters) )
84
+ try testDecoding ( functionNameWithParameters)
85
+ try testDecoding ( getFuncSignature ( functionNameWithParameters) )
88
86
}
89
87
90
88
func test_encodeMethodBasedOnHexSignature( ) async throws {
91
- let web3 = try ! await Web3 . new ( LocalTestCase . url)
92
- let contract = web3. contract ( EthereumContractTest . overloadedFunctionsABI,
93
- at: EthereumAddress ( " 0x6394b37Cf80A7358b38068f0CA4760ad49983a1B " ) !) !
94
- let parameters : [ AnyObject ] = [ Data . randomBytes ( length: 32 ) !, Data . randomBytes ( length: 32 ) !] as [ AnyObject ]
89
+ let web3 = try await Web3 . new ( LocalTestCase . url)
90
+ let contract = try XCTUnwrap ( web3. contract ( EthereumContractTest . overloadedFunctionsABI, at: EthereumAddress ( " 0x6394b37Cf80A7358b38068f0CA4760ad49983a1B " ) ) )
91
+ let parameters : [ AnyObject ] = [ Data . randomBytes ( length: 32 ) , Data . randomBytes ( length: 32 ) ] as [ AnyObject ]
95
92
let functionSignature = getFuncSignature ( " setData(bytes32,bytes) " )
96
93
let transaction = contract. createWriteOperation ( functionSignature, parameters: parameters)
97
94
XCTAssertNotNil ( transaction)
98
95
99
- let decodedData = contract. contract. methods [ functionSignature] ? . first? . decodeInputData ( transaction!. transaction. data)
96
+ let data = try XCTUnwrap ( transaction? . transaction. data)
97
+ let decodedData = try XCTUnwrap ( contract. contract. methods [ functionSignature] ? . first? . decodeInputData ( data) )
100
98
XCTAssertNotNil ( decodedData)
101
- XCTAssertEqual ( decodedData! [ " 0 " ] as! Data , parameters [ 0 ] as! Data )
102
- XCTAssertEqual ( decodedData! [ " 1 " ] as! Data , parameters [ 1 ] as! Data )
99
+ XCTAssertEqual ( try XCTUnwrap ( decodedData [ " 0 " ] as? Data ) , try XCTUnwrap ( parameters [ 0 ] as? Data ) )
100
+ XCTAssertEqual ( try XCTUnwrap ( decodedData [ " 1 " ] as? Data ) , try XCTUnwrap ( parameters [ 1 ] as? Data ) )
103
101
}
104
102
}
105
103
0 commit comments