TL;DR: Second's msg.sender
isn't being set when it should.
Consider these two contracts:
First.sol:
import "Second.sol";
contract First {
address public second;
function First() {
}
function createSecond() {
second = address(new Second());
}
}
Second.sol:
contract Second {
address public sender;
function Second() {
sender = msg.sender;
}
}
When I send a transaction to First's createSecond()
, then get the Second's address and try to get the sender of Second, I get 0x0
returned instead of an address.
Perhaps this is best shown in a test. Here, I'm using Truffle to run the test, and the First
and Second
objects are provided by Truffle through ether-pudding.
contract('First -', function(accounts) {
it("Second should have an sender on creation", function(done) {
var first, second;
console.log("Creating first...");
First.new().then(function(instance) {
first = instance;
console.log("Creating second...");
return first.createSecond();
}).then(function(tx) {
console.log("Getting Second's address...");
return first.second.call();
}).then(function(second_address) {
console.log(second_address);
second = Second.at(second_address);
console.log("Getting Second's sender...");
return second.sender.call();
}).then(function(sender) {
console.log("SENDER ADDRESS:", sender);
assert.notEqual(sender, "0x0");
}).then(done).catch(done);
});
});
Which produces this output:
Using environment test.
Compiling contracts...
Contract: First
Creating first...
Creating second...
Getting Second's address...
0xbf12bb70b9bb9e1b58984a4ba5331a118d622d86
Getting Second's sender...
SENDER ADDRESS: 0x0
1) Second should have an admin on creation.
> No events were emitted
0 passing (2s)
1 failing
1) Contract: First - Second should have an sender on creation:
AssertionError: expected '0x0' to not equal '0x0'
So as you can see, Second's msg.sender
isn't being set when it should. Anyone know why?
My environment:
- OS X El Capitan
- solc 0.2.0-1 via npm
- Confirmed with ethereumjs-testrpc and Geth/v1.3.2-54904379/darwin/go1.5.1