0

I was following this tutorial.I have deployed the following two contracts on testrpc:

pragma solidity ^0.4.7;

contract MDBService {

    mapping (address => address) accounts;
    mapping (uint => address) ids;
    uint numberAccounts;

    function MDBService() {
        numberAccounts = 0;
    }

    function register() {
        require(accounts[msg.sender] == 0); //We check if the user is already registered
        ids[numberAccounts] = msg.sender;
        accounts[msg.sender] = new MDBAccount(msg.sender);
        numberAccounts++;
    }
}

contract MDBAccount {

    struct Post {
        uint timestamp;
        string message;
    }

    uint public numberPosts;
    address public owner;
    mapping (uint => Post) posts;

    modifier isOwner() {
        require(owner == msg.sender);
        _;
    }

    function MDBAccount(address _owner) {
        owner = _owner;
        numberPosts = 0;
    }

    function post(string message) isOwner() {
        require(bytes(message).length <= 160);
        posts[numberPosts].timestamp = now;
        posts[numberPosts].message = message;
        numberPosts++;
    }
}

Now when I call

MDBService.register({gas:700000,from:web3.eth.accounts[1]}) 

a new instance of MDBAccount is created(i.e. a new account MDBAccount for the user with address of account[1]).So now I want to post a message from this account instead of the default account[0].

When I call

MDBAccount.post('Hello') 

it works because it uses the instance created when the contract deployed.But when I use

MDBAccount.post('Hello',{from:web3.eth.accounts[1]}) 

it fails because isOwner is not satisfied as it is using the same instance and not the one created on calling the above register.So how can I call post for account[1].

1 Answer 1

0

So how can I call post for accounts[1].

  1. Remove isOwner() modifier

or

  1. Initiate contract and send accounts[1] in parameter.

or

  1. Add multiple owners

This is what this modifier is for, it restricts other addresses from calling functions which they should not call.


In your particular case I suggest to add address _owner directly to your struct Post. When you will need to retrieve only the posts of particular account, you will just filter them by post._owner.

struct Post {
        uint timestamp;
        string message;
        address _owner;
    }
4
  • Any of them won't work because what I want is like twitter where there is a separate account for each user and each user has his own tweets separated from each other stored only in their account.When I send account address in parameter than the post should be saved in the account of that address only. Jan 27, 2018 at 5:41
  • Well then I suggest you to add address _owner directly to your struct Post. When you will need to retrieve only the posts of particular account, you will just filter them by post._owner. Another way is to use events, but in this case events will be visible only outside Solidity. Jan 27, 2018 at 10:19
  • Adding _owner to struct Post is a really good alternative.Thanks. Add this as the answer and I will accept it as right. Jan 27, 2018 at 19:23
  • @HimanshuChandra Edited the answer. Jan 27, 2018 at 19:26

Your Answer

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

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