0
0
Blockchain / Solidityprogramming~30 mins

Sending Ether (transfer, send, call) in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Sending Ether (transfer, send, call)
📖 Scenario: You are creating a simple smart contract on Ethereum that can send Ether to another address. You will learn three ways to send Ether: transfer, send, and call. This is important for handling payments or sending funds securely.
🎯 Goal: Build a Solidity contract named EtherSender that can send Ether to a recipient address using transfer, send, and call methods. You will set up the contract, add a recipient address, implement the sending functions, and finally test sending Ether.
📋 What You'll Learn
Create a payable contract named EtherSender
Add a public address variable called recipient
Add a function to set the recipient address
Implement three functions to send Ether using transfer, send, and call
Add a function to receive Ether into the contract
Print or return success status for send and call methods
💡 Why This Matters
🌍 Real World
Sending Ether is a core operation in Ethereum smart contracts for payments, refunds, and transfers between users or contracts.
💼 Career
Understanding how to safely send Ether using transfer, send, and call is essential for blockchain developers to build secure and reliable decentralized applications.
Progress0 / 4 steps
1
Create the contract and recipient address
Create a Solidity contract named EtherSender. Inside it, declare a public address variable called recipient to store the address that will receive Ether.
Blockchain / Solidity
Need a hint?

Use contract EtherSender {} to start. Declare address public recipient; inside.

2
Add a function to set the recipient address
Add a public function named setRecipient that takes an address parameter called _recipient and sets the contract's recipient variable to this value.
Blockchain / Solidity
Need a hint?

Write a function setRecipient with parameter address _recipient. Inside, assign recipient = _recipient;.

3
Implement functions to send Ether using transfer, send, and call
Add three public payable functions: sendViaTransfer, sendViaSend, and sendViaCall. Each function should send the Ether sent to the contract to the recipient address using the respective method. For sendViaSend and sendViaCall, return a bool indicating success.
Blockchain / Solidity
Need a hint?

Use payable(recipient).transfer(msg.value); in sendViaTransfer. For sendViaSend, use bool sent = payable(recipient).send(msg.value); return sent;. For sendViaCall, use (bool sent, ) = payable(recipient).call{value: msg.value}(""); return sent;.

4
Add a receive function and test sending Ether
Add a receive external payable function to allow the contract to accept Ether. Then add a public view function named getBalance that returns the contract's Ether balance. Finally, print the balance after sending Ether using any of the sending functions.
Blockchain / Solidity
Need a hint?

Add receive() external payable {} to accept Ether. Add getBalance() to return address(this).balance.