问题描述
我正在尝试编写一个去中心化应用程序来购买音乐会门票。由于某些原因,零件owner.transfer(this.balance)一直给我错误。另外,由于solidity的版本太多,因此我找不到最适合的版本。请帮助我。谢谢
错误消息
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)
实体代码
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;
}
}
将其更改为owner.transfer(address(this).balance);
后,它给了我另一个错误。
[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.
解决方法
转账仅适用于应付地址。 尝试如下。
address payable owner;
...
owner.transfer(address(this).balance);