Trade guide

Do not send eth directly to an Ethereum contract

In general, the Ethereum smart contract coding system allows you to receive or send tokens through them. But there are bigger problems. For example, you can not send eth directly to a smart contract, and you have to send and receive tokens instead, but what is the reason for this error, and how can we fix it?

Each Ethereum contract can be coded so that ETH can be sent to the contract address. This method makes the operation of Ethereum Smart Contract easier, and there is no need for different applications to work with Smart Contract.

Do not send eth directly to an Ethereum contract

But just as you can enable this feature by embedding a line of code in a smart contract, we need to keep in mind that sending ETH directly to a contract is not the right thing to do.

According to solidity documents, when you enable this code snippet, more than 2300 gases are required to send each ETH to Ethereum Smart Contract. So this becomes one of the most costly smart contract processes.

The error when sending ETH to an Ethereum contract address is also due to default contracts called ECRECOVER. The fallback process in ECRECOVER requires more than 2300 gases to carry out ETH transmission operations. So you better not do that and only use tokens for smart contracts.

How to send ether to an Ethereum Contract?

You can use buyer, seller, and codes to implement ETH submission codes to an Ethereum contract. An variable called Wei is the default value for keeping assets in the smart contract, so try to do unit conversions wherever necessary.

To send ETH to the contract, the buyer must purchase the asset with ether. The contract requires the holder to hold the asset until it is received.

After confirming the purchase and receiving the asset, this amount is released to the seller. The transfer of ETH from the contract to a specific address takes place.

If the buyer does not receive the asset, the contract returns the same ETH sent to the buyer. In this case, blockchain will send the ETH to an address from the contract.

Transfer ether to the contract

1. Have a Payable fallback method.

function () payable {
}

2. If you do not want to make any sales in a smart contract, you do not need to write these codes, and you only use the contract to maintain the assets. In general, buying and selling ETH and sending it to the contract means sending from an address to the contract or vice versa.

function buyCollectible (uint _certNum)

payable

returns (bool success) {

   collStructs [_certNum] .status = “paid”;

   collStructs [_certNum] .buyerAddress = msg.sender;

    return true;

}

By calling the buyCollectible function, buyers can buy the desired amount of assets from the smart contract.

coll.buyCollectible (2, {from: web3.eth.accounts [3], value: web3.toWei (10, ‘ether’)})

You can check the balance of the contract -

web3.fromWei (web3.eth.getBalance (contractAddress), ‘ether’)

.toString ()

Transfer ether from contract to an address

1. The simplest way to send ETH from a contract to an address. You must use the transfer function in the smart contract with the initial amount of payment. The payment is stored in Wei, and you have to convert it to an ETH token using the conversion functions.

function confirmCollRecv (uint _certNum)

public

returns (bool) {

    destinationAddress.transfer (collStructs [_certNum] .price);

    collStructs [_certNum] .status = “received”;

    return true;

}

2. The buyer shows the receipt of the asset, and then the amounts go from the smart contract to the seller’s address.

coll.confirmCollRecv (2, {from: web3.eth.accounts [3]})

Also, check the number of assets in the smart contract and the number of transfers using the following functions in Web3.

web3.fromWei (web3.eth.getBalance (contractAddress), ‘ether’)

.toString ()

web3.fromWei (web3.eth.getBalance (destinationAddress), ‘ether’)

.toString ()

What is your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
Reza Siavashi is a seasoned marketing professional with over seven years of experience, specializing in social media marketing, digital advertising, content strategy, and marketing analytics. He holds an MBA in Commercial Management and is known for his creative and forward-thinking approach. Reza is passionate about ethical marketing and social responsibility, and is currently exploring opportunities that align with these values.

    You may also like

    Comments are closed.

    More in:Trade guide