I'm trying to subscribe to a smart contract event from my java app using web3j.
I deployed my contract to truffle, here it is:
pragma solidity ^0.4.24;
contract MyContract {
string message;
event MyEvent(address contractAddress, string message);
constructor() public {
message = "I'm ready!";
}
function setGreetings(string _message) public {
message = _message;
}
function getGreetings() public view returns (string) {
return message;
}
function triggetEvent() public {
MyEvent(address(this), message);
}
}
In my java app I'm subscribing to this event:
web3 = Web3j.build(new HttpService("http://localhost:9545/"));
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
MyContract myContract = MyContract.load(CONTRACT_ADDRESS, web3, credentials, GAS_PRICE, GAS_LIMIT);
EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, myContract.getContractAddress());
String encodedEventSignature = EventEncoder.encode(MyContract.MYEVENT_EVENT);
filter.addSingleTopic(encodedEventSignature);
log.info("subscribing to event with filter");
web3.ethLogObservable(filter).subscribe(eventString -> log.info("event string={}", eventString.toString()));
Where MyContract.MYEVENT_EVENT
looks like this:
public static final Event MYEVENT_EVENT = new Event("MyEvent",
Arrays.<TypeReference<?>>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Utf8String>() {}));
Now I'm calling the triggetEvent()
method from truffle console, the method runs but my java app prints nothing.
What am I missing here?