Skip to content

Commit 58da054

Browse files
authored
feat: add getLogs method in eth namespace (#143)
1 parent c076d6a commit 58da054

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Sources/Core/Web3/Web3.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,31 @@ public struct Web3 {
460460
properties.provider.send(request: req, response: response)
461461
}
462462

463+
public func getLogs(
464+
addresses: [EthereumAddress]?,
465+
topics: [[EthereumData]]?,
466+
fromBlock: EthereumQuantityTag,
467+
toBlock: EthereumQuantityTag,
468+
response: @escaping Web3ResponseCompletion<[EthereumLogObject]>
469+
) {
470+
struct LogsParam: Codable {
471+
let address: [EthereumAddress]?
472+
let topics: [[EthereumData]]?
473+
474+
let fromBlock: EthereumQuantityTag
475+
let toBlock: EthereumQuantityTag
476+
}
477+
478+
let req = RPCRequest<[LogsParam]>(
479+
id: properties.rpcId,
480+
jsonrpc: Web3.jsonrpc,
481+
method: "eth_getLogs",
482+
params: [LogsParam(address: addresses, topics: topics, fromBlock: fromBlock, toBlock: toBlock)]
483+
)
484+
485+
properties.provider.send(request: req, response: response)
486+
}
487+
463488
// MARK: - Events
464489

465490
public func subscribeToNewHeads(

Sources/PromiseKit/Web3+PromiseKit.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ public extension Web3.Eth {
277277
}
278278
}
279279
}
280+
281+
func getLogs(
282+
addresses: [EthereumAddress]?,
283+
topics: [[EthereumData]]?,
284+
fromBlock: EthereumQuantityTag,
285+
toBlock: EthereumQuantityTag
286+
) -> Promise<[EthereumLogObject]> {
287+
return Promise { seal in
288+
self.getLogs(addresses: addresses, topics: topics, fromBlock: fromBlock, toBlock: toBlock) { response in
289+
response.sealPromise(seal: seal)
290+
}
291+
}
292+
}
280293
}
281294

282295
fileprivate extension Web3Response {

Tests/Web3Tests/Web3Tests/Web3HttpTests.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,64 @@ class Web3HttpTests: QuickSpec {
548548
}
549549
}
550550
}
551+
552+
context("eth get logs") {
553+
// HTTP
554+
waitUntil(timeout: .seconds(2)) { done in
555+
firstly {
556+
try web3.eth.getLogs(
557+
addresses: [EthereumAddress(hex: "0xdAC17F958D2ee523a2206206994597C13D831ec7", eip55: true)],
558+
topics: [[EthereumData(ethereumValue: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")]],
559+
fromBlock: .block(BigUInt(15884445)),
560+
toBlock: .block(BigUInt(15884445))
561+
)
562+
}.done { logs in
563+
for log in logs {
564+
it("should only include logs with this topic") {
565+
expect(log.topics[0].hex()) == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
566+
}
567+
}
568+
it("should be the expected number of logs") {
569+
expect(logs.count) == 22
570+
}
571+
done()
572+
}.catch { error in
573+
it("should not fail") {
574+
expect(false) == true
575+
}
576+
done()
577+
}
578+
}
579+
580+
// WebSocket
581+
waitUntil(timeout: .seconds(2)) { done in
582+
try! web3Ws.eth.getLogs(
583+
addresses: [EthereumAddress(hex: "0xdAC17F958D2ee523a2206206994597C13D831ec7", eip55: true)],
584+
topics: [[EthereumData(ethereumValue: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")]],
585+
fromBlock: .block(BigUInt(15884445)),
586+
toBlock: .block(BigUInt(15884445))
587+
) { response in
588+
it("should be status ok") {
589+
expect(response.status.isSuccess) == true
590+
}
591+
it("should not be nil") {
592+
expect(response.result).toNot(beNil())
593+
}
594+
595+
for log in response.result ?? [] {
596+
it("should only include logs with this topic") {
597+
expect(log.topics[0].hex()) == "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
598+
}
599+
}
600+
it("should be the expected number of logs") {
601+
expect(response.result?.count) == 22
602+
}
603+
604+
// Tests done
605+
done()
606+
}
607+
}
608+
}
551609
}
552610
}
553611
}

0 commit comments

Comments
 (0)