-
Notifications
You must be signed in to change notification settings - Fork 444
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
feat: implement eth_getLogs #830
Merged
JeneaVranceanu
merged 5 commits into
web3swift-team:develop
from
zhangliugang:query-fileter
Sep 20, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7719379
feat: enable encode Event and IETH.getLogs
zhangliugang 909a1b0
remove a force unwrap
zhangliugang df87030
Trim Trailing Whitespace
zhangliugang 89a9863
add more test for encodeTopic
zhangliugang d744289
encodeTopic bytes should accept hex string
zhangliugang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// EventTests.swift | ||
// | ||
// | ||
// Created by liugang zhang on 2023/8/24. | ||
// | ||
|
||
import XCTest | ||
import Web3Core | ||
|
||
@testable import web3swift | ||
|
||
class EventTests: XCTestCase { | ||
func testEncodeTopc() throws { | ||
zhangliugang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let encoder = JSONEncoder() | ||
let t1: [EventFilterParameters.Topic] = [] | ||
let t2: [EventFilterParameters.Topic] = [.string(nil)] | ||
let t3: [EventFilterParameters.Topic] = [.strings([.string(nil), .string("1")])] | ||
XCTAssertNoThrow(try encoder.encode(t1)) | ||
XCTAssertNoThrow(try encoder.encode(t2)) | ||
XCTAssertNoThrow(try encoder.encode(t3)) | ||
|
||
let t4: [EventFilterParameters.Topic] = [ | ||
.string("1"), | ||
.strings([ | ||
.string("2"), | ||
.string("3"), | ||
] | ||
)] | ||
let encoded = try encoder.encode(t4) | ||
let json = try JSONSerialization.jsonObject(with: encoded) | ||
XCTAssertEqual(json as? NSArray, ["1", ["2", "3"]]) | ||
} | ||
|
||
func testEncodeLogs() throws { | ||
let contract = try EthereumContract(TestEvent) | ||
let logs = contract.events["UserOperationEvent"]?.encodeParameters( | ||
[ | ||
"0x2c16c07e1c68d502e9c7ad05f0402b365671a0e6517cb807b2de4edd95657042", | ||
"0x581074D2d9e50913eB37665b07CAFa9bFFdd1640", | ||
"hello,world", | ||
true, | ||
"0x02c16c07e1c68d50", | ||
nil | ||
] | ||
) | ||
|
||
XCTAssert(logs?.count == 7) | ||
} | ||
|
||
let TestEvent = """ | ||
zhangliugang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"userOpHash","type":"bytes32"},{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"string","name":"a","type":"string"},{"indexed":true,"internalType":"bool","name":"b","type":"bool"},{"indexed":true,"internalType":"bytes","name":"c","type":"bytes"},{"indexed":true,"internalType":"uint256","name":"d","type":"uint256"}],"name":"UserOperationEvent","type":"event"}] | ||
""" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// EventFilterTests.swift | ||
// | ||
// | ||
// Created by liugang zhang on 2023/8/24. | ||
// | ||
|
||
import XCTest | ||
import Web3Core | ||
|
||
@testable import web3swift | ||
|
||
class EventFilerTests: XCTestCase { | ||
|
||
func testErc20Transfer() async throws { | ||
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken) | ||
let address = EthereumAddress("0xdac17f958d2ee523a2206206994597c13d831ec7")! | ||
let erc20 = ERC20(web3: web3, provider: web3.provider, address: address) | ||
|
||
let topics = erc20.contract.contract.event("Transfer", parameters: [ | ||
"0x003e36550908907c2a2da960fd19a419b9a774b7" | ||
]) | ||
let block = try await web3.eth.block(by: .latest) | ||
let parameters = EventFilterParameters(fromBlock: .exact(block.number - 1000), address: [address], topics: topics) | ||
let result = try await web3.eth.getLogs(eventFilter: parameters) | ||
|
||
// result not always has a log in it. | ||
print(result) | ||
zhangliugang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: move it up and potentially combine into a guard statement with
input.indexed
.This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured it out. If we have a event with 3 fields like
When we want to build a filter to query this event with first and third fields, we have to pass
nil
to second field, and the topicFilter will be likeIf combine
input.indexed
andinput.type.isArray
together, we can only build a filter with first one field.