Metamask Issues: Unable to Transfer ETH from Smart Contract to User Account with Truffle and ReactJS

Metamask: Not able to transfer my ETH from the Smart Contract to an User Account while using Truffle or ReactJS with Web3

I recently ran into an issue while trying to transfer Ethereum (ETH) from a smart contract to a user account using Truffle and ReactJS. The issue lies in the way Metamask handles transactions between the smart contract and the user’s wallet.

Problem:

In my Solidity code, I am trying to use the “TransferETH” function provided by Truffle to transfer ETH from an Ethereum address stored in the smart contract’s storage to a user account. However, when I try to run this transaction using ReactJS, Metamask encounters issues and cannot complete the transfer.

Solution:

To resolve this issue, we need to use the Truffle function eth_sendTransaction instead of the TransferETH function. The latter option is deprecated in favor of eth_sendTransaction, which allows for more control over the transaction flow. Here is an updated version of my Solidity code that uses these changes:

`solidity

pragma solidity ^ 0,6,0;

contract MySmartContract {

// Define a mapping to store user account addresses and their corresponding ETH balances.

mapping(address => uint256) public userBalances;

function donateETH(address recipient, amount uint256) public payable {

require(amount > 0, "Invalid amount");

// Store the donation amount in the contract storage.

userBalances[recipient] = userBalances[recipient].add(amount);

// Send the donated ETH to the recipient address using theeth_sendTransactionfunction.

transferETH(recipient);

}

function transferETH(receiver address) public {

require(userBalances[msg.sender] >= 1 ether, "Insufficient balance");

// Create a transaction object using Truffle'seth_sendTransactionfunction.

Transaction in transaction memory = transaction(

msg.sender,

address(this),

Wei(1),

Wei("0x"),

0

);

// Sign the transaction with our private key and then send it to the network.

require(tx.signatures[0].from != "", "No signatures");

require(tx.hash, true);

txt.submit();

}

}


Using Truffle and ReactJS:

To use this updated contract in a ReactJS application, you will need to install thetufflelibrary and configure the Truffle provider. Here is an example of how to create a new project using Truffle:

bash

npm init truffle -y

npx truffle init

Create a new file calledmy_contract.sol, copy the Solidity code above into it, and then create a new directory structure:

bash

my_contract/

my_contract.sol

public/

User.sol

contract/

user/

__init__.js

In your ReactJS application, you can use theTruffleProvidercomponent from@truffle/core` to connect to the Truffle provider and interact with your smart contract:

jsx

import React, { useEffect } from 'react';

import { Provider } from 'truffle provider';

import Web3 from 'web3';

const App = () => {

const web3 = new Web3(window.ethereum);

useEffect(() => {

if (window.ethereum) {

web3.currentProvider.connect().then((account) => {

// Use the account address to transfer ETH to user accounts.

web3.currentProvider.eth.sendTransaction({

from: '0x', // Your contract address

to: '0x', // User wallet address

value: 1 ether, // Transfer amount

gasPrice: web3.utils.toWei('20', 'gwei') // Gas price

});

}).catch((error) => {

console.error(error);

});

}

}, []);

return (

Token Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *