为什么不能将以太币发送到智能合约的地址?

mav*_*vix 3 ethereum solidity

contract_file = 'contract.sol'
contract_name = ':SimpleContract'

Solc = require('solc')
Web3 = require('web3')

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
source_code = fs.readFileSync(contract_file).toString()

admin_account = web3.eth.accounts[0]

compiledContract = Solc.compile(source_code)
abi = compiledContract.contracts[contract_name].interface
bytecode = compiledContract.contracts[contract_name].bytecode;
ContractClass =  web3.eth.contract(JSON.parse(abi))

contract_init_data = {
    data: bytecode,
    from: admin_account,
    gas: 1000000,
}

deployed_contract = ContractClass.new(contract_init_data)
contract_instance = ContractClass.at(deployed_contract.address)
Run Code Online (Sandbox Code Playgroud)

直到这里,这都是有效的。但是,令人惊讶的是,该行msg.sender.transfer(amount); 尽管我从solidity 文档的公共模式部分直接获得了这条线,但我的合同中的内容仍无法在web3上编译。必须使用Solc,因为transfer()不在0.4.6中...

transfer()不是以太坊的核心部分吗?我本来希望在v 0.1中存在

无论如何,我然后尝试像这样将以太添加到合同中:

load_up = {
    from: admin_account, 
    to: deployed_contract.address, 
    value: web3.toWei(1, 'ether'),
}
web3.eth.sendTransaction(load_up)
Run Code Online (Sandbox Code Playgroud)

我得到:

Error: VM Exception while processing transaction: invalid opcode
Run Code Online (Sandbox Code Playgroud)

这并没有给我太多帮助。我在做什么错,将来如何调试此类问题?

mav*_*vix 5

事实证明,我需要使用payable关键字在合同上创建一个方法,例如:

function AddEth () payable {}

然后我可以像这样与我的合同进行交互:

load_up = {
    from: admin_account, 
    to: deployed_contract.address, 
    value: web3.toWei(10, 'ether'),
}
deployed_contract.AddEth.sendTransaction(load_up)
Run Code Online (Sandbox Code Playgroud)