1

My contract source code likes below:

contract StandardToken
{
  uint256 totalSupply;
  ...
}
contract Ownable {
  address public owner;
  function Ownable() public {
    owner = msg.sender;
  }
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
  function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0));
    OwnershipTransferred(owner, newOwner);
    owner = newOwner;
  }
}
contract MyToken is StandardToken, Ownable {
     string public name ;
     string public symbol ;
     uint8  public decimals ;
     function MyToken(
            uint256 tokenInitialSupply,
            string tokenName,
            string tokenSymbol,
            uint8 tokenDecimals,
         ) public {
         totalSupply = tokenInitialSupply.mul( 10**uint256(tokenDecimals));
         name = tokenName;
         symbol = tokenSymbol;
         decimals = tokenDecimals;
         balances[msg.sender] = totalSupply;
     }
     ...
     function getowner() public view returns (address) {
            return owner;
       }
}

I use the js code below to transfer ownership:

var tokenAddress = "0xc11b5692d92c5a5f72c5286caaaf27281ff23cc4";
var ownerAddr = "0xf05b0e099cbbb7805cef0c3a822ae2d3b49040ce";
var targetOwnerAddr = "0x700fac5cb73979f0d64b9b410b14a214146f1df1";

var async = require('asyncawait/async');
var await = require('asyncawait/await');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const main = async () => {
    var abi = JSON.parse(fs.readFileSync('/root/abi/mytoken.json', 'utf-8'));
    const contract = new web3.eth.Contract(abi, tokenAddress );
    await web3.eth.personal.unlockAccount(ownerAddr, "********", 600);

    //current owner address
    var currOwnerAddr = await contract.methods.getowner().call();
    console.log(`current owner address: ${currOwnerAddr}`);

    //transfer owner
    await contract.methods.transferOwnership(targetOwnerAddr).call({from: ownerAddr});

    //new owner address
    var newOwnerAddr = await contract.methods.getowner().call();
    console.log(`new owner address: ${newOwnerAddr}`);
}
main();

However, the log below shows owner is unchanged:

current owner address: 0xF05B0e099CbBB7805CEF0C3A822aE2D3B49040ce
new owner address: 0xF05B0e099CbBB7805CEF0C3A822aE2D3B49040ce

So would you help point out where the problem is?Many THANKS.

1

Change this:

await contract.methods.transferOwnership(targetOwnerAddr).call({from: ownerAddr});

To this:

await contract.methods.transferOwnership(targetOwnerAddr).send({from: ownerAddr});

And of course, you'll need to make sure that account ownerAddr is unlocked on the node that you're communicating with...

Please read some more about call and send.

| improve this answer | |
  • THANKS! It works! what's the difference between call() and send() method? – Ntydrm Jan 6 at 12:50
  • @Ntydrm: I've added a couple of links in the answer. Please don't forget to accept it by clicking on the V. – goodvibration Jan 6 at 12:59
  • Checked the links, very clear.thanks again. – Ntydrm Jan 6 at 14:30
  • @Ntydrm: You're welcome. – goodvibration Jan 6 at 14:41

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.