0

I have the following contract:

pragma solidity >= 0.5.0 < 0.7.0;
 
contract SimpleContract {
  string  public name;
  address private owner;
  constructor() public {
     name = 'abs'; 
     owner = msg.sender; 
  }
  function getName() public view returns(string memory) {
      return (name);
   }
   modifier onlyOwner(){
      require(msg.sender == owner);
      _;
   }

   function changeName(string memory _name) public onlyOwner{
      name = _name;
   }
}

It works if I access through the owner's account:

truffle(development)> await SimpleContract.getName()
‘abs’

But I want to access the functions through a non-owner. I am trying the following:

truffle(development)> await SimpleContract.start({from: web3.acc1}) Thrown: TypeError: SimpleContract.start is not a function at evalmachine.:1:24

truffle(development)> await web3.eth.accounts[1].SimpleContract.getName(); Thrown: TypeError: Cannot read property 'SimpleContract' of undefined at evalmachine.:1:30 reentrancy:

Somebody please guide me how to access the contract using a non-owner.

Zulfi.

1 Answer 1

1

First you need to retrieve the accounts:

web3.eth.getAccounts().then(function(acc){ accounts = acc })

Then, for instance, if you try to call the changeName() function from a different address than the contract creator, transaction will revert:

await SimpleContract.changeName('Sergi', {from: accounts[2]})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.