From a6b37c30fb27f757956fc714ceea9d35dd61e986 Mon Sep 17 00:00:00 2001 From: Oguz Yuksel Date: Sun, 10 Jul 2022 16:52:25 +0300 Subject: [PATCH] conform EthereumAdress to ExpressibleByStringLiteral --- README.md | 2 +- web3sTests/Account/EthereumAccountTests.swift | 4 +- web3sTests/Client/EthereumClientTests.swift | 42 +++++++++---------- web3sTests/Contract/ABIDecoderTests.swift | 16 +++---- .../Contract/ABIFunctionEncoderTests.swift | 16 +++---- web3sTests/Contract/ABIFunctionTests.swift | 4 +- web3sTests/ENS/ENSTests.swift | 20 ++++----- web3sTests/ERC20/ERC20Tests.swift | 10 ++--- .../OffchainLookup/OffchainLookupTests.swift | 14 +++---- web3sTests/Transaction/TransactionTests.swift | 14 +++---- .../src/Client/Models/EthereumAddress.swift | 9 +++- web3swift/src/ENS/ENSContracts.swift | 4 +- .../src/Multicall/MulticallContract.swift | 6 +-- 13 files changed, 84 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index f346f010..db20a959 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ public struct Transfer: ABIFunction { This function can be used to generate contract call transactions to send with the client: ```swift -let function = transfer(contract: EthereumAddress("0xtokenaddress"), from: EthereumAddress("0xfrom"), to: EthereumAddress("0xto"), value: 100) +let function = transfer(contract: "0xtokenaddress", from: "0xfrom", to: "0xto", value: 100) let transaction = try function.transaction() client.eth_sendRawTransaction(transacton, withAccount: account) { (error, txHash) in diff --git a/web3sTests/Account/EthereumAccountTests.swift b/web3sTests/Account/EthereumAccountTests.swift index b2ea9129..e9a6d146 100644 --- a/web3sTests/Account/EthereumAccountTests.swift +++ b/web3sTests/Account/EthereumAccountTests.swift @@ -76,8 +76,8 @@ class EthereumAccountTests: XCTestCase { } func test_toChecksumAddress() { - let add1 = EthereumAddress("0x12ae66cdc592e10b60f9097a7b0d3c59fce29876") - let add2 = EthereumAddress("0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1") + let add1: EthereumAddress = "0x12ae66cdc592e10b60f9097a7b0d3c59fce29876" + let add2: EthereumAddress = "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1" XCTAssertEqual(add1.toChecksumAddress(), "0x12AE66CDc592e10B60f9097a7b0D3C59fce29876") XCTAssertEqual(add2.toChecksumAddress(), "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1") } diff --git a/web3sTests/Client/EthereumClientTests.swift b/web3sTests/Client/EthereumClientTests.swift index 60f76d6e..b0f9f52c 100644 --- a/web3sTests/Client/EthereumClientTests.swift +++ b/web3sTests/Client/EthereumClientTests.swift @@ -69,7 +69,7 @@ class EthereumClientTests: XCTestCase { func testEthGetBalanceIncorrectAddress() async { do { - _ = try await client?.eth_getBalance(address: EthereumAddress("0xnig42niog2"), block: .Latest) + _ = try await client?.eth_getBalance(address: "0xnig42niog2", block: .Latest) XCTFail("Expected to throw while awaiting, but succeeded") } catch { XCTAssertEqual(error as? EthereumClientError, .executionError( @@ -107,7 +107,7 @@ class EthereumClientTests: XCTestCase { func testEthGetCode() async { do { - let code = try await client?.eth_getCode(address: EthereumAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010"), block: .Latest) + let code = try await client?.eth_getCode(address: "0x112234455c3a32fd11230c42e7bccd4a84e02010", block: .Latest) XCTAssertNotNil(code, "Contract code not available") } catch { XCTFail("Expected code but failed \(error).") @@ -116,7 +116,7 @@ class EthereumClientTests: XCTestCase { func testEthSendRawTransaction() async { do { - let tx = EthereumTransaction(from: nil, to: EthereumAddress("0x3c1bd6b420448cf16a389c8b0115ccb3660bb854"), value: BigUInt(1600000), data: nil, nonce: 2, gasPrice: BigUInt(4000000), gasLimit: BigUInt(500000), chainId: EthereumNetwork.ropsten.intValue) + let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1600000), data: nil, nonce: 2, gasPrice: BigUInt(4000000), gasLimit: BigUInt(500000), chainId: EthereumNetwork.ropsten.intValue) let txHash = try await client?.eth_sendRawTransaction(tx, withAccount: account!) XCTAssertNotNil(txHash, "No tx hash, ensure key is valid in TestConfig.swift") @@ -137,7 +137,7 @@ class EthereumClientTests: XCTestCase { func testEthCall() async { do { - let tx = EthereumTransaction(from: nil, to: EthereumAddress("0x3c1bd6b420448cf16a389c8b0115ccb3660bb854"), value: BigUInt(1800000), data: nil, nonce: 2, gasPrice: BigUInt(400000), gasLimit: BigUInt(50000), chainId: EthereumNetwork.ropsten.intValue) + let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1800000), data: nil, nonce: 2, gasPrice: BigUInt(400000), gasLimit: BigUInt(50000), chainId: EthereumNetwork.ropsten.intValue) let txHash = try await client?.eth_call(tx, block: .Latest) XCTAssertNotNil(txHash, "Transaction hash not available") } catch { @@ -147,7 +147,7 @@ class EthereumClientTests: XCTestCase { func testSimpleEthGetLogs() async { do { - let logs = try await client?.eth_getLogs(addresses: [EthereumAddress("0x23d0a442580c01e420270fba6ca836a8b2353acb")], topics: nil, fromBlock: .Earliest, toBlock: .Latest) + let logs = try await client?.eth_getLogs(addresses: ["0x23d0a442580c01e420270fba6ca836a8b2353acb"], topics: nil, fromBlock: .Earliest, toBlock: .Latest) XCTAssertNotNil(logs, "Logs not available") } catch { XCTFail("Expected logs but failed \(error).") @@ -268,7 +268,7 @@ class EthereumClientTests: XCTestCase { do { let to = try! ABIEncoder.encodeRaw("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854", forType: ABIRawType.FixedAddress) let filters = [ - EventFilter(type: ERC20Events.Transfer.self, allowedSenders: [EthereumAddress("0xdb0040451f373949a4be60dcd7b6b8d6e42658b6")]) + EventFilter(type: ERC20Events.Transfer.self, allowedSenders: ["0xdb0040451f373949a4be60dcd7b6b8d6e42658b6"]) ] let eventsResult = try await client?.getEvents(addresses: nil, @@ -287,8 +287,8 @@ class EthereumClientTests: XCTestCase { do { let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854")) let filters = [ - EventFilter(type: ERC20Events.Transfer.self, allowedSenders: [EthereumAddress("0xdb0040451f373949a4be60dcd7b6b8d6e42658b6")]), - EventFilter(type: TransferMatchingSignatureEvent.self, allowedSenders: [EthereumAddress("0xdb0040451f373949a4be60dcd7b6b8d6e42658b6")]) + EventFilter(type: ERC20Events.Transfer.self, allowedSenders: ["0xdb0040451f373949a4be60dcd7b6b8d6e42658b6"]), + EventFilter(type: TransferMatchingSignatureEvent.self, allowedSenders: ["0xdb0040451f373949a4be60dcd7b6b8d6e42658b6"]) ] let eventsResult = try await client?.getEvents(addresses: nil, @@ -305,10 +305,10 @@ class EthereumClientTests: XCTestCase { func test_GivenDynamicArrayResponse_ThenCallReceivesData() async { do { - let function = GetGuardians(wallet: EthereumAddress("0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd")) + let function = GetGuardians(wallet: "0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd") let response = try await function.call(withClient: client!, responseType: GetGuardians.Response.self) - XCTAssertEqual(response.guardians, [EthereumAddress("0x44fe11c90d2bcbc8267a0e56d55235ddc2b96c4f")]) + XCTAssertEqual(response.guardians, ["0x44fe11c90d2bcbc8267a0e56d55235ddc2b96c4f"]) } catch { XCTFail("Expected response but failed \(error).") } @@ -333,9 +333,9 @@ class EthereumClientTests: XCTestCase { func test_GivenValidTransaction_ThenEstimatesGas() async { do { - let function = TransferToken(wallet: EthereumAddress("0xD18dE36e6FB4a5A069f673723Fab71cc00C6CE5F"), - token: EthereumAddress("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"), - to: EthereumAddress("0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd"), + let function = TransferToken(wallet: "0xD18dE36e6FB4a5A069f673723Fab71cc00C6CE5F", + token: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", + to: "0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd", amount: 1, data: Data(), gasPrice: nil, @@ -351,8 +351,8 @@ class EthereumClientTests: XCTestCase { struct GetGuardians: ABIFunction { static let name = "getGuardians" - let contract = EthereumAddress("0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90") - let from: EthereumAddress? = EthereumAddress("0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90") + let contract: EthereumAddress = "0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90" + let from: EthereumAddress? = "0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90" let gasPrice: BigUInt? = nil let gasLimit: BigUInt? = nil @@ -376,8 +376,8 @@ struct GetGuardians: ABIFunction { struct TransferToken: ABIFunction { static let name = "transferToken" - let contract = EthereumAddress("0xe4f5384d96cc4e6929b63546082788906250b60b") - let from: EthereumAddress? = EthereumAddress("0xe4f5384d96cc4e6929b63546082788906250b60b") + let contract: EthereumAddress = "0xe4f5384d96cc4e6929b63546082788906250b60b" + let from: EthereumAddress? = "0xe4f5384d96cc4e6929b63546082788906250b60b" let wallet: EthereumAddress let token: EthereumAddress @@ -399,8 +399,8 @@ struct TransferToken: ABIFunction { struct InvalidMethodA: ABIFunction { static let name = "invalidMethodCallBoolResponse" - let contract = EthereumAddress("0xed0439eacf4c4965ae4613d77a5c2efe10e5f183") - let from: EthereumAddress? = EthereumAddress("0xed0439eacf4c4965ae4613d77a5c2efe10e5f183") + let contract: EthereumAddress = "0xed0439eacf4c4965ae4613d77a5c2efe10e5f183" + let from: EthereumAddress? = "0xed0439eacf4c4965ae4613d77a5c2efe10e5f183" let gasPrice: BigUInt? = nil let gasLimit: BigUInt? = nil @@ -421,8 +421,8 @@ struct InvalidMethodA: ABIFunction { struct InvalidMethodB: ABIFunction { static let name = "invalidMethodCallBoolResponse" - let contract = EthereumAddress("0xC011A72400E58ecD99Ee497CF89E3775d4bd732F") - let from: EthereumAddress? = EthereumAddress("0xC011A72400E58ecD99Ee497CF89E3775d4bd732F") + let contract: EthereumAddress = "0xC011A72400E58ecD99Ee497CF89E3775d4bd732F" + let from: EthereumAddress? = "0xC011A72400E58ecD99Ee497CF89E3775d4bd732F" let gasPrice: BigUInt? = nil let gasLimit: BigUInt? = nil diff --git a/web3sTests/Contract/ABIDecoderTests.swift b/web3sTests/Contract/ABIDecoderTests.swift index 54662275..18ceedfc 100644 --- a/web3sTests/Contract/ABIDecoderTests.swift +++ b/web3sTests/Contract/ABIDecoderTests.swift @@ -212,11 +212,11 @@ class ABIDecoderTests: XCTestCase { func test_GivenDynamicArrayOfAddresses_ThenDecodesCorrectly() { do { - let addresses = ["0x26fc876db425b44bf6c377a7beef65e9ebad0ec3", - "0x25a01a05c188dacbcf1d61af55d4a5b4021f7eed", - "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", - "0x8c2dc702371d73febc50c6e6ced100bf9dbcb029", - "0x007eedb5044ed5512ed7b9f8b42fe3113452491e"].map { EthereumAddress($0) } + let addresses: [EthereumAddress] = ["0x26fc876db425b44bf6c377a7beef65e9ebad0ec3", + "0x25a01a05c188dacbcf1d61af55d4a5b4021f7eed", + "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "0x8c2dc702371d73febc50c6e6ced100bf9dbcb029", + "0x007eedb5044ed5512ed7b9f8b42fe3113452491e"] let result = try ABIDecoder.decodeData("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000500000000000000000000000026fc876db425b44bf6c377a7beef65e9ebad0ec300000000000000000000000025a01a05c188dacbcf1d61af55d4a5b4021f7eed000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000008c2dc702371d73febc50c6e6ced100bf9dbcb029000000000000000000000000007eedb5044ed5512ed7b9f8b42fe3113452491e", types: [ABIArray.self]) XCTAssertEqual(try result[0].decodedArray(), addresses) @@ -328,7 +328,7 @@ class ABIDecoderTests: XCTestCase { do { let value = try ABIDecoder.decodeData("0x00000000000000000000000064d0ea4fc60f27e74f1a70aa6f39d403bbe56793000000000000000000000000000000000000000000000000000000000000001e", types: [SimpleTuple.self]) - XCTAssertEqual(try value[0].decoded(), SimpleTuple(address: EthereumAddress("0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793"), amount: BigUInt(30))) + XCTAssertEqual(try value[0].decoded(), SimpleTuple(address: "0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793", amount: BigUInt(30))) } catch { print(error.localizedDescription) XCTFail() @@ -365,8 +365,8 @@ class ABIDecoderTests: XCTestCase { let value = try ABIDecoder.decodeData("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000064d0ea4fc60f27e74f1a70aa6f39d403bbe56793000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000003c1bd6b420448cf16a389c8b0115ccb3660bb8540000000000000000000000000000000000000000000000000000000000000078", types: [ABIArray.self]) XCTAssertEqual(try value[0].decodedTupleArray(), [ - SimpleTuple(address: EthereumAddress("0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793"), amount: 30), - SimpleTuple(address: EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"), amount: 120)]) + SimpleTuple(address: "0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793", amount: 30), + SimpleTuple(address: "0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854", amount: 120)]) } catch { print(error.localizedDescription) XCTFail() diff --git a/web3sTests/Contract/ABIFunctionEncoderTests.swift b/web3sTests/Contract/ABIFunctionEncoderTests.swift index d1b6e26c..9ef322e2 100644 --- a/web3sTests/Contract/ABIFunctionEncoderTests.swift +++ b/web3sTests/Contract/ABIFunctionEncoderTests.swift @@ -99,11 +99,11 @@ class ABIFunctionEncoderTests: XCTestCase { } func testGivenArrayOfAddressses_ThenEncodesCorrectly() { - let addresses = ["0x26fc876db425b44bf6c377a7beef65e9ebad0ec3", + let addresses: [EthereumAddress] = ["0x26fc876db425b44bf6c377a7beef65e9ebad0ec3", "0x25a01a05c188dacbcf1d61af55d4a5b4021f7eed", "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "0x8c2dc702371d73febc50c6e6ced100bf9dbcb029", - "0x007eedb5044ed5512ed7b9f8b42fe3113452491e"].map(EthereumAddress.init) + "0x007eedb5044ed5512ed7b9f8b42fe3113452491e"] XCTAssertNoThrow(try encoder.encode(addresses)) let encoded = try! encoder.encoded() @@ -141,7 +141,7 @@ class ABIFunctionEncoderTests: XCTestCase { } func testGivenSimpleTuple_ThenEncodesCorrectly() { - let tuple = SimpleTuple(address: EthereumAddress("0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793"), amount: BigUInt(30)) + let tuple = SimpleTuple(address: "0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793", amount: BigUInt(30)) do { try encoder.encode(tuple) @@ -208,8 +208,8 @@ class ABIFunctionEncoderTests: XCTestCase { func testGivenArrayOfTuples_ThenEncodesCorrectly() { let tuples = [ - SimpleTuple(address: EthereumAddress("0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793"), amount: 30), - SimpleTuple(address: EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"), amount: 120)] + SimpleTuple(address: "0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793", amount: 30), + SimpleTuple(address: "0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854", amount: 120)] do { try encoder.encode(tuples) @@ -239,7 +239,7 @@ class ABIFunctionEncoderTests: XCTestCase { func test_GivenArrayOfComplexTuples_WhenEncodesOneEntry_ThenEncodesCorrectly() { do { - let tuple = ComplexTupleWithArray(address: EthereumAddress("0xdF136715f7bafD40881cFb16eAa5595C2562972b"), amount: 2, owners: [SimpleTuple(address: EthereumAddress("0xdF136715f7bafD40881cFb16eAa5595C2562972b"), amount: 100)]) + let tuple = ComplexTupleWithArray(address: "0xdF136715f7bafD40881cFb16eAa5595C2562972b", amount: 2, owners: [SimpleTuple(address: "0xdF136715f7bafD40881cFb16eAa5595C2562972b", amount: 100)]) try encoder.encode([tuple]) XCTAssertEqual(try encoder.encoded().web3.hexString, @@ -251,8 +251,8 @@ class ABIFunctionEncoderTests: XCTestCase { func test_GivenArrayOfComplexTuples_WhenEncodesTwoEntries_ThenEncodesCorrectly() { do { - let tuple1 = ComplexTupleWithArray(address: EthereumAddress("0xdF136715f7bafD40881cFb16eAa5595C2562972b"), amount: 2, owners: [SimpleTuple(address: EthereumAddress("0x4bf21a47b608841e974ff4147fd1a005da7fdf9b"), amount: 100)]) - let tuple2 = ComplexTupleWithArray(address: EthereumAddress("0x69F84b91E7107206E841748C2B52294A1176D45e"), amount: 3, owners: [SimpleTuple(address: EthereumAddress("0xc07d381fFadB957e0FC9218AaBa88556f5C4BB7a"), amount: 200)]) + let tuple1 = ComplexTupleWithArray(address: "0xdF136715f7bafD40881cFb16eAa5595C2562972b", amount: 2, owners: [SimpleTuple(address: "0x4bf21a47b608841e974ff4147fd1a005da7fdf9b", amount: 100)]) + let tuple2 = ComplexTupleWithArray(address: "0x69F84b91E7107206E841748C2B52294A1176D45e", amount: 3, owners: [SimpleTuple(address: "0xc07d381fFadB957e0FC9218AaBa88556f5C4BB7a", amount: 200)]) try encoder.encode([tuple1, tuple2]) XCTAssertEqual(try encoder.encoded().web3.hexString, "0x07e0fd750000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000df136715f7bafd40881cfb16eaa5595c2562972b0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000004bf21a47b608841e974ff4147fd1a005da7fdf9b000000000000000000000000000000000000000000000000000000000000006400000000000000000000000069f84b91e7107206e841748c2b52294a1176d45e000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000c07d381ffadb957e0fc9218aaba88556f5c4bb7a00000000000000000000000000000000000000000000000000000000000000c8") diff --git a/web3sTests/Contract/ABIFunctionTests.swift b/web3sTests/Contract/ABIFunctionTests.swift index bfa17fe4..08c914b8 100644 --- a/web3sTests/Contract/ABIFunctionTests.swift +++ b/web3sTests/Contract/ABIFunctionTests.swift @@ -11,7 +11,7 @@ struct Deposit_NoParameter: ABIFunction { static let name = "deposit" let gasPrice: BigUInt? = nil let gasLimit: BigUInt? = nil - var contract = EthereumAddress("0xFFB9239F43673068E3c8D7664382Dd6Fdd6e40cb") + var contract: EthereumAddress = "0xFFB9239F43673068E3c8D7664382Dd6Fdd6e40cb" let from: EthereumAddress? = nil func encode(to encoder: ABIFunctionEncoder) throws { @@ -22,7 +22,7 @@ struct BalanceOf_Parameter: ABIFunction { static let name = "balanceOf" let gasPrice: BigUInt? = nil let gasLimit: BigUInt? = nil - var contract = EthereumAddress("0xFFB9239F43673068E3c8D7664382Dd6Fdd6e40cb") + var contract: EthereumAddress = "0xFFB9239F43673068E3c8D7664382Dd6Fdd6e40cb" let account: EthereumAddress let from: EthereumAddress? = nil diff --git a/web3sTests/ENS/ENSTests.swift b/web3sTests/ENS/ENSTests.swift index b1bfb3f6..06647886 100644 --- a/web3sTests/ENS/ENSTests.swift +++ b/web3sTests/ENS/ENSTests.swift @@ -44,7 +44,7 @@ class ENSTests: XCTestCase { do { let nameService = EthereumNameService(client: client!) let ens = try await nameService.resolve( - address: EthereumAddress("0xb0b874220ff95d62a676f58d186c832b3e6529c8"), + address: "0xb0b874220ff95d62a676f58d186c832b3e6529c8", mode: .onchain ) XCTAssertEqual("julien.argent.test", ens) @@ -57,7 +57,7 @@ class ENSTests: XCTestCase { do { let nameService = EthereumNameService(client: client!) _ = try await nameService.resolve( - address: EthereumAddress("0xb0b874220ff95d62a676f58d186c832b3e6529c9"), + address: "0xb0b874220ff95d62a676f58d186c832b3e6529c9", mode: .onchain ) XCTFail("Expected to throw while awaiting, but succeeded") @@ -68,9 +68,9 @@ class ENSTests: XCTestCase { func testGivenCustomRegistry_WhenNotExistingAddress_ThenResolvesFailsCorrectly() async { do { - let nameService = EthereumNameService(client: client!, registryAddress: EthereumAddress("0x7D7C04B7A05539a92541105806e0971E45969F85")) + let nameService = EthereumNameService(client: client!, registryAddress: "0x7D7C04B7A05539a92541105806e0971E45969F85") _ = try await nameService.resolve( - address: EthereumAddress("0xb0b874220ff95d62a676f58d186c832b3e6529c9"), + address: "0xb0b874220ff95d62a676f58d186c832b3e6529c9", mode: .onchain ) XCTFail("Expected to throw while awaiting, but succeeded") @@ -107,7 +107,7 @@ class ENSTests: XCTestCase { func testGivenCustomRegistry_WhenInvalidENS_ThenErrorsRequest() async { do { - let nameService = EthereumNameService(client: client!, registryAddress: EthereumAddress("0x7D7C04B7A05539a92541105806e0971E45969F85")) + let nameService = EthereumNameService(client: client!, registryAddress: "0x7D7C04B7A05539a92541105806e0971E45969F85") _ = try await nameService.resolve( ens: "**somegarbage)_!!", mode: .onchain @@ -124,9 +124,9 @@ class ENSTests: XCTestCase { var results: [EthereumNameService.ResolveOutput]? let result = await nameService.resolve(addresses: [ - EthereumAddress("0xb0b874220ff95d62a676f58d186c832b3e6529c8"), - EthereumAddress("0x09b5bd82f3351a4c8437fc6d7772a9e6cd5d25a1"), - EthereumAddress("0x7e691d7ffb007abe91d8a24d7f22fc74307dab06") + "0xb0b874220ff95d62a676f58d186c832b3e6529c8", + "0x09b5bd82f3351a4c8437fc6d7772a9e6cd5d25a1", + "0x7e691d7ffb007abe91d8a24d7f22fc74307dab06" ]) switch result { @@ -167,8 +167,8 @@ class ENSTests: XCTestCase { XCTAssertEqual( results, [ - .resolved(EthereumAddress("0xb0b874220ff95d62a676f58d186c832b3e6529c8")), - .resolved(EthereumAddress("0x7e691d7ffb007abe91d8a24d7f22fc74307dab06")), + .resolved("0xb0b874220ff95d62a676f58d186c832b3e6529c8"), + .resolved("0x7e691d7ffb007abe91d8a24d7f22fc74307dab06"), .couldNotBeResolved(.ensUnknown) ] ) diff --git a/web3sTests/ERC20/ERC20Tests.swift b/web3sTests/ERC20/ERC20Tests.swift index f5eb726d..adfc50e3 100644 --- a/web3sTests/ERC20/ERC20Tests.swift +++ b/web3sTests/ERC20/ERC20Tests.swift @@ -42,7 +42,7 @@ class ERC20Tests: XCTestCase { func testNoDecimals() async { do { - let decimals = try await erc20?.decimals(tokenContract: EthereumAddress("0x40dd3ac2481960cf34d96e647dd0bc52a1f03f52")) + let decimals = try await erc20?.decimals(tokenContract: "0x40dd3ac2481960cf34d96e647dd0bc52a1f03f52") XCTAssertEqual(decimals, 0) } catch { XCTFail("Expected decimals but failed \(error).") @@ -73,7 +73,7 @@ class ERC20Tests: XCTestCase { func testGivenAddressWithInTransfers_ThenGetsTheTransferEvents() async { do { - let events = try await erc20?.transferEventsTo(recipient: EthereumAddress("0x72e3b687805ef66bf2a1e6d9f03faf8b33f0267a"), fromBlock: .Earliest, toBlock: .Latest) + let events = try await erc20?.transferEventsTo(recipient: "0x72e3b687805ef66bf2a1e6d9f03faf8b33f0267a", fromBlock: .Earliest, toBlock: .Latest) XCTAssert(events!.count > 0) } catch { XCTFail("Expected events but failed \(error).") @@ -82,7 +82,7 @@ class ERC20Tests: XCTestCase { func testGivenAddressWithoutInTransfers_ThenGetsNoTransferEvents() async { do { - let events = try await erc20?.transferEventsTo(recipient: EthereumAddress("0x78eac6878f5ef99bf2b12698f03faf8b33f02676"), fromBlock: .Earliest, toBlock: .Latest) + let events = try await erc20?.transferEventsTo(recipient: "0x78eac6878f5ef99bf2b12698f03faf8b33f02676", fromBlock: .Earliest, toBlock: .Latest) XCTAssertEqual(events?.count, 0) } catch { XCTFail("Expected events but failed \(error).") @@ -91,7 +91,7 @@ class ERC20Tests: XCTestCase { func testGivenAddressWithOutgoingEvents_ThenGetsTheTransferEvents() async { do { - let events = try await erc20?.transferEventsFrom(sender: EthereumAddress("0x2FB78FA9842f20bfD515A41C3196C4b368bDbC48"), fromBlock: .Earliest, toBlock: .Latest) + let events = try await erc20?.transferEventsFrom(sender: "0x2FB78FA9842f20bfD515A41C3196C4b368bDbC48", fromBlock: .Earliest, toBlock: .Latest) XCTAssertEqual(events?.first?.log.transactionHash, "0xfb6e0d7fdf8f9b97fe9b634cb5abc7041ee47a396191f23425955f9fda008efe") XCTAssertEqual(events?.first?.to, EthereumAddress("0xFe325C1E3396b2285d517B0CE2E3ffA472260Bce")) XCTAssertEqual(events?.first?.value, BigUInt(10).power(18)) @@ -103,7 +103,7 @@ class ERC20Tests: XCTestCase { func testGivenAddressWithoutOutgoingEvents_ThenGetsTheTransferEvents() async { do { - let events = try await erc20?.transferEventsFrom(sender: EthereumAddress("0x78eac6878f5ef99bf2b12698f03faf8b33f02676"), fromBlock: .Earliest, toBlock: .Latest) + let events = try await erc20?.transferEventsFrom(sender: "0x78eac6878f5ef99bf2b12698f03faf8b33f02676", fromBlock: .Earliest, toBlock: .Latest) XCTAssertEqual(events?.count, 0) } catch { XCTFail("Expected events but failed \(error).") diff --git a/web3sTests/OffchainLookup/OffchainLookupTests.swift b/web3sTests/OffchainLookup/OffchainLookupTests.swift index ed53a5ce..a4e5f40d 100644 --- a/web3sTests/OffchainLookup/OffchainLookupTests.swift +++ b/web3sTests/OffchainLookup/OffchainLookupTests.swift @@ -12,7 +12,7 @@ struct DummyOffchainENSResolve: ABIFunction { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0x7A876E79a89b9B6dF935F2C1e832E15930FEf3f6") + var contract: EthereumAddress = "0x7A876E79a89b9B6dF935F2C1e832E15930FEf3f6" var from: EthereumAddress? var node: Data @@ -28,7 +28,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data @@ -43,7 +43,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data @@ -58,7 +58,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data @@ -73,7 +73,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data @@ -88,7 +88,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data @@ -103,7 +103,7 @@ enum EthersTestContract { var gasPrice: BigUInt? var gasLimit: BigUInt? - var contract = EthereumAddress("0xAe375B05A08204C809b3cA67C680765661998886") + var contract: EthereumAddress = "0xAe375B05A08204C809b3cA67C680765661998886" var from: EthereumAddress? var data: Data diff --git a/web3sTests/Transaction/TransactionTests.swift b/web3sTests/Transaction/TransactionTests.swift index de3944fd..0207c1a5 100644 --- a/web3sTests/Transaction/TransactionTests.swift +++ b/web3sTests/Transaction/TransactionTests.swift @@ -8,8 +8,8 @@ import XCTest @testable import web3 class TransactionTests: XCTestCase { - let withoutChainID: EthereumTransaction = EthereumTransaction(from: EthereumAddress("0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173"), - to: EthereumAddress("0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174"), + let withoutChainID: EthereumTransaction = EthereumTransaction(from: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", + to: "0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174", value: 0, data: Data(), nonce: 1, @@ -18,14 +18,14 @@ class TransactionTests: XCTestCase { chainId: nil) func test_GivenLocalTransaction_WhenTransactionOnlyWithToAndData_HashIsNil() { - let transaction = EthereumTransaction(to: EthereumAddress("0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173"), + let transaction = EthereumTransaction(to: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", data: Data()) XCTAssertNil(transaction.hash) } func test_GivenLocalTransaction_WhenTransactionDoesNotHaveNonce_HashIsNil() { - let transaction = EthereumTransaction(from: EthereumAddress("0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173"), - to: EthereumAddress("0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174"), + let transaction = EthereumTransaction(from: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", + to: "0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174", data: Data(), gasPrice: 10, gasLimit: 400000) @@ -33,8 +33,8 @@ class TransactionTests: XCTestCase { } func test_GivenLocalTransaction_WhenTransactionWithNonce_HashIsCorrect() { - let transaction = EthereumTransaction(from: EthereumAddress("0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173"), - to: EthereumAddress("0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174"), + let transaction = EthereumTransaction(from: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", + to: "0x1639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7174", value: 0, data: Data(), nonce: 1, diff --git a/web3swift/src/Client/Models/EthereumAddress.swift b/web3swift/src/Client/Models/EthereumAddress.swift index 4f01c279..46e0ab46 100644 --- a/web3swift/src/Client/Models/EthereumAddress.swift +++ b/web3swift/src/Client/Models/EthereumAddress.swift @@ -7,7 +7,7 @@ import Foundation public struct EthereumAddress: Codable, Hashable { public let value: String - public static let zero = EthereumAddress("0x0000000000000000000000000000000000000000") + public static let zero: Self = "0x0000000000000000000000000000000000000000" public init(_ value: String) { self.value = value.lowercased() @@ -48,3 +48,10 @@ public extension EthereumAddress { return result } } + +extension EthereumAddress: ExpressibleByStringLiteral { + + public init(stringLiteral value: String) { + self.init(value) + } +} diff --git a/web3swift/src/ENS/ENSContracts.swift b/web3swift/src/ENS/ENSContracts.swift index 650a8183..448f53b4 100644 --- a/web3swift/src/ENS/ENSContracts.swift +++ b/web3swift/src/ENS/ENSContracts.swift @@ -9,8 +9,8 @@ import Foundation public typealias ENSRegistryResolverParameter = ENSContracts.ResolveParameter public enum ENSContracts { - static let RopstenAddress = EthereumAddress("0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e") - static let MainnetAddress = EthereumAddress("0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e") + static let RopstenAddress: EthereumAddress = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e" + static let MainnetAddress: EthereumAddress = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e" public static func registryAddress(for network: EthereumNetwork) -> EthereumAddress? { switch network { diff --git a/web3swift/src/Multicall/MulticallContract.swift b/web3swift/src/Multicall/MulticallContract.swift index 57fa1487..4131d23f 100644 --- a/web3swift/src/Multicall/MulticallContract.swift +++ b/web3swift/src/Multicall/MulticallContract.swift @@ -9,9 +9,9 @@ import Foundation extension Multicall { public enum Contract { - static let ropstenAddress = EthereumAddress("0x604D19Ba889A223693B0E78bC1269760B291b9Df") - static let rinkebyAddress = EthereumAddress("0xF20A5837Eb2D9F1F7cdf9D635f3Bc68C47B8B8fF") - static let mainnetAddress = EthereumAddress("0xF34D2Cb31175a51B23fb6e08cA06d7208FaD379F") + static let ropstenAddress: EthereumAddress = "0x604D19Ba889A223693B0E78bC1269760B291b9Df" + static let rinkebyAddress: EthereumAddress = "0xF20A5837Eb2D9F1F7cdf9D635f3Bc68C47B8B8fF" + static let mainnetAddress: EthereumAddress = "0xF34D2Cb31175a51B23fb6e08cA06d7208FaD379F" public static func registryAddress(for network: EthereumNetwork) -> EthereumAddress? { switch network {