Metamask Problems: Unable to transfer eth from smart contract to user account with truffle and reactjs
Recently, I met a problem while trying to transfer the ethereum (ETH) to an intelligent contract to a user account using truffle and reactjs. The problem lies in the way metamask manages transactions between the smart contract and the user’s wallet.
The question:
In my solidity code, I try to use the “handover” feature provided by the truffle to transfer eth from ethereum to the user account stored in the smart contract storage. However, when I try to execute this transaction with reactjs, metamask has problems and does not complete the transfer.
The Solution:
To solve this problem, you need to use the ETH_Sendtransection function instead of the truffle instead of the “transfer”. The later is obsolete in favor of eth_sendtransection, which allows for greater control of the transaction flow. Here’s an updated version of My Solidity Code that takes Advantage of These Changes:
`Solidity
Pragma Solidity ^0.6.0;
Contract MysmartContract {
// determine a mapping to store user account addresses and their appropriate eth balances.
mapping (address => uint256) Public User Balance;
Doneateeth Feature (Address Recipient, Uint256 Amount) Public Pay {
Claim (Amount> 0, "Invalid quantity");
// store the amount of the donation in the storage of the contract.
User equations [recipient] = user equation [recipient] .add (SUM);
// send the donated eth to the addressed address using theEth_sendtransectionFunction.
transfer (recipient);
}
Feature Transfereth (Address Recipient) Public {
is required (user equations [msg.sender]> = 1 ether, "insufficient balance");
// create a transaction object with the "ETH_Sendtransection" feature of the truffle.
Transaction Memory Transaction = Transaction (
Msg.sender,
Title (this),
Wei (1),
Wei ("0x"),
0
);
// sign the transaction with our private key and then broadcast it to the network.
You need (tx.Signatures [0] .from! = "", "No signatures");
Need (tx.hash, true);
tx.submit ();
}
}
Use of Truffles and Reactjek:
To use the updated contract, you need to install the “truffle” directory in angle application and set up a truffle provider. HERE’S AN example of how to create a new project with the help of Truffle:
`Bash
Npm init truffle -Y
Npx truffle init
`
Create a new file called My_contract.sol
, copy the above strength code, and then create a new library structure:
`Bash
My_contract/
my_contract.sol
public/
User.sol
Contracts/
User/
__init __. S
`
You can use reactjs to use the “Truffleprovider” ingredient @@ core" to connect to the Truffle Service Provider and to keep in touch with the smart contract:
` Jsx
Import react, {useefect} from ‘react’;
Import {Service Provider} from the “Truffle Service Provider”;
Import Web3 from ‘Web3’;
Const app = () => {{{{{
Const Web3 = New Web3 (Window.ethereum);
Useefect (() => {{{
if (window.ethereum) {
Web3.currentprovider.connect (). Then ((account) => {{{{{{
// use the account address to transfer eth to user accounts.
Web3.currentprovider.eth.sendtransection ({{
Sender: ‘0x’, // title of the contract
To: ‘0x’, // User’s Wallet Address
Value: 1 ether, // quantity transmission
GasSrice: Web3.utils.towei (’20 ‘,’ Gwei ‘) // Gas Price
});
}). catch ((error) => {{{{{
Console.error (Error);
});
}
}, []);
Return (