SoFunction
Updated on 2025-04-07

Detailed explanation of the use example of Solidity payment function

introduction

Learn about payable functions in Solidity, learn how important they are in handling Ether deposits, and how to create and use them in smart contracts.

Through this guide you should be able to:

  • Understand the purpose and usage of payable functions in Solidity
  • Learn how to send Ether to smart contracts
  • Write payable functions in Solidity
  • Create a revocable payable function with conditions
  • Implement custom logic in payable functions
  • Understand message calls and their relevance in Ethereum Virtual Machine (EVM)

Payable function

In Solidity, a payable function is a function that allows smart contracts to accept Ether. They help developers manage received ether and act upon receipt. For example, a simple payable function can collect Ether donations for fundraising activities.

Here is a basic code example:

pragma solidity ^0.8.0;
contract Fundraiser {
    function donate() external payable {
        // Ether is received and stored in the contract's balance
        // You can perform any other actions with the Ether received here - for example, sending it to some other address etc.
    }
}

In this example, when callingdonateWhen a function it accepts the Ether sent by the donor and adds it to the balance of the contract.

KeywordspayableAllows someone to send ether to the contract and run code to process this deposit.

This code may log events, modify the storage to record the deposit, or may even cancel the transaction if you choose to do so.

When the developer explicitly tags aSmart contracts with payable typesWhen they say "I want to send ether to this function". To understand why this is important, imagine how bad it would be if someone sent ether to a contract but the developer didn't write code to handle the event. In that case, ether may be permanently locked, or cannot be extracted by its intended recipient.

How to send ether coins to smart contracts

Sending Ether is a local feature of the Ethereum Virtual Machine (EVM). This is different from any other transfer in EVM, which requires the developer to write custom logic to handle the transfer (such as NFT or ERC20).

When someone sends Ether to a smart contract, they operate through the "value" field of the transaction itself. Let's take a look at the transaction content in JSON format:

{
    "to": "0x5baf84167cad405ce7b2e8458af73975f9489291",
    "value": "0xb1a2bc2ec50000", // 1 ether 
    "data": "0xd0e30db0" // deposit() 
    // ... other properties
}

This transaction sends 1 Ether to the address0x5baf84167cad405ce7b2e8458af73975f9489291

If this address is a smart contract, it will try to parse calldata(data) to determine which smart contract function this user is trying to call (in this case deposit()).

Depending on the situation where the function is paid or non-payment, one of two things will occur:

  • If the function is a payable function, then it will run the logic.
  • If the function is unpaid, the transaction will be restored and the funds will be returned minus the gas cost of the transaction.

What is an example of a Solidity payable function?

Here is an example of a basic payable function using Solidity, including the "deposit" function:

function deposit() payable external {
    // no need to write anything here!
}

Note that in this case, we are not indepositWrite any code in the function body. Writing a payable function is enough to receive ether, you probably don't need to write any logic.

For example, if this is a payable smart contract, controlled by a charity, accepting cryptocurrency donations, maybe the user just needs to calldeposit, charities will eventually be able to withdraw these donations to the address of their choice. In this case, write areceiveFunctions may be better:

receive() external payable {
    // this built-in function doesn't require any calldata,
    // it will get called if the data field is empty and 
    // the value field is not empty.
    // this allows the smart contract to receive ether just like a 
    // regular user account controlled by a private key would.
}

What is an inversion example of a Solidity payable function?

A payable smart contract function can be rolled back. Here is an example of a rollback payable function that uses two require statements to verifyandbalances[]

mapping(address => uint) balances;
function deposit() payable external {
    // deposit sizes are restricted to 1 ether
    require( == 1 ether);
    // an address cannot deposit twice
    require(balances[] == 0);
    balances[] += ;
}

If anyrequireIf the statement is not true, the transaction will be rolled back and the sender will recover their funds.

Why do we write logic in payable functions?

If we have a smart contract that needs to track who deposited which Ether coins, we can track it in storage:

mapping(address => uint) balances;
function deposit() payable external {
    // record the value sent 
    // to the address that sent it
    balances[] += ;
}

HereCorresponding to the transactions we saw in the "How to Send Ether" sectionvalueField. As Solidity developers, we can use message values ​​to record deposits and map them to some internal balances at that transaction address.

Why is it called?

In Ethereum Virtual Machine (EVM), interaction with smart contracts is called "message calls". This applies to situations where the user calls a smart contract directly, and also to situations where the smart contract calls another smart contract (internal transaction).

Payable Solidity Function

In short, a payable function is a function that can receive ether. It provides developers with the opportunity to respond to Ether deposits for recording or any other necessary logic.

The above is the detailed explanation of the use of Solidity payable functions. For more information about Solidity payable functions, please follow my other related articles!