简体   繁体   中英

Solidity : Getting error as Member “balance” not found or not visible after argument-dependent lookup

I am trying to write a Decentralization App to buy a concert ticket. For some reason, the part owner.transfer(this.balance) keeps giving me error. Also since solidity has too many version, I can't find a the best for mine. please help me in this. Thank you

Erro Message

Getting error as Member “balance” not found or not visible after argument-dependent lookup. Use address(this).balance to access address owner.transfer(this.balance)

Solidity Code

pragma solidity 0.6.6;

contract Event {
    
    address owner;
    uint public tickets;
    string public description;
    string public website;
    uint constant price = 0.01 ether;
    mapping (address => uint) public purchasers;
    
    constructor(uint t,  string memory _description, string memory _webstite) public {
        owner = msg.sender;
        description = _description;
        website = _webstite;
        tickets = t;
    }
    
    // function () payable {
    //     buyTickets(1);
    // }
    
    function buyTickets(uint amount) public payable {
        if (msg.value != (amount * price) || amount > tickets) {
            revert();
        }
        purchasers[msg.sender] += amount;
        tickets -= amount;
        if (tickets == 0) {
            owner.transfer(this.balance);
        }
    }
    
    function refund(uint numTickets)  public {
        if (purchasers[msg.sender] < numTickets) {
            revert();
        }
        
        msg.sender.transfer(numTickets * price);
        purchasers[msg.sender] -= numTickets;
        tickets += numTickets;
    }
    
}

After I change it to owner.transfer(address(this).balance); , it gave me another error.

[vm] from: 0x5b3...eddc4to: Event.buyTickets(uint256) 0xd91...39138value: 0 weidata: 0x2f3...00001logs: 0hash: 0x030...bbdf1
status  0x0 Transaction mined but execution failed
 transaction hash   0x03045aab3f5d40ebeef4eacedf50ce506edfc2b75c279652839fd74f8e9bbdf1 
 from   0x5b38da6a701c568545dcfcb03fcb875f56beddc4 
 to Event.buyTickets(uint256) 0xd9145cce52d386f254917e481eb44e9943f39138 
 gas    3000000 gas 
 transaction cost   21760 gas 
 execution cost 296 gas 
 hash   0x03045aab3f5d40ebeef4eacedf50ce506edfc2b75c279652839fd74f8e9bbdf1 
 input  0x2f3...00001 
 decoded input  { "uint256 amount": { "_hex": "0x01" } } 
 decoded output {} 
 logs   []  
 value  0 wei 
transact to Event.buyTickets errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

transfer is only able for payable address. try as follows.

address payable owner;
...
owner.transfer(address(this).balance);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM