0

So I try to transfer the funds using below code its working in metamask extension its shows SEND Ether

amountToSent = "0.002"
web3.eth.sendTransaction(
    {from: ethereum.selectedAddress,to: receverAddress, value: web3.utils.toWei(amountToSent, "ether")},
    function(err, transactionHash) {
      if (err) {
            console.log('Payment failed', err);
            //$('#status').html('Payment failed');
        } else {
            console.log("Payment was successful... data returned is: ", transactionHash);
            
       }
    }
);

but I want to transfer funds using abi contacts I tried below code and when metamask extension open it shows TRANSFER transfer was success but wont received in receiver account

var abi = [...]

var web3 = new Web3(window.ethereum);
var amountToSent = "0.002"
var contract = new web3.eth.Contract(abi, receverAddress, , { from: ethereum.selectedAddress });

contract.methods.transfer(address,web3.utils.toWei(amountToSent, "ether") )
   .send().then(receipt=> {console.log(receipt)});

the amount which show in metamask in too big & I think I want to transfer ether I'm attaching the image of that

enter image description here

1 Answer 1

1

To call a contract function and send ether in the same transaction you have to use the value parameter from send().

var abi = [...]

var web3 = new Web3(window.ethereum);
var amountToSent = "0.002"
var contract = new web3.eth.Contract(abi, receverAddress, , { from: ethereum.selectedAddress });

contract.methods.transfer(address, param2)
   .send({
      from: ethereum.selectedAddress,
      value: web3.utils.toWei(amountToSent, "ether"),
   }).then(receipt=> {console.log(receipt)});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.