1

I have struct in an external contract that I need access to

Contract Ext{
 Struct user{
    uint dailybalance;
 }
 mapping ( address => user[] ) userDailyBalances;
}

How do I access the dailybalance for a specific index in the mapped array of structs?

Contract Need{

   function need(){
      Ext x = Ext ( Extcontractaddress );
      uint256 bal = x.userDailyBalances( msg.sender )[0].dailybalance;

  } 
}

Does not work nor does x.userDailyBalances( msg.sender[0].dailybalance ) nor x.userDailyBalances( msg.sender.[0].dailybalance )

1

You are trying to access the storage of the external contract directly which is not allowed. Refer here.

You need to have a getter method implemented inside the external contract to make this feasible.

External Contract:

Contract Ext{
 Struct user{
    uint dailybalance;
 }
 mapping ( address => user[] ) userDailyBalances;

function getDailyBalance(user) constant returns(unit) {
        return userDailyBalances( user )[0].dailybalance;
    }

}

Your Contract:

Contract Need{

   function need(){
      Ext x = Ext ( Extcontractaddress );
      uint256 bal = x.getDailyBalance( msg.sender );

  }

}

Hope this helps.

| improve this answer | |
  • it is exactly what I was planning to do.. was just curious if a direct approach was possible – Dino Anastos Sep 19 '17 at 17:40
  • since the mapping is not initialized yet (declared only), you need to have setter function as well in the practical scenario. – Achala Dissanayake Sep 19 '17 at 18:01
  • I used a getter function in the external; contract and then accessed that instead of the mapping directly – Dino Anastos Sep 19 '17 at 20:53

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.