1
votes

I'm having trouble running my first solidity contract in remix ethereum and through web3 so I'm guessing something is wrong with my contract code:

pragma solidity ^0.4.0;

contract cntrct 
{
  uint public aaa; 

  function() payable public
  {
    create(msg.value);
  }

  function create(uint _value) internal
  {
    require(_value>0);
    aaa = _value;
  }

  function reader() view public returns(uint)
  {
    return aaa;
  }
} 

I succesfully deployed the contract in both remix and web3. However, after sending a transaction to the contract, the aaa variable is still 0. What I want this code to do is update the aaa variable to the last deposited amount so I can read it later by calling the reader function. In remix it does not show any input field for the aaa variable. Also, in MetaMask transactions sent to the contract stay in a pending status even if they're already completed (balances updated in remix and tx in testRPC.)

In node I'm using the following line to try to execute the reader function but I'm unsure if this will work.

contract.methods.reader().call(0, (error, result) => { if(!error){console.log(result);}});
2

2 Answers

0
votes

There’s no reason to store the ether sent in a state variable unless you need to maintain a mapping of balances spread across multiple addresses. The total balance is held in the contract and is accessible with this.balance.

In addition, fallback functions are limited to 2300 gas. You can’t write to storage (ie, update a state variable) within that limit. Your variable isn’t updated because it’s failing. See the second bullet point here for more info.

0
votes

In solidity contract, you can read the value of public filed by calling field as a method. here, aaa()

I have deployed with mist browser under private network and send 10 ether to this contract successfully.

deployed contract with 10 ehter