CRYPTOCURRENCY

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

Metamask Problems: Not to transfer ETH transfer from intelligent contract to user account with truffle and Reactj

Recently, I met the problem when trying to transfer Ethereum (ETH) from an intelligent contract to a user account with Truffel and Reactj. The problem is the way Metamask controls the intelligent contract and the user’s wallet.

Problem:

In my attempt to use Truffel’s transmission function, in my firm codes, with the Ethereum address, which is archived in an intelligent contract archive with a user account. However, when I try to perform this event with Reactj, Metamask encounters problems and cannot carry out a transfer.

Solution:

In order to solve this problem, we must use the ‘eh_sendransation’ of the truffle instead of ”. The latter is outdated for the benefit of `eh_sendransotion ', which allows for better control of the flow of trade. Here's an updated version of my solidity code that uses these changes:

The firmness

Pragman solidity ^0.6.0;

MySmartContratct {Agreement

// Set the mapping of a user account and equivalent ETH sales addresses.

Mapping (address => UINT256) public users;

Donate the action (address of the recipient, sum uint256) to pay public (

request (quantity> 0, “no -valid quantity”);

// Remember the amount of donation in the agreement in the archive.

Userbalances [recipient] = user user [recipient] .adad (quantity);

// Send eth Donato to the recipient’s address using eh_sendransation.

transfer (recipient);

}

Function transferth (address of the recipient) Public {

request (user [msg.seender]> = 1 ether, “inadequate balance”);

// Create a transaction object using a trusffel et_sendraSation ‘.

Memory Traffic Event = Event (

msg.sender,

Address (this),

Wei (1),

Wei (“0x”),

0 –

)

// sign the event with a private key and then sends it to the network.

request (tx.signures [0]. From! = “”, “No signature”);

request (tx.hash, true);

Tx.submit ();

}

}

`

Use of Tryffel and Reactj:

In order to use this updated contract in the Reactjs application, you need to install the "Truffel" library and set the truffle supplier. Here's an example of how to create a new project with truffle:

Bash

NPM INIT TRUFF -Y

Truffle init npx

`

Create a new file namedmy_conttract.sol, copy the solid code above and therefore create a new directory structure:

Bash

My_conttract/

My_contract.sol

public/

User.Sol

contracts/

user/

__init __. JS

`

You can therefore use the "Scaming component"@truffel/core in your Reactjs application to connect to the truffle supplier and interact with an intelligent contract:

`Jsx

Import reacts, {useeffect} ‘react’;

Bring {Service Provider} from the Tartofo Provider;

Bring Web3s “Web3”;

Const app = () => {

Const Web3 = New Web3 (Window.ethereum);

Useeffect (() => {

If (Window.ethereum) {

Web3.currentprosider.connect (). Then ((account) => {

// Transfer ETH accounts to user accounts.

Web3.currentprosider.eth.sendtransation ({{

Sender: ‘0x’, // Your Contract address

A: ‘0x’, // User portfolio address

Value: 1 Ether, // Transfer

Gasprice: web3.utils.towei (’20 ‘,’ Gwei ‘) //

});

}). Catch ((error) => {

Console.error (error);

});

}

}, []);

return (

Deixe um comentário

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