Select Page

Understanding How To Use Payable In Solidity

by | Dec 31, 2022

Payable in solidity is a very common modifier that you will see. In this post, we break down what it is and how to use it with code examples.

The Payable Modifier

In Solidity, the payable modifier is used to indicate that a function can accept ether (the native cryptocurrency of the Ethereum platform) as a parameter. Functions that are marked as payable can be called with a transaction that includes a value, which will be transferred to the contract and stored in the contract’s balance.

Here is an example of a payable function in Solidity:

pragma solidity ^0.5.0;

contract MyContract {
    function myFunction() public payable {
        // This function can accept ether as a parameter.
    }
}

In this example, the myFunction function is marked as payable, which means that it can be called with a transaction that includes a value. The value will be transferred to the contract and stored in the contract’s balance.

It is important to note that only functions marked as payable can accept ether as a parameter. If a function is not marked as payable, it will not be able to accept ether and any attempt to call it with a value will result in an error.

Overall, the payable keyword is an important part of the Solidity language and is used to enable contracts to accept and store ether.

Address Payable

In Solidity, the “address payable” type is a variant of the “address” type that includes additional functionality for sending and receiving Ether (the native cryptocurrency of the Ethereum blockchain).

An “address payable” variable is a state variable (a variable that is stored on the Ethereum blockchain and can be accessed by contract functions) that holds the address of an Ethereum account. It is similar to a regular “address” variable, but it includes additional functions for interacting with the account’s balance.

Here is an example of an “address payable” variable in Solidity:

pragma solidity ^0.6.12;

contract Example {
  // Declare an address payable variable
  address payable public beneficiary;

  function setBeneficiary(address payable _beneficiary) public {
    // Set the beneficiary variable to the provided address
    beneficiary = _beneficiary;
  }

  function sendFunds() public {
    // Send Ether to the beneficiary address
    beneficiary.transfer(1 ether);
  }
}

The “address payable” type is useful for interacting with Ethereum accounts and sending and receiving Ether within a smart contract. It is recommended for anyone working with Ethereum smart contracts.

Looking for more Solidity content?

Best Smart Contract Security Audit Teams

Best Smart Contract Security Audit Teams

Smart contract technology is revolutionizing the way we conduct business and transfer value online. One of the most important aspects of using smart contracts is ensuring their security and reliability. There are many smart contract security audit teams, but which one...

The Best ERC-20 Wallet For Developers

The Best ERC-20 Wallet For Developers

Ethereum is a decentralized platform that enables the creation of smart contracts and decentralized applications (dApps). The Ethereum platform uses its own cryptocurrency, Ether (ETH), to facilitate transactions and execute smart contracts. One of the most popular...

How To Perform A Web3 Security Risk Assessment

How To Perform A Web3 Security Risk Assessment

Performing a web3 security risk assessment is an essential step when running a Web3 product. Many companies fail to perform the proper security steps. Often, they get hacked and lose customer data and funds. In order to prevent your project from getting hacked, we...

Our Recommended Solidity Security Audit Process

Our Recommended Solidity Security Audit Process

A Solidity security audit is a vital step in deploying smart contracts. If you choose not to have your contracts audited, you are putting your community and reputation at risk. However, many audit firms do not perform the proper due diligence on your smart contracts....

What Is The Ethereum Virtual Machine?

What Is The Ethereum Virtual Machine?

The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts on the Ethereum blockchain. It is a software-based virtual machine that can execute code in the form of smart contracts on the Ethereum network. The EVM is designed to be...

Complete Guide To Learn Solidity

Complete Guide To Learn Solidity

Ready to learn Solidity? Whether you are a smart contract developer or simply want to add better understanding of the programming language to your toolbelt, this guide provides everything that you need to get started. Ready to learn Solidity? What is Solidity?...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *