Why can't I send ether to my smart address?
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)
it still works. However, there was one surprise: the msg.sender.transfer line (amount); in my contract won't compile on web3 even though it got this line straight from the common hardness docs palette . Had to use Solc instead because transfer () was not in 0.4.6 ...
Is the main part of the ether not transmitted ()? I would suggest to exist in v 0.1
Anyway, I then try to add ether to the contract like this:
load_up = {
from: admin_account,
to: deployed_contract.address,
value: web3.toWei(1, 'ether'),
}
web3.eth.sendTransaction(load_up)
and I get:
Error: VM Exception while processing transaction: invalid opcode
which doesn't give me much work to do. What am I doing wrong and how should I debug problems like this in the future?
source to share
It turns out I need to create a method for the keyword contract payable
like:
function AddEth () payable {}
and then I can interact with my contract like this:
load_up = {
from: admin_account,
to: deployed_contract.address,
value: web3.toWei(10, 'ether'),
}
deployed_contract.AddEth.sendTransaction(load_up)
source to share