1

Considering this contract :

pragma solidity ^0.4.7;

contract Test {

    function Test() {
        // constructor
    }

    function getMsgSender() constant returns (address account) {
        account = msg.sender;
    }

}

When I deploy my app with truffle serve and use chrome dbg and run

var t = Test.deployed();
t.getMsgSender().then(function(res) {
    console.log(res.toString())
});

I expect to received my accounts[0] but I received another address which I'm not aware of.

EDIT As Roland Kofler said on the comment section, it seems that the injected web3 form MetaMask is somehow overwrite by web3 provided by truffle.
When I open another tab and play with the injected web3 from MetaMask :

var testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getMsgSender","outputs":[{"name":"account","type":"address"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
var test = testContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x606060405234610000575b5b5b60bd8061001a6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637a6ce2e114603c575b6000565b3460005760466088565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60003390505b905600a165627a7a7230582010d921743656299a4367d73c9ad8249848967aebebbc91093aeb9dce6f74a3380029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })
test.getMsgSender(function(error, result) {
    console.log(result.toString()) // Works, accounts[0] as result
});

Is there a way to make things works with truffle ? Where does the address I get from the overwriting cames from ?

  • you probably overwrite web3 with metamask, while having your own localhost geth or the other way around, or you are on a different network, like testnet, mainnet etc. – Roland Kofler Jan 19 '17 at 13:48
  • For sure I'm on the same network (ropsten) and my MetaMask is configured as default, not connected with my localhost 8545 node. What do you mean by overwriting web3 ? I just consume the injection directly in js :/ For instance I retrieve my metamask account with web3.eth.getAccounts or web3.eth.accounts, I don't change its value. – onepix Jan 19 '17 at 14:07

Your Answer

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

Browse other questions tagged or ask your own question.