1

I have a contract that looks like this:

contract MainContract is usingOraclize {
    ...

    address minter;
    uint public returnRate;

    ...

    function MainContract(uint _returnRate) {
        minter = msg.sender;
        returnRate = returnRate;
        oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
    }        

    modifier onlyMinter {
        if (msg.sender != minter) throw;
        _;
    }

     function updateReturnRate(uint newReturnRate) onlyMinter {
        returnRate = newReturnRate;
     }

     ...    

}

I have deployed it on the testnet: https://testnet.etherscan.io/address/0x5cad3cbdbf64b65174b36269f5201d3265569f2b

However, when I try and get the value of the 'returnRate' variable, I get that it is undefined even though it should have been set upon contract creation. So I then tried to update it through the updateReturnRate function and it is still undefined. And I can't explain why...

Do I have to specify the function as being public? Even so why didn't the value get set upon contract creation?

I am using the browser solidity compiler for testing.

  • 2
    Your parameter is _returnRate (underscore prefixed) so you should initialize returnRate via returnRate=_returnRate; – Greg Jeanmart Nov 13 '16 at 0:14
  • Ah right oops... Forgot the underscore. But how come I can't update it using the update function? – Pabi Nov 13 '16 at 8:57
  • 1
    For the updateReturnRate function. it should work, nothing wrong in your code.The only reason it might be is if the transaction is sent by somebody else than minter (contract owner). Otherwise, any exception ? Also for the writing method,I advice to return a successful flag function updateReturnRate(uint newReturnRate) onlyMinter returns(bool successful){ returnRate = newReturnRate; return true; } – Greg Jeanmart Nov 13 '16 at 10:55
  • As it turns out looking at the blockchain: testnet.etherscan.io/address/… the transaction was not being sent from the same address as the contract creation address. Although I do not know why the browser solidity compiler called a function from an address that is not mine. – Pabi Nov 13 '16 at 12:34
  • Even with the same address as the creation address, it doesn't update it. Here is the transaction hash: testnet.etherscan.io/tx/… sent from the same address as the contract creation address: testnet.etherscan.io/address/… – Pabi Nov 13 '16 at 12:37
1

After a quick look, your contract is working fine. I think there is definitely something wrong with Browser Solidity

I'm usually developping with Truffle, so I quickly made a Truffle project of your contract (below)

pragma solidity ^0.4.4;
contract MainContract {
    address minter;
    uint public returnRate;

    function MainContract(uint _returnRate) {
        minter = msg.sender;
        returnRate = _returnRate;
    }        
    modifier onlyMinter {
        if (msg.sender != minter) throw;
        _;
    }
     function updateReturnRate(uint newReturnRate) onlyMinter {
        returnRate = newReturnRate;
     } 
}

I compiled the contract and deployed it :

And tested it in the console. Here is the result :

truffle(default)> var c = MainContract.deployed()
truffle(default)> c.returnRate.call()
{ [String: '20'] s: 1, e: 1, c: [ 20 ] }
truffle(default)> c.updateReturnRate(30)
'0x642d24031f582730000c37dd254173944840e5e771fbb0b74c032bd74f7294ba'
truffle(default)> c.returnRate.call()
{ [String: '30'] s: 1, e: 1, c: [ 30 ] }

The contract initializes the rate to 20. And I changed it to 30 calling updateRate function.

Anyway, I'm not expert in Browser Solidity but I recommend tools like Truffle to industrialize your development and be more efficient.

Everything is there

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.