Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new article #347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions apps/base-docs/base-camp/docs/transfer-eth/transfer-eth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: Transfer ETH in solidity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capitalize Solidity

description: A guide to understand different ways to transfer ETH through smart contracts
hide_table_of_contents: false
---

This article will delve into the three primary methods for sending Ether: transfer, send, and call.e'll discuss their differences, use cases, and the recommended practices.

---

## Objectives

By the end of this lesson, you should be able to:

- Userstand the different ways to transfer ETH
- Determine when to use each type appropriately in contract development
- Get insight about gas utilization and best practices, while transfering Ether

---

## Sending Ether
### Transfer
This method allows you to send Ether, consuming 2300 gas units. If the operation fails, it throws an error.

:::info
The receiving smart contract should have a fallback function defined or else the transfer call will throw an error. There is a gas limit of 2300 gas, which is enough to complete the transfer operation
:::

```solidity
function sendViaTransfer(address payable recipient) public payable {
recipient.transfer(msg.value); // Automatically reverts on failure
}
```

### Send
Similar to transfer, send also consumes 2300 gas units but returns a boolean value indicating the success or failure of the operation.

```solidity
function sendViaSend(address payable recipient) public payable {
bool sent = recipient.send(msg.value); // Returns false on failure
require(sent, "Failed to send Ether");
}
```

### Call
This method is more flexible, allowing you to forward all gas or set a specific gas limit. Like send, it returns a boolean value indicating the operation's success or failure

```solidity
function sendViaCall(address payable recipient) public payable {
(bool sent, bytes memory data) = recipient.call{gas :10000, value: msg.value}(""); // Returns false on failure
require(sent, "Failed to send Ether");
}
```

## Usage guidelines
The choice between transfer, send, and call depends on the specific requirements of your contract and the level of control you need over the transaction.

### Transfer
This was once the simplest and safest way to send Ether, as it automatically reverts the transaction if the call fails. However, it only forwards a fixed amount of gas (2300), which may not be enough for more complex operations in the receiving contract.

The gas limit of 2300 gas, which is enough to complete the transfer operation. It is hardcoded to prevent reentrancy attacks.

### Send
This method is similar to transfer but returns false instead of throwing an exception when the call fails. This allows you to handle the failure in your contract. However, like transfer , it also only forwards 2300 gas.

### Call
It allows you to forward all remaining gas or specify a certain amount. This flexibility makes it suitable for more complex operations. However, it also requires you to handle the possibility of reentrancy attacks and manually check the return value.

:::note
Call is the recommended way of sending ETH to a smart contract as of Solidity 0.6.0
:::

Using call, one can also trigger other functions defined in the contract and send a fixed amount of gas to execute the function.
```solidity
function sendViaCallExternalContract(address payable recipient) public payable {
(bool sent, bytes memory data) = recipient.call{ gas :10000, value: msg.value }
("func_signature(uint256 args)");
}
```
11 changes: 11 additions & 0 deletions apps/base-docs/base-camp/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,17 @@ const sidebars = {
},
],
},
{
type: 'category',
label: 'Transfer Ether',
items: [
{
type: 'doc',
id: 'docs/transfer-eth/transfer-eth',
className: 'sidebar-coding',
},
],
},
],
},
{
Expand Down
Loading