6

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 ?

4

The problem is actually with the declaration of getOwner() in your contract.

returns (address owner) works like return owner but it expects you to set owner in function's body. Otherwise owner is set to zero.
Read more in Solidity FAQ: Output parameters

function getOwner() returns (address owner) {
    return owner;
}

In your example returns named argument clashes with contracts state variable owner. So, you are actually creating local variable owner with zero value and returning it.

Possible solution is to drop owner from returns:

function getOwner() returns (address) {
    return owner;
}

Btw, I think this is quite counterintuitive and compiler should throw a warning at least.

  • 4
    Thank you, that was indeed the cause. I'm therefore happy to answer my own question. Test.new({from: accounts[0]}) does deploy a new Test contract from the account[0] address ! – Dora Esmephi Mar 3 '17 at 10:07

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.