1

i want test forcing ether send to contract that without payable function. mastering ethereum_chap 9 in this page, we can send ether forcibly with contract's selfdestruct function.

first, make deposit contract. this contract have two function, deposit and return balance but have no payable function

pragma solidity ^0.4.18;
contract EtherGame {
    mapping(address => uint256) public balances;

    function depositFunds() public {
        balances[msg.sender] += msg.value;
        }

    function call_amount() public returns(uint256) {
        return this.balance;
    }
}

and second contract is simple selfdestruct contract.

contract send_ether{
mapping(address => uint256) public balances;

function depositFunds() public payable {
    balances[msg.sender] += msg.value;
    }

function close() public {
    selfdestruct('first contract's address);
}

}

i deploy two contract and deposit 10 ether on second contract. then, run close() on second contract. but there is no ether on first contract. where my ether is gone? why first contract do not send ether?

i test this simple on remix web, with javascrpit VM environment.


i solve this problem myself, and share my improve code.

    pragma solidity ^0.4.18;
contract EtherGame {
    mapping(address => uint256) public balances;
    uint256 public contract_balances;

    function depositFunds() public payable {
        balances[msg.sender] += msg.value;
        contract_balances = this.balance;
    }
}

second contract

contract send_ether{
    mapping(address => uint256) public balances;

    function depositFunds() public payable {
        balances[msg.sender] += msg.value;
        }

    function close() public {
        selfdestruct(0x692a70d2e424a56d2c6c27aa97d1a86395877b3a);
    }
}
  • Your code is fine, so the problem is most likely in your testing methodology. You might want to explain it in detail, step by step. (What do you deploy first, how do you obtain the address, how are you sending ether, how do you check the balances, etc.) – smarx May 2 at 13:22
  • Test it in remix and it works. – Jaime May 2 at 13:26
  • @smarx thanks for your adviser. problem is solved – sang oh May 3 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.