library Balance {
    using SafeMath for uint256;

    struct BalanceStorage {
        // users/referrals balances
        mapping(address => uint) balances;
        mapping(address => uint) totalEarn;
    }

    // Add amount wei to address balance
    function _addBalance(BalanceStorage storage self, address _address, uint _amount) internal {
        self.balances[_address] = self.balances[_address].add(_amount);
        self.totalEarn[_address] = self.totalEarn[_address].add(_amount);
    }

    function _subBalance(BalanceStorage storage self, address _address, uint _amount) internal {
        self.balances[_address] = self.balances[_address].sub(_amount);
        self.totalEarn[_address] = self.totalEarn[_address].sub(_amount);
    }

    function _clearBalance(BalanceStorage storage self, address _address) internal {
        self.balances[_address] = 0;
    }

    function _getBalance(BalanceStorage storage self, address _address) internal view returns (uint){
        return self.balances[_address];
    }

    function _getTotalEarn(BalanceStorage storage self, address _address) internal view returns (uint){
        return self.totalEarn[_address];
    }
}
function _addBalance(BalanceStorage storage self, address _address, uint _amount) internal {
    self.balances[_address] = self.balances[_address].add(_amount);
    self.totalEarn[_address] = self.totalEarn[_address].add(_amount);
}

// Add amount wei to address balance