2

With this code, at the creation of the contract (sending some eth in tx), shouldn't the fallback function be called? Then the state variable feedback shouldn't be "Fallback function correctly called"? What am I missing out? I'm running the contract on Javascript VM.

contract MyToken {

    mapping (address => uint256) public balanceOf;
    string name;
    string symbol;
    uint8 decimals; 
    string fallback;

    function MyToken(uint256 initialSupply, string tokenName, string tokenSymbol) payable {
        balanceOf[msg.sender] = initialSupply;             
        name = tokenName;
        symbol = tokenSymbol;
        decimals = 18;
        fallback = "Fallback not called!";
    }

    function feedBack() {
        fallback = "Fallback function correctly called!";
    }

    function getFeedback() constant returns(string) {
        return fallback;
    }

    function() payable {
        feedBack();
    }
}

Output of the contract's deployment:

enter image description here

1 Answer 1

2

When you create your contract (in remix +javascript VM) and you set some ethers in your transaction you will execute then the constructor of your contract function MyToken which will return of course fallback = "Fallback not called!";
to run your fallback function you need to send a transaction with value to the contract(without a fct call) as i know this not possible in javscript VM ( you need to deploy it locally or you can call directly the fallback function).

0

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.