Metamask Issues: Unable to transfer ETH from smart contract to user account with Truffle and ReactJS
I recently encountered an issue when trying to transfer Ethereum (ETH) from a smart contract to a user account using Truffle and ReactJS. The problem lies in the way Metamask handles transactions between a smart contract and a user’s wallet.
The Issue:
In my Solidity code, I am trying to use the TransferETH function provided by Truffle to transfer ETH from an Ethereum address stored in a smart contract’s storage to a user account. However, when I try to execute this transaction using ReactJS, Metamask encounters issues and is unable to complete the transfer.
The 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 more control over the transaction flow. Here is an updated version of my Solidity code that takes advantage of these changes:
pragma solidity ^0.6.0;
contract MySmartContract { {
// Define a mapping to store user account addresses and their corresponding ETH balances.
mapping(address => uint256) public userBalance;
function donateETH(recipient address, uint256 amount) public payable { ;
require(amount > 0, "Invalid amount");
// Store the donation amount in the contract storage.
userBalance[container] = userBalance[container].add(amount);
// Send the donated ETH to the recipient address using the eth_sendTransaction function.
transferETH(recipient);
} }
function transferETH(recipient address) public { ;
require(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),
Blank(1),
This("0x"),
0
);
// Sign the transaction with our private key and then broadcast it to the network.
require(tx.signatures[0].from != "", "No signature");
require( tx . hash , true ) ;
tx.submit();
} }
} }
Using Truffle and ReactJS:
To use this updated contract in a ReactJS application, you will need to install the truffle library and configure a Truffle provider. Here’s an example of how to create a new project using Truffle:
npm init truffle -y
npx truffle initialization
Create a new file called my_contract.sol, copy the Solidity code above into it, and create a new directory structure:
my_contract/
my_contract.sol
public/
User.sol
contracts/
user/
__init__.js
In your ReactJS application, you can use the TruffleProvider component from @truffle/core to connect to your Truffle provider and interact with your smart contract:
“`jsx
import React, {useEffect} from ‘react’;
import {Provider} from ‘truffle-provider’;
import Web3 from ‘web3’;
const Application = () => {
const web3 = new Web3(window. ethereum);
useEffect(() => {
if (window. ethereum) {
web3.currentProvider.connect().then((account) => {
// Use account address to transfer ETH to user accounts.
web3.currentProvider.eth.sendTransaction({
from: ‘0x’, // Your contract address
to: ‘0x’, // User’s wallet address
amount: 1 ether, // Transfer amount
gasPrice: web3.utils.toWei(’20’, ‘wei’) // Gas price
});
}).catch((error) => {
console.error(error);
});
} }
}, []);
return (
< Provider provider = { web3