2

I am writing a simple contract which is basically a registry. I am using testRPC for my ethereum testing. Following is my contract on Truffle:

notary.sol

pragma solidity ^0.4.11;

contract notary {
    struct individual {
        bytes32 firstname;
        bytes32 lastname;
        uint yob;
        bytes32 pancard;
        uint aadhar;
    }
    mapping(address => individual) public user;
    function Register(address _to, bytes32 _firstname, bytes32 _lastname, uint _yob, bytes32 _pancard, uint _aadhar) returns (bool success) {
        if (user[_to].aadhar != 0) {
            return false; 
        }
        user[_to].firstname = _firstname;
        user[_to].lastname = _lastname;
        user[_to].yob = _yob;
        user[_to].pancard = _pancard;
        user[_to].aadhar = _aadhar;

        return true;
    }

    function getDetails(address _to) constant returns (bytes32 _firstname, bytes32 _lastname, uint _yob, bytes32 _pancard, uint _aadhar) {
        return (user[_to].firstname, user[_to].lastname, user[_to].yob, user[_to].pancard, user[_to].aadhar);
    }
}

When I have deployed the contract using truffle compile and truffle migrate --reset everything is working as intended. I have already registered one entry on the contract:

Action through console

> notary.deployed().then(function(instance) {NOTR = notary.deployed.at(instance.address)})
> NOTR.Register(web3.eth.accounts[9], 'mukesh', 'kumar', 1987, 'fbjsdkfd67', 324234)
> NOTR.getDetails(web3.eth.accounts[9])

[ '0x6d756b6573680000000000000000000000000000000000000000000000000000',
  '0x6b756d6172000000000000000000000000000000000000000000000000000000',
  { [String: '1987'] s: 1, e: 3, c: [ 1987 ] },
  '0x66626a73646b6664363700000000000000000000000000000000000000000000',
  { [String: '324234'] s: 1, e: 5, c: [ 324234 ] } ]

Now, next I am trying to have this with React as an interface. My React code is longer than I would want to put in here. Mainly, the interface is simply like the below:

enter image description here

Basically, as I click Submit I want to create a new registration. I have declared ABI, Contract Address and other parameters as required in my React App, in addition to all proper import etc. as required therein. In fact the following actually work on my React interface:

  handleSubmit(event) {
    // alert(notaryContract.getDetails(ETHEREUM_CLIENT.eth.accounts[9]))
    // alert(JSON.stringify(this.state))
    // alert(ETHEREUM_CLIENT.isAddress(ETHEREUM_CLIENT.eth.accounts[0]))

But as soon as I try notaryContract.Register('0xcc82fa55789663402c013a497eac268172af7f9d', this.state.setFirstName, this.state.setLastName, this.state.setYearofBirth, this.state.setPanCard, this.state.setAadhar) it gives me an error as following (the _to address included is one of the testrpc ones:

    Uncaught Error: invalid address
        at inputAddressFormatter (http://localhost:3000/static/js/bundle.js:59056:11)
        at inputTransactionFormatter (http://localhost:3000/static/js/bundle.js:34345:27)
...long list...
        at dispatchEvent (http://localhost:3000/static/js/bundle.js:31505:20)

Apologies for a long question but I am sure I am missing something minor here and I had to add in step by step reproduction to allow others to point out the problem. I have tried entering address in string form, then also simply using web3.eth.accounts[x] for including the [_to] address, but to no avail. I am thinking possibly because getDetails is a constant function it is working since it does not expend any gas whereas Register doesn't creates a transaction and cause a state change. However, I can't seem to fathom why it works in truffle console whereas it doesn't in React app. I have checked the bundle.js from where invalid address error is coming from but given that there are no proper debugging logs yet to understand the issues, I am not able to get it running. Any help is very appreciated.

3

There should be an account address associated to any transaction. While using truffle console, it automatically take the default account but not while using web3.

Try to associate an account address to your transaction like below:

notaryContract.Register('0xcc82fa55789663402c013a497eac268172af7f9d', this.state.setFirstName, this.state.setLastName, this.state.setYearofBirth, this.state.setPanCard, this.state.setAadhar, {from: eth.accounts[0]})

or

notaryContract.Register('0xcc82fa55789663402c013a497eac268172af7f9d', this.state.setFirstName, this.state.setLastName, this.state.setYearofBirth, this.state.setPanCard, this.state.setAadhar, {from: <your account address>})
  • Did that, must say it works thanks, however I am getting a Runtime Error: out of gas, whenever I am creation a registration (90000 gas required). Any idea why? Do you also have to add in gas estimation if we are not using console? – Manganese Aug 8 '17 at 5:22
  • Almost transaction costs 120,000 gas through console and 90,000 through application, still through application it fails with a runtime error of gas runout. Should I create a separate question on this? – Manganese Aug 8 '17 at 5:35
  • 1
    Check the estimated gas in testprc log. Convert it to decimal. That much of gas will be required. Try to pas ass that amount of gas in the transaction: notaryContract.Register('0xcc82fa55789663402c013a497eac268172af7f9d', this.state.setFirstName, this.state.setLastName, this.state.setYearofBirth, this.state.setPanCard, this.state.setAadhar, {from: eth.accounts[0], gas: 2000000}). – Abhishek Aug 8 '17 at 6:53
  • Perfect, it is a bit odd though everytime I try and create a contract and subsequent transaction, I need to estimate gas....will it be always like this? When it happens though console, these things are automatically taken care of. Is there a way in applications as well to do that (web3)? – Manganese Aug 8 '17 at 17:12
  • I think, if you set "gas: 2000000", it should be enough gas to execute any contract. you can set default gas limit like "var myContract = MyContract.at(web3.eth.accounts[0], {gasLimit: "2000000"});" – Abhishek Aug 8 '17 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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