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

* Metamask problems: note to transfer ETH from an intelligent contract to groffle and reactjs *

The recently contemplated by the probability to transfer Ethereum (TH) from an intelligent consumer use of truffle and Reactjs. The probability of line in the way Metamask procedures can be a vein of intelligent contract and the user’s’us’.

**

Individual code, Thery to use the storage feature provid by truffle to transfer ETHI’s frafer to storage of an intelligence of an intelligent contract. Howver, white It to perform sis transaction sing Reactjs, Metamask counters problems and fakes to complete the transfer.

Decise:

To resolve this is probably the use of the TH_Sendransction’ feature of truffle instyle instya of 's transferre. The latter was drive in furniture of graphics of graphics, which reaching over the transaction flow. Take the right version of my solidity code to the changes:

Solidness

Pragma Solidity ^0.6.0;

Contract MySmartContract {

/// Determine app to store the user account and their corresponding ETH balls.

Mapping (addreate => Uint256) public use balances;

Function of Dari (Recipinate at Address, UINT256 Sub) Public Payble {

require (about> 0, "invalid amount");

// Keep the storage of the storage of the storage.

Users [recipent] = serbrations [recipent] .Add (amount);

// Sand the donated ETH to recipient’s addressing the ATEH_Sendansction ‘TH_Sendansction ‘Thature.

Transfereth (recipent);

}

Functional transfer (recipent to address) public {

require (usbalance [msg.sender] > = 1 ether, "insufficiency of balaance);

// Create a transaction object using the ATEH_Sendan’ on Truffle.

Transection memoory transaction = transaction (

mm.sender,

Address (this),

Wei (1),

Wei ("0x”),

0

),

// Sign the transaction with the private die and the broad range of the network.

require (tx.signatures [0] .from! = ""from! = ""from!" "no signatures");

require (tx.hash, tree);

tx.submit ();

}

}

* Using truffle and reactjs: *

To use this updated contract in the Reactjs application, you yuu will need new to install the Truffle library and set up a truffle provider. He was an exam of the exam to create and new project with the help of truffle:

bash

NPM INIT truffle -y

NPX TRUFFLE INIT

Create a file called ‘my_contrat., copy the lawyerbod code of solidness in touch, and the create creatment with the structure of the directory:

bash

My_CContract/

my_contract..sol

Publicly/

User

contracts/

User/

__init __. Js

Infected with the reactjs app, you use the ATUFLOVIDER component of from@truffle/core to contact your truffle your your mark:

jsx

Import React, {UREFEFEFCT} that by ‘React’;

Import {supplier} forf’ ‘truffle’;

Import Web3 is ‘Web3’;

CONST App =F () => {

CONST Web3 = New Web3 (Window.Thereum);

elected (() => {

If (Window.Thereum) {

web3. compressor.connected.connect (). The ((account) => {

// Use your address to transfer ETH to user accounts.

web but.urrently. it is sensory. sensors ({

From: ‘0x’, / The address of your contract

to: ‘0x’, / Use portfolio address

Value: 1 ether, // transfer amte

Gasprice: Web3.towei.towei.towei (’20 ‘,’ Gwei ‘) // The price of gas

^)

}). Catch (((error) => {

Consolate.orr (errorr);

^)

}

}, []);

return (

<Suppplier supplier = {web3.

testnet bull

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *