updated the title and more details added.
I am not able to send the ether to contract operation or contract via Web3.js code and Metamask (blockchain browser plugin). Please see the original post for more details.
- In original post please skip the first code snippet.
- The second code snippet shows what I wrote to send the transaction to contract. It specifies "from address", "to address", "the value of the transaction", and "method" to invoke.
-
Metamask screenshot, which shows every detail accurately, however amount field is still blank. I thought it should have taken from value field of passed transaction.
Maybe I am missing something here, any help to resolve this is highly appreciated.
original post
I am trying to create a contract via web3.js. It works fine. However, need to send ether while creating contract. how do that via web3.js. Please find the code which I am using for creating the contract.
function createContract() {
web3.eth.getAccounts(function(error, result) {
var browser_safebuy_sol_safebuyContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"seller","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"abort","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"buyer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"confirmReceived","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"state","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"demandRefund","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"deposit","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"confirmPurchase","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[],"payable":true,"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"Aborted","type":"event"},{"anonymous":false,"inputs":[],"name":"PurchaseConfirmed","type":"event"},{"anonymous":false,"inputs":[],"name":"ItemReceived","type":"event"},{"anonymous":false,"inputs":[],"name":"DepositReceived","type":"event"}]);
var browser_safebuy_sol_safebuy = browser_safebuy_sol_safebuyContract.new(
{
from: result[0],
data: '0x60606040526000600360146101000a81548160ff0219169083600281111561002357fe5b021790555033600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034600181905550346000819055507ffb606260831153b2b5b9f4a20fdfc7c2394d87a166d6a64035e3d5c11ada9baf60405160405180910390a161077c806100b26000396000f30060606040523615610097576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806308551a531461009c57806335a063b4146100f15780633fa4f245146101065780637150d8ae1461012f57806373fac6f014610184578063c19d93fb14610199578063ca973f07146101d0578063d0e30db0146101e5578063d69606971461020e575b600080fd5b34156100a757600080fd5b6100af610218565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100fc57600080fd5b61010461023e565b005b341561011157600080fd5b610119610386565b6040518082815260200191505060405180910390f35b341561013a57600080fd5b61014261038c565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561018f57600080fd5b6101976103b2565b005b34156101a457600080fd5b6101ac61050f565b604051808260028111156101bc57fe5b60ff16815260200191505060405180910390f35b34156101db57600080fd5b6101e3610522565b005b34156101f057600080fd5b6101f8610668565b6040518082815260200191505060405180910390f35b61021661066e565b005b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561029a57600080fd5b60008060028111156102a857fe5b600360149054906101000a900460ff1660028111156102c357fe5b1415156102cf57600080fd5b7f72c874aeff0b183a56e2b79c71b46e1aed4dee5e09862134b8821ba2fddbf8bf60405160405180910390a16002600360146101000a81548160ff0219169083600281111561031a57fe5b0217905550600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc6001549081150290604051600060405180830381858888f19350505050151561038357600080fd5b50565b60005481565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561040e57600080fd5b600180600281111561041c57fe5b600360149054906101000a900460ff16600281111561043757fe5b14151561044357600080fd5b7fe89152acd703c9d8c7d28829d443260b411454d45394e7995815140c8cbcbcf760405160405180910390a16002600360146101000a81548160ff0219169083600281111561048e57fe5b0217905550600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f19350505050151561050c57600080fd5b50565b600360149054906101000a900460ff1681565b600180600281111561053057fe5b600360149054906101000a900460ff16600281111561054b57fe5b14151561055757600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156105b357600080fd5b62278d0060045401421115156105c857600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f19350505050151561064157600080fd5b6002600360146101000a81548160ff0219169083600281111561066057fe5b021790555050565b60015481565b600080600281111561067c57fe5b600360149054906101000a900460ff16600281111561069757fe5b1415156106a357600080fd5b60005434148015156106b457600080fd5b7fd5d55c8a68912e9a110618df8d5e2e83b8d83211c57a8ddd1203df92885dc88160405160405180910390a14260048190555033600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600360146101000a81548160ff0219169083600281111561074757fe5b021790555050505600a165627a7a72305820d5114b3793f81efb23ccdd1b6d7237f337b13bb75cc42954373733d730fcadd00029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
});
}
I did some more investigation and tried to send ether via web3.js to smart contract's method. I used web3.eth.sendTransaction operation and passed ether via "value" attribute. However metamask is not showing the ether value. please find the code and screenshot. Appreciate any help.
function sendDeposit() {
web3.eth.getAccounts(function(error, result) {
web3.eth.sendTransaction(
{from:result[0],
to:"0x5333752f6d1007f8d4dabaca20afce38369de2a5",
value: "0x30D40", //200000 Wei
data: "0x06cb4bcd"
}, function(err, transactionHash) {
if (!err)
console.log(transactionHash);
});
});
}
Best Answer
Totally agree with @Rob Magier's answer,
Here is the method you can change your wei to ether,
Thanks, Hope this helps you. Happy Coding :-)