0

I am trying to make smart contracts with an eternal storage design. So lets say I have contract A and B:

Contract A is Ownable
{ 
  struct Item
  {
     uint256 id;
     uint256 price;
     address owner;
  }

  Item it;

  modifier onlyItemOwner
  {
    require(msg.sender == it.owner, "Incorrect owner !");
  }

  function setPrice(uint256 p) public onlyItemOwner
  {
    it1.price = p;
  }
}

Contract B
{
   A itemContract;

   function changePrice(uint256 newp) public
   {
     itemContract.setPrice(newp);
   }
}

So contract A is the data contract and contract B has the logic. This is just a simple example to explain the problem that I have. In contract A the setPrice() function has a modifier onlyItemOwner. This means that only the owner of the item can change the price of the item. Now the problem that I have is when I call the changePrice() function in contract B with the owner of the item, it will call the setPrice() method from contract A with the contract address of contract B. So the setPrice() method will throw an error.

What is the best way to solve this issue ? Or is it impossible to use this Eternal Storage pattern to program this smart contract ?

A solution I tried was to make contract B the owner of contract A. And then change the setPrice function in contract A as follows:

function setPrice(uint256 p, addres sender) public
{
   require(sender==it.owner);
   it.price = p;
}

And then in contract B I changed the function changePrice as follows:

function changePrice(uint256 newp) public
{
  itemContract.setPrice(newp, msg.sender);
}

Logically this would solve the problem. However, when I do this I run into problems during testing. If I call the setPrice function of contract A in javascript as follows:

await instance.setPrice(10, {from: contractB});

This will give me an error: sender account not recognized. I searched online what is causing this and according to what I found on some forum is that you can't put a contract address in the "from" part, which is exactly what I did here.

So I am a bit stuck now. If anyone can give me some pointers to solve this, I would be grateful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.