Created
June 15, 2020 06:49
-
-
Save kiview/e951b60d065f009e52ded4717dc8df0b to your computer and use it in GitHub Desktop.
Extract historic Ethereum transactions using web3j
This file contains hidden or 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
public class EthereumTransactionExtractor implements AutoCloseable { | |
private final WebSocketService webSocketService; | |
private final Web3j web3j; | |
private final ObjectMapper mapper = new ObjectMapper(); | |
private final JsonGenerator jsonGenerator; | |
public EthereumTransactionExtractor(String url, String fileName) throws IOException { | |
webSocketService = new WebSocketService(url, false); | |
webSocketService.connect(); | |
web3j = Web3j.build(webSocketService); | |
jsonGenerator = new JsonFactory() | |
.createGenerator(new File(fileName), JsonEncoding.UTF8) | |
.setCodec(new ObjectMapper()); | |
} | |
public void extractFromTo(int startBlock, int endBlock) throws IOException { | |
jsonGenerator.writeStartArray(); | |
web3j.replayPastTransactionsFlowable( | |
new DefaultBlockParameterNumber(startBlock), | |
new DefaultBlockParameterNumber(endBlock) | |
).doOnError(Throwable::printStackTrace) | |
.blockingSubscribe(pojo -> { | |
System.out.println("foo"); | |
jsonGenerator.writeObject(pojo); | |
}, | |
100000); | |
jsonGenerator.writeEndArray(); | |
jsonGenerator.close(); | |
} | |
@Override | |
public void close() { | |
webSocketService.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment