0
votes

I have a function in my smart contract as follows:

function transferToaddress(address payable addressToSend) public payable{
    addressToSend.transfer(0.5 ether);
}

From my front-end, I'm calling the above function as follows:

var response = await this.contract.transferToaddress(vm.userAccountAddress,{
   from: vm.userAccountAddress,
   gas: vm.gas
})

The transaction fails. My contract has some balance. On a click of a button I want some ether (0.5 in this case) to get transferred from my smart contract to the address passed to the function. Any idea what am I doing wrong?

2

2 Answers

0
votes
var response = await this.contract.transferToaddress(vm.userAccountAddress)
.send({ from: vm.userAccountAddress, gas: vm.gas })

You missed the send keyword

0
votes

Contract should not be payable as user is getting ether and it's not supplying ether.

function transferToaddress(address payable addressToSend) public{
    addressToSend.transfer(0.5 ether);
}

var response = await this.contract.transferToaddress(vm.userAccountAddress)
    .send({ from: vm.userAccountAddress, gas: vm.gas })