Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conform EthereumAdress to ExpressibleByStringLiteral #254

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions web3sTests/Account/EthereumAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
42 changes: 21 additions & 21 deletions web3sTests/Client/EthereumClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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).")
Expand All @@ -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")
Expand All @@ -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 {
Expand All @@ -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).")
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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).")
}
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions web3sTests/Contract/ABIDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<EthereumAddress>.self])
XCTAssertEqual(try result[0].decodedArray(), addresses)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -365,8 +365,8 @@ class ABIDecoderTests: XCTestCase {
let value = try ABIDecoder.decodeData("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000064d0ea4fc60f27e74f1a70aa6f39d403bbe56793000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000003c1bd6b420448cf16a389c8b0115ccb3660bb8540000000000000000000000000000000000000000000000000000000000000078", types: [ABIArray<SimpleTuple>.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()
Expand Down
16 changes: 8 additions & 8 deletions web3sTests/Contract/ABIFunctionEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions web3sTests/Contract/ABIFunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
Loading