2

I have a contract called tracking. It has a function called move() I called this function using code:

import {bonds} from 'oo7-parity';
const counterABI = [...]
const ContractAddress = '...'

constructor() {
    super()
    this.counter = bonds.makeContract(ContractAddress, counterABI)
}


/*hidden code*/

this.counter.move(data, this.state.caminho[this.state.caminho.length - 1], id)

This function move() returns true or false. But how can I have access to this returned value?

I tried the following:

let res = this.counter.move(data, this.state.caminho[this.state.caminho.length - 1], id)
console.log(res)

But is doesn't work. This just print a huge json and I don't know where ir the returned value (true or false)

1 Answer 1

2

You probably want to do this using events. So in your contract you'll add something like this to the top of the contract.

event Minted(uint256 amount, uint256 totalCost);

and in your method you'll emit the event like this:

function mint(uint256 numTokens) public payable {
    uint256 priceForTokens = priceToMint(numTokens);
    require(msg.value >= priceForTokens);

    totalSupply = totalSupply.add(numTokens);
    balances[msg.sender] = balances[msg.sender].add(numTokens);
    poolBalance = poolBalance.add(priceForTokens);
    if (msg.value > priceForTokens) {
        msg.sender.transfer(msg.value - priceForTokens);
    }

    emit Minted(numTokens, priceForTokens);
}

Then in your Node.js code, you can iterate through the events with the .getPastEvents() method:

   await EthPolynomialCurveToken.getPastEvents(['Minted', 'Burned'], {fromBlock: blockNum, toBlock: 'latest'}, async (err, events) => {
      for(var i = 0; i < events.length(); i++){
        // Something like tokensMinted = events[i].returnValues.amount;
      } 
    })

If you want the most recent event you can just do something like:

events[events.length()].returnValues.amount
2
  • 1
    I'm using events now instead of just returning true or false. I'm using the oo7-parity library with reactjs. So I added to my constructor: this.events = this.counter.moved() the name of the event in the contract is "moved" If I use this.events.map(result => console.log(result())) in an <Rspan> tag provided by oo7-react I can see the the events in the console. But when I try in a regular function nothing happens Jun 12, 2018 at 18:41
  • I have not used this library before, but if you're getting these events to output in the console, then you are getting them, you just need to figure out how to store them locally. I wish I could help more, sorry :/
    – Kyle
    Jun 12, 2018 at 19:23

Your Answer

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

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