I don't seem to find the answer to what looks like a really basic question. How does one specify a sender address when deploying a new contract using Truffle ?
My contract is owned. So most of my functions can only be called by the address that created the contract. Here is a simple contract's code to highlight the issue :
pragma solidity ^0.4.4;
contract Test {
address owner;
modifier restricted() {
if (msg.sender == owner) _;
}
// Constructor
function Test(){
owner = msg.sender;
}
// Functions reserved to the owner
function remove() restricted {
selfdestruct(owner);
}
function getOwner() returns (address owner){
return owner;
}
}
Here are the tests :
var assert = require('assert');
var Test = artifacts.require('./Test.sol');
contract('Test', function(accounts){
it('should test that the Test contract can be deployed', function(done){
Test.new().then(function(instance){
assert.ok(instance.address);
}).then(done);
});
it('should test that the Test contract is deployed by the correct address (default)', function(done){
Test.new().then(function(instance){
var test = instance;
test.getOwner.call().then(function(owner){
assert.equal(owner, accounts[0], 'Test owned by the wrong address');
}).then(done);
});
});
it('should test that the Test contract is deployed by the correct address (using from)', function(done){
Test.new({from: accounts[0]}).then(function(instance){
var test = instance;
test.getOwner.call().then(function(owner){
assert.equal(owner, accounts[0], 'Test owned by the wrong address');
}).then(done);
});
});
Running this, gives me the following result :
Contract: Test
✓ should test that the Test contract can be deployed (39ms)
1) should test that the Test contract is deployed by the correct address (default)
> No events were emitted
2) should test that the Test contract is deployed by the correct address (set)
> No events were emitted
1 passing (198ms)
2 failing
1) Contract: Test should test that the Test contract is deployed by the correct address (default):
Uncaught AssertionError: Test owned by the wrong address
actual : 0x0000000000000000000000000000000000000000
expected : 0xe75bca9dadb23579e105d3a470e9ba0050ad60ce
+ expected - actual
-0x0000000000000000000000000000000000000000
+0xe75bca9dadb23579e105d3a470e9ba0050ad60ce
at test/testTest.js:16:28
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
2) Contract: Test should test that the Test contract is deployed by the correct address (set):
Uncaught AssertionError: Test owned by the wrong address
actual : 0x0000000000000000000000000000000000000000
expected : 0xe75bca9dadb23579e105d3a470e9ba0050ad60ce
+ expected - actual
-0x0000000000000000000000000000000000000000
+0xe75bca9dadb23579e105d3a470e9ba0050ad60ce
at test/testTest.js:25:28
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
So how can I specify an address ?
Test.new({from: accounts[0]})does deploy a new Test contract from theaccount[0]address ! – Dora Esmephi Mar 03 '17 at 10:07