Solidity
Solidity:adress.transfer(this.balance)不起作用
在我的契約中,我有一個功能應該將契約的餘額轉移給它的所有者。函式為:function moveFunds(){ owner.transfer(this.balance); }
所有者設置為:地址所有者= msg.sender;在合約的建構子中。
但它不起作用。交易已完成,但合約或所有者錢包中的餘額沒有變化。有誰知道如何解決這個問題?還有另一種方法可以做到這一點嗎?我已經在專用網路和 Rinkeby 中多次嘗試過。合約中還有一個捐贈功能執行良好,輸入的值被添加到合約餘額中,並從發送者中減去相同的值。(在嘗試將任何東西轉移給所有者之前,我會這樣做)
提前致謝。
編輯:下面的整個契約:(此處的格式無效..)
pragma solidity ^0.4.2; contract donation { address owner; modifier onlyowner { if (msg.sender == owner) _; } address[] _giver; function Donation() public { owner = msg.sender; } function donate() public payable { if (msg.value == 0) { revert(); } addGiver(msg.sender); } /* * TODO: */ function moveFund() public onlyowner{ owner.transfer(this.balance); } function addGiver(address _a) internal { _giver.push(_a); } }
有一個錯字,確實是一個非常關鍵的錯字。
契約名稱為捐贈(d不大寫)
建構子名為 Donation(D 大寫)
由於合約和建構子的名稱不同,建構子永遠不會被呼叫,所以所有者永遠不會被設置。
pragma solidity 0.4.24; contract eventTickets{ address owner; uint tickets; string description; uint constant price = 1 ether; mapping (address => uint) public purchasers; constructor (uint _tickets, string _description) public { owner = msg.sender; description = _description; tickets = _tickets; } modifier OnlyBy(){ assert (owner == msg.sender); _; } function infoEvent() public view returns (address, string, uint, uint) { return(owner, description, tickets, price); } function buyTickets(uint amount) public payable { if (msg.value < (amount * price) || amount > tickets) { revert(); } if (msg.value > (amount * price)) { msg.sender.transfer(msg.value - (amount * price)); } purchasers[msg.sender] += amount; tickets -= amount; } function refund(uint numTickets) public payable { if (purchasers[msg.sender] <= numTickets) { revert(); } msg.sender.transfer(numTickets * price); purchasers[msg.sender] -= numTickets; tickets += numTickets; } function endSale() public payable OnlyBy(){ owner.transfer(this.balance); }
}