Not able send ether to my smart contract's address

0 votes
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)
until here, this works. But the line msg.sender.transfer(amount); in my contract wouldn't compile on web3. 

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
Sep 25, 2018 in Blockchain by digger
• 26,550 points
137 views

1 answer to this question.

0 votes

It turns out that you need to create a method on the contract with the payable keyword, for example:

function AddEth () payable {}

and then you 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)
answered Sep 25, 2018 by slayer
• 29,050 points

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer

Blockchain Hyperledger private key not able to send coins

From the code you posted, if(whichType == TX_SCRIPTHASH){//pay ...READ MORE

answered Sep 14, 2018 in Blockchain by digger
• 26,550 points
65 views
0 votes
1 answer

How do I send ether from an EOA to a smart contract?

You include ether to send in the ...READ MORE

answered Sep 27, 2018 in Blockchain by digger
• 26,550 points
52 views
+1 vote
3 answers

Not able to read a file from my assets folder

Hi.. I faced a similar problem in ...READ MORE

answered Oct 25, 2018 in Blockchain by Shrikanth
651 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
140 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,620 points
136 views
0 votes
1 answer

Not able to send payments using Blockchain API.

Try the following code. It should help ...READ MORE

answered Jul 31, 2018 in Blockchain by slayer
• 29,050 points
41 views
0 votes
1 answer

Not able to send Ethereum transaction

Hey, Change this line: await crowdsale.sendTransaction({value, from: buyer, gas: ...READ MORE

answered Sep 24, 2018 in Blockchain by slayer
• 29,050 points
24 views