web3j listens to smart contract event events

Suppose an event is defined in the smart contract:

pragma solidity ^ 0.4.0;

contract Transfer{
  event transfer(address indexed _from, address indexed _to, uint indexed value);

  function deposit() payable {
    address current = this;
    uint value = msg.value;
    transfer(msg.sender, current, value);
  }


  function getBanlance() constant returns(uint) {
      return this.balance;
  }

  /* fallback function */
  function(){}
}

The monitoring method for this event on the web3j side is:

void listenEvent()
    {
        Event event = new Event("transfer",
                Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}),
                Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
        EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST,
                DefaultBlockParameterName.LATEST, contract_addr);
        filter.addSingleTopic(EventEncoder.encode(event));
        getWeb3jClient().ethLogObservable(filter).subscribe(new Action1<Log>() {
            @Override
            public void call(Log log) {
                System.println(log.toString());
            }
        });
    }
Note that because the definition form of the transfer event in the smart contract is
event transfer(address indexed _from, address indexed _to, uint indexed value);

So when web3j defines event, it should correspond to:

Event event = new Event("transfer",
                Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}),
                Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
Otherwise the event listener will fail.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326017621&siteId=291194637