0

I have many questions about the workflow ( sequence ) of interacting with the smart contract from Java application, so I will first explain what I have done and then put my questions, and if something wrong in my understanding please let me know.

1- I have written smart contract 2- Use truffle to get the smart contract java wrapper.(contract.java) 3- Use testrpc to test the contract

I have 2 class uses testrpc accounts (credentials) to interact with the smart contract and call its functions

Each class (node1.java, node2.java) call a function in the smart contract called (send) to send their data to the chain.

I have added an event which trigger if the 2 nodes have sent there data

What I don't understand is, how I can let the java code ( Let say MainProgram.class) always check for that event. Because I need to check if both nodes send their data, then I will call another function to analyse this data.

How I can manage, control and check what transactions have been done or not, I mean how I can use the events in java code and let the code run forever and check if the this event happen, do action.

Hope I can explain what I need clearly

Thank you in advance.

2
  • Just add one more thing, if my understanding is correct and I need a MainProgram class to control and check the events then I need to use one of testrpc account to run this code. This means this program is considered as a node in the chain and play as the manager for the network, so the smart contract isn't the control !!
    – sheemar
    Mar 3, 2018 at 0:48
  • Am not java developer, but see if this answer here ethereum.stackexchange.com/a/41534/20357 gives you some idea.
    – Rajesh
    Mar 3, 2018 at 6:53

1 Answer 1

0

In your java wrapper for each type of event in your SmartContract, there will be a function that looks like this:

public Observable<ApprovalEventResponse> approvalEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {

            ...                

            return typedResponse;
        }
    });
}

You can subscribe to this observable from your java code as follows:

contract.approvalEventObservable(DefaultBlockParameter.valueOf("Earliest"),DefaultBlockParameter.valueOf("Latest")).asObservable().subscribe( event ->{
    System.out.prtinln(event._approver);
});

This will replay all events that have occurred and print the attribute of previous events and will also print new events when they occur.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.