-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiple event subscribers in filters (#1107)
- Loading branch information
1 parent
3271e26
commit b524b0d
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
core/src/main/java/org/web3j/protocol/core/filters/BlocksFilter.java
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,66 @@ | ||
/* | ||
* Copyright 2019 Web3 Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.web3j.protocol.core.filters; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.core.Request; | ||
import org.web3j.protocol.core.methods.response.EthFilter; | ||
import org.web3j.protocol.core.methods.response.EthLog; | ||
import org.web3j.protocol.core.methods.response.EthLog.LogResult; | ||
|
||
/** Handler hashes for working with block filter requests */ | ||
public class BlocksFilter extends Filter<List<String>> { | ||
|
||
public BlocksFilter(Web3j web3j, Callback<List<String>> callback) { | ||
super(web3j, callback); | ||
} | ||
|
||
@Override | ||
EthFilter sendRequest() throws IOException { | ||
return web3j.ethNewBlockFilter().send(); | ||
} | ||
|
||
@Override | ||
void process(List<LogResult> logResults) { | ||
List<String> blockHashes = new ArrayList<>(logResults.size()); | ||
|
||
for (EthLog.LogResult logResult : logResults) { | ||
if (!(logResult instanceof EthLog.Hash)) { | ||
throw new FilterException( | ||
"Unexpected result type: " + logResult.get() + ", required Hash"); | ||
} | ||
|
||
blockHashes.add(((EthLog.Hash) logResult).get()); | ||
} | ||
|
||
callback.onEvent(blockHashes); | ||
} | ||
|
||
/** | ||
* Since the block filter does not support historic filters, the filterId is ignored and an | ||
* empty optional is returned. | ||
* | ||
* @param filterId Id of the filter for which the historic log should be retrieved | ||
* @return Optional.empty() | ||
*/ | ||
@Override | ||
protected Optional<Request<?, EthLog>> getFilterLogs(BigInteger filterId) { | ||
return Optional.empty(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
core/src/main/java/org/web3j/protocol/core/filters/LogsFilter.java
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,66 @@ | ||
/* | ||
* Copyright 2019 Web3 Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.web3j.protocol.core.filters; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.core.Request; | ||
import org.web3j.protocol.core.methods.response.EthFilter; | ||
import org.web3j.protocol.core.methods.response.EthLog; | ||
import org.web3j.protocol.core.methods.response.EthLog.LogResult; | ||
import org.web3j.protocol.core.methods.response.Log; | ||
|
||
/** Logs filter handler. */ | ||
public class LogsFilter extends Filter<List<Log>> { | ||
|
||
private final org.web3j.protocol.core.methods.request.EthFilter ethFilter; | ||
|
||
public LogsFilter( | ||
Web3j web3j, | ||
Callback<List<Log>> callback, | ||
org.web3j.protocol.core.methods.request.EthFilter ethFilter) { | ||
super(web3j, callback); | ||
this.ethFilter = ethFilter; | ||
} | ||
|
||
@Override | ||
EthFilter sendRequest() throws IOException { | ||
return web3j.ethNewFilter(ethFilter).send(); | ||
} | ||
|
||
@Override | ||
void process(List<LogResult> logResults) { | ||
List<Log> logs = new ArrayList<>(logResults.size()); | ||
|
||
for (EthLog.LogResult logResult : logResults) { | ||
if (!(logResult instanceof EthLog.LogObject)) { | ||
throw new FilterException( | ||
"Unexpected result type: " + logResult.get() + " required LogObject"); | ||
} | ||
|
||
logs.add(((EthLog.LogObject) logResult).get()); | ||
} | ||
|
||
callback.onEvent(logs); | ||
} | ||
|
||
@Override | ||
protected Optional<Request<?, EthLog>> getFilterLogs(BigInteger filterId) { | ||
return Optional.of(web3j.ethGetFilterLogs(filterId)); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
core/src/main/java/org/web3j/protocol/core/filters/PendingTransactionsFilter.java
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,65 @@ | ||
/* | ||
* Copyright 2019 Web3 Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package org.web3j.protocol.core.filters; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.core.Request; | ||
import org.web3j.protocol.core.methods.response.EthFilter; | ||
import org.web3j.protocol.core.methods.response.EthLog; | ||
|
||
/** Handler hashes for working with transaction filter requests. */ | ||
public class PendingTransactionsFilter extends Filter<List<String>> { | ||
|
||
public PendingTransactionsFilter(Web3j web3j, Callback<List<String>> callback) { | ||
super(web3j, callback); | ||
} | ||
|
||
@Override | ||
EthFilter sendRequest() throws IOException { | ||
return web3j.ethNewPendingTransactionFilter().send(); | ||
} | ||
|
||
@Override | ||
void process(List<EthLog.LogResult> logResults) { | ||
List<String> logs = new ArrayList<>(logResults.size()); | ||
|
||
for (EthLog.LogResult logResult : logResults) { | ||
if (!(logResult instanceof EthLog.Hash)) { | ||
throw new FilterException( | ||
"Unexpected result type: " + logResult.get() + ", required Hash"); | ||
} | ||
|
||
logs.add(((EthLog.Hash) logResult).get()); | ||
} | ||
|
||
callback.onEvent(logs); | ||
} | ||
|
||
/** | ||
* Since the pending transaction filter does not support historic filters, the filterId is | ||
* ignored and an empty optional is returned | ||
* | ||
* @param filterId Id of the filter for which the historic log should be retrieved | ||
* @return Optional.empty() | ||
*/ | ||
@Override | ||
protected Optional<Request<?, EthLog>> getFilterLogs(BigInteger filterId) { | ||
return Optional.empty(); | ||
} | ||
} |