3

I have ReactJS working to read data from my blockchain but I cannot update the blockchain. Here is my code to read the blockchain which works -

  var reviewContract = ETHEREUM_CLIENT.eth.contract(reviewContractABI).at(reviewContractAddress)
  var reviewData = reviewContract.getReviews()

When I try and update it fails:

reviewContract.addReview("company1", "trevor lee oakley", 1)


Uncaught Error: invalid address
at inputAddressFormatter (http://localhost:3000/static/js/bundle.js:45751:12)
at inputTransactionFormatter (http://localhost:3000/static/js/bundle.js:45577:21)
at http://localhost:3000/static/js/bundle.js:45881:29
at Array.map (native)
at Method.formatInput (http://localhost:3000/static/js/bundle.js:45880:33)
at Method.toPayload (http://localhost:3000/static/js/bundle.js:45906:24)
at Eth.send [as sendTransaction] (http://localhost:3000/static/js/bundle.js:45931:31)
at SolidityFunction.sendTransaction (http://localhost:3000/static/js/bundle.js:48458:27)
at SolidityFunction.execute (http://localhost:3000/static/js/bundle.js:48544:38)
at App.componentWillMount (http://localhost:3000/static/js/bundle.js:32663:23)

I have this code to add.

pragma solidity ^0.4.8;
contract Review {

Review[] public reviews;
struct Review {
    bytes32 companyName;
    bytes32 companyReviewer;
    uint companyReview;
}

function addReview (bytes32 _companyName, bytes32 _companyReviewer, uint _companyReview) returns (bool success) {
    Review memory newReview;
    newReview.companyName = _companyName;
    newReview.companyReviewer = _companyReviewer;
    newReview.companyReview = _companyReview;

    reviews.push(newReview);
    return true;
}

function getReviews() constant returns (bytes32[], bytes32[], uint[]) {

    uint length = reviews.length;
    bytes32[] memory companyNames = new bytes32[](length);
    bytes32[] memory companyReviewers = new bytes32[](length);
    uint[] memory companyReviews = new uint[](length);

    for (uint i=0; i<reviews.length; i++) {
        Review memory currentReview;
        currentReview = reviews[i];

        companyNames[i]=currentReview.companyName;
        companyReviewers[i]=currentReview.companyReviewer;
        companyReviews[i]=currentReview.companyReview;

    }
    return (companyNames, companyReviewers, companyReviews);
}
}

The contract works in truffle so I know -

  1. The solidity is OK
  2. Access to the blockchain is OK
  3. Update fails.

Any help is welcome.

Trevor Lee Oakley
  • 2,327
  • 2
  • 19
  • 48

1 Answers1

1

I finally worked this out. You need to make the function payable and also define a base account.

So I added in the ReactJS -

ETHEREUM_CLIENT.eth.defaultAccount = ETHEREUM_CLIENT.eth.coinbase;

and

  function addReview (bytes32 _companyName, bytes32 _companyReviewer, uint _companyReview) payable returns (bool success) {

I checked at github and the method I think is method.js which is relevant. The solidity method needs to be payable to change the state on the blockchain and there a default account worked for the charges.

Trevor Lee Oakley
  • 2,327
  • 2
  • 19
  • 48