Solidity:在依赖于参数的查找后,由于找不到成员“余额”或不可见而出现错误
Solidity : Getting error as Member “balance” not found or not visible after argument-dependent lookup
我正在尝试编写一个去中心化应用程序来购买音乐会门票。出于某种原因,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)
Solidity 代码
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);
我正在尝试编写一个去中心化应用程序来购买音乐会门票。出于某种原因,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)
Solidity 代码
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);