1

I am trying to compile two contracts. I am getting 'balance' variable error in "HoneyPotCollect.sol" contract. Is balance not a global contract variable? Somebody please guide me how to correct my error.

//HoneyPot.sol

    pragma solidity ^0.5.0;
      contract HoneyPot {
      mapping (address => uint) public balances;
    
      constructor() payable public {
        put();
      }
    
      function put() payable public {
        balances[msg.sender] = msg.value; // msg.sender here is the address from the sender
      }
    
      function get() public {
         (bool success,) = msg.sender.call.value(balances[msg.sender])("");
          success = false;
          //revert();
        
        balances[msg.sender] = 0;
      }

  function() external {
    revert();
  }
}

//HoneyPotCollect contract

pragma solidity ^0.5.0;

import './HoneyPot.sol';

contract HoneyPotCollect {
  HoneyPot public honeypot;

  constructor(address _honeypot) public {

    honeypot = HoneyPot(_honeypot);
  }

  function kill () public {
    selfdestruct(msg.sender);
  }

  function collect() payable public {
    honeypot.put.value(msg.value)();
    honeypot.get();
  }

  function () external payable  {
    if (honeypot.balance >= msg.value) {
      honeypot.get();
    }
  }
}

Following is my error message:

$ solc HoneyPotCollect.sol

HoneyPotCollect.sol:19:9: Error: Member "balance" not found or not visible after argument-dependent lookup in contract HoneyPot. Use "address(honeypot).balance" to access this address member.

if (honeypot.balance >= msg.value) {
    ^--------------^
1

Is balance not a global contract variable?

No, but it's a member of the address type, so change this:

honeypot.balance

To this:

address(honeypot).balance

See the official documentation for more details.

| improve this answer | |

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.