2
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)
直到这里,这个工程。然而,令人意外的是,该行 msg.sender.transfer(amount);我的合同中的 无法在web3上编译,尽管从solidity docs的common pattern section中直接得到了该行。不得不使用Solc,因为transfer()不在0.4.6中...为什么我无法将乙醚发送到我的智能合约地址?
不是transfer()是ethereum的核心部分吗?我本来预计到v中存在0.1
无论如何,后来我尝试醚添加到合同这样的:
load_up = {
from: admin_account,
to: deployed_contract.address,
value: web3.toWei(1, 'ether'),
}
web3.eth.sendTransaction(load_up)
,我也得到:
Error: VM Exception while processing transaction: invalid opcode
这不给我很多工作。我做错了什么,以及我将来如何调试这样的问题?
你可以发布你的合同代码吗?你正在使用哪个版本的solc?你使用什么样的节点(主,测试或本地testrpc?) – Gerben
检查此https://ethereum.stackexchange.com/questions/3613/getting-invalid-opcode-error – Gawey