Skip to content

Instantly share code, notes, and snippets.

@kiview
Created June 15, 2020 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiview/e951b60d065f009e52ded4717dc8df0b to your computer and use it in GitHub Desktop.
Save kiview/e951b60d065f009e52ded4717dc8df0b to your computer and use it in GitHub Desktop.
Extract historic Ethereum transactions using web3j
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