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

Problems with Metamask: He is unable to transfer ETH from an intelligent contract to the user’s account with Truffa and Reactjs

I recently encountered a problem while trying to transfer Ethereum (ETH) from an intelligent contract to a user account using truffles and reactjs. The problem is that Metamask supports transactions between an intelligent contract and the user’s portfolio.

problem:

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 try to use the “transferreth” function provided by Truffle to transfer ETH from the address of the Ethereum address stored in the memory of an intelligent contract on the user’s account. However, when I try to perform this transaction using Reactjs, Metamask encounters problems and does not complete the transfer.

Solution:

To solve this problem, we must use the “ETH_SendTransaction” function with truffle instead of “Transferth”. The latter is outdated in favor of “Eth_sendTransaction”, which allows for greater control over the flow of the transaction. Here is an updated version of my solidity code that uses these changes:

`Solidity

Pragma solidity ^0.6.0;

MYSMARTCONTRACT agreement {

// Define mapping to store the user account addresses and the corresponding ETH balance.

mapping (address => uint256) public balance of users;

The function transfers a donation (address recipient, uint256 amount) public public {

requires (amount> 0, "incorrect amount");

// Keep the donation amount in the contract warehouse.

SerSerbalales [recipient] = bolales [recipient] .add (amount);

// Send ETH donations to the recipient's address using the "eth_sendransation" function.

transfer (recipient);

}

Public sending function (address) {

Require (Bolaleski Sersg.Sender]> = 1 Ether, "insufficient balance");

// Create a transaction object using theETH_SEDTRANSACTION ‘Truffle function.

Transaction memory transaction = transaction (

msg.sender,

address (this),

Wei (1),

Wei (“0x”),

0

);

// sign the transaction with our private key and then broadcast it to the network.

requires (tx.Signatures [0]. From! = “”, “without signatures”);

Requires (tx.hash, True);

tx.submit ();

}

}

`

using truffles and reactjs:

To use this updated contract in the Reactjs application, you need to install the “Truffle” library and configure the truffle supplier. Here is an example of creating a new project using truffles:

`Bash

NPM Init Truffle -y

Npx truffle inteit

Create a new file called My_Contract.sol, copy to it the above solidity code to it, and then create a new catalog structure:

`Bash

My_Contract/

my_contract.sol

public/

User.sol

contracts/

user/

__init __. JS

In the Reactjs application, you can then use the “TruffProvider” component with @@ Truffle/Core “to connect to the truffle supplier and interact with an intelligent contract:

“ Jsx

Import React, {useeffect} with “react”;

Import {Supplier} from “Truffle-Provider”;

import web3 with “web3”;

const app = () => {

Const Web3 = New Web3 (Window.ethereum);

Useeffect (() => {

if (window.ethereum) {

web3.currentprovider.connect (). Then ((concert) => {

// Use the account address to send ETH to user accounts.

web3.currentprovider.eth.sendTransaction ({{

From: “0x”, // your contract address

to: “0x”, // User portfolio address

Value: 1 ether, // transfer amount

Gasprice: web3.utils.towei (’20 ‘,’ gwei ‘) // gas price

});

}). catch ((error) => {

console.error (error);

});

}

}, []);

return (

ethereum there bitcoins ethereum


Comentários

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *