0

I have 3 contracts: sender, receiver and transferTest. I am getting error in transferTest due to an address array declared in the reciver contract:

receiver.sol:

pragma solidity ^0.5.8;

contract receiver{
   address public owner;
   mapping(address => uint) balance; 
constructor () public{
   owner = msg.sender;
}
function() external payable{
   balance[owner] += msg.value;}
}

transferTest:

pragma solidity ^0.5.8;
import "truffle/Assert.sol";
import "../contracts/sender.sol";
import "../contracts/receiver.sol";

contract TestTransfer{

   function testTransfer() public{
      sender senderObj = new sender();
      receiver receiverObj = new receiver();
      senderObj.transferTo(msg.sender, 10);
      Assert.equal(receiver.balance[receiver.owner], 9, "Received amount is not correct");
   }
}

I am getting following error message:

testingTransfer.sol:12:20: TypeError: Member "balance" not found or not visible after argument-dependent lookup in type(contract receiver). Assert.equal(receiver.balance[receiver.owner], 9, "Received amount is not correct"); ^--------------^

Compilation failed. See above. Truffle v5.1.67 (core: 5.1.67) Node v10.23.3

Also I used msg.sender for the address of receiver contract as the first argument of transferTo function. Please guide me how to replace msg.sender with the address of receiver contract. ==Update:Dec11 2021 My question is similar to: Accessing Solidity Mapping

Zulfi.

1 Answer 1

1

The balancew isn't public so it can't be accessed from the outside

mapping(address => uint) balance; 

If it were public you would be able to access like a function with the keys as parameters.

mapping(address => mapping(address => uint256)) public allowance; 

From truffle's console

> let allow = await token.allowance("0x1234..", "0x4444...")

A fixed version of TestTransfer should look like

contract TestTransfer {

   function testTransfer() public{
      sender senderObj = new sender();
      receiver receiverObj = new receiver();
      senderObj.transferTo(msg.sender, 10);
      Assert.equal(receiverObj.balance(receiverObj.owner()), 9, "Received amount is not correct");
   }
}
6
  • Kindly suggest me answer for Assert.equal(...) statement in the context of truffle test.
    – zak100
    Dec 12, 2021 at 4:09
  • I made the balance public but same error.
    – zak100
    Dec 12, 2021 at 4:43
  • 1
    @zak100 I haven't tried but Assert.equal(receiver.balance(receiver.owner()), 9, "..."); should work if owner is a public variable in the receiver contract.
    – Ismael
    Dec 12, 2021 at 4:47
  • thanks but it gives the error that member "owner" not found or not visible.
    – zak100
    Dec 12, 2021 at 5:07
  • 1
    @zak100 The problem was that the function were referencing the contract receiver but it should have used the instance receiverObj instead. Take a look at the updated answer, I tested in remix and it should work.
    – Ismael
    Dec 12, 2021 at 19:42

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.