Metamask: Unable to transfer my ETH from Smart Contract to a user account when using Truffle or ReactJS with Web3

Metamask Issues: Unable to Transfer ETH from Smart Contract to User Account using 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 is in how 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 storage to a user account. However, when I try to perform this operation 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 is no longer used, but “eth_sendTransaction” allows for better transaction flow management. Here is an updated version of the Solidity code that uses the following changes:

pragma durability ^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, uint256 amount) public paid {

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

// Store the donation amount in the contract archive.

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

// Send the donated ETH to the recipient address using the "eth_sendTransaction" function.

transferETH(recipient);

}

function transferETH(address recipient) public {

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

// Create a transaction object using the Truffle function "eth_sendTransaction".

Transaction memory transaction = Transaction(

message sender,

address (this),

Wei (1),

Wei ("0x"),

0

);

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

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

Require(tx.hash, true);

tx.submit();

}

}

Using Truffle and ReactJS:

To use this updated contract in your 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 create a new directory structure:

my_contract/

my_contract.sol

public/

User.sol

contracts/

user/

__init__.js

Then in your ReactJS application, you can use the @truffle/core TruffleProvider component to connect to the 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

recipient: ‘0x’, // User wallet address

value: 1 ether, // Transfer amount

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

});

}).catch((error) => {

console error (error);

});

}

}, []);

return (


Comments

Leave a Reply

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