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

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

Subject:

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

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 memory to the user account. However, when I try to perform this transaction using ReactJS, Metamask encounters issues and does not complete the transfer.

Solution:

To resolve this issue, we need to use Truffle’s eth_sendTransaction function instead of TransferETH. The latter has been deprecated in favor of eth_sendTransaction', which allows for better control over the transaction flow. Here is an updated version of my Solidity code that takes advantage of these changes:

pragma strength ^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(recipientAddress, uint256 amount) 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 the "eth_sendTransaction" function.

transferETH(recipient);

}

function transferETH(recipient address) public {

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

// Create a transaction object using Truffle's eth_sendTransaction function.

Transaction memory transaction = Transaction(

msg.sender,

address(this),

Wei (1),

Wei("0x"),

0

);

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

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

require(tx.hash, true);

tx.submit();

}

}

Using Truffle and ReactJS:

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

npm init truffle -y

npx truffle init

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

my_contract/

my_contract.sol

public/

User.sol

contracts/

user/

__init__.js

In your ReactJS application, you can then use the @truffle/core component with the 'TruffleProvider' component to connect to your Truffle provider and interact with the smart contract:

“ jsx

import React, { useEffect } from ‘react’;

import { Provider } from ‘truffleprovider’;

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 (

INTEGRATE YOUR CRYPTOCURRENCY STRATEGY