Payable function in Solidity – Example & How to use it?
Solidity is a high-level, object-oriented programming language for writing smart contracts in the Ethereum Blockchain. Smart contracts are used to manipulate the Ethereum Blockchain and govern the behavior of the accounts within the Ethereum Blockchain.
Solidity is highly influenced by Javascript, C++, and Python. It is designed to target the EVM (Ethereum Virtual Machine). The most recent Solidity version is 0.8x.
What is Payable in Solidity?
When writing a smart contract, you need to ensure that money is being sent to the contract and out of the contract as well. Payable does this for you, any function in Solidity with the modifier Payable ensures that the function can send and receive Ether. It can process transactions with non-zero Ether values and rejects any transactions with a zero Ether value. Additionally, if you want a function to process transactions and have not included the payable keyword in them the transaction will be automatically rejected. An example of this is supposing you have a receive() function with the payable modifier, this means that this function can receive money in the contract and now imagine there is a function send() without a payable modifier it will reject the transaction when you want to send money out of the contract.
Be a Web3 & Blockchain Developer
Fallback payable functions are also a big help in Solidity. Imagine if someone sends funds to your contract in a function without the payable modifier, you can define such function to have a fallback payable function which ensures that the transaction will go through regardless. This is why it is considered good practice to use some version of a function with noname and a payable modifier. Notice here the name of the function is ‘noname’ and not payable, payable is the modifier.
function *noname* () payable { }
Code language: JavaScript (javascript)
You can define a payable function using the following syntax:
function receive() payable {}
function send() payable {}
Code language: JavaScript (javascript)
As you can see the payable keyword is not a function but a modifier. Sometimes, people confuse it for a function and end up changing the meaning of the whole function causing the code to malfunction. As we specified before this about the ‘noname’ function, if someone tries calling another function without the payable modifier it acts a fallback and transfers the ether being sent to this noname function.
pragma solidity ^0.8.15;
contract payableSample {
//add the keyword payable to the state variable
address payable public Owner;
//set the owner to the msg.sender
constructor () public {
Owner = msg.sender;
}
//create a modifier that the msg.sender must be the owner modifier
onlyOwner() {
require(msg.sender == Owner, 'Not owner');
_;
}
//the owner can withdraw from the contract because payable was added to the state variable above
function withdraw (uint _amount) public onlyOwner {
Owner.transfer(_amount);
}
//to.transfer works because we made the address above payable.
function transfer(address payable _to, uint _amount) public onlyOwner {
_to.transfer(_amount);
//to.transfer works because we made the address above payable.
}
}
Code language: JavaScript (javascript)
As Web3.0 has just started to gain traction, many companies are ahead of the curve and have already started to adapt to the change and have started implementing Solidity in their development processes. Some of the top companies using Solidity are:
- LeewayHertz
- EngineerBabu
- TreeHouse Technology Group
- OpenGeek Slab
- Altoros
- AppInvetiv
- Abes Lab
- Arc Touch
Learn more about Payable function in Solidity, here.
How you can start learning Solidity via Codedamn?
Codedamn offers a concise learning path to help you get started with writing Smart Contracts in Solidity to help you build multiple projects in the Web3.0 space. You can learn about the Ethereum Blockchain, Solidity, Smart Contracts, MetaMask, Creating your own coin and launching it, ICO(Initial Coin Offering), etc.
You can access the course here
Codedamn’s Solidity related material
Solidity Fundamentals
- What is Solidity?
- Remix IDE
- Solidity Compilation Steps.
- Mainnet vs Testnet
Solidity Programming
- Say “Hello World” in Solidity
- Contract Development Environment
- Solidity Sample Program
- State Variables
- Local Variables
- Functions
- Create functions
- Pass an argument to the function
- View vs Pure
- Constructor
- Integers
- Strings
- Storage vs Memory vs Stack on EVM
- If Else
- Booleans
- Fixed Size Array
- Dynamic Size Array
- Fixed (byte) Size Array
- Dynamic (byte ) Size Array
- Dynamic Array
- Loops
- Storage program
- Struct
- Enum
- Mappings
- Mappings Struct
- Global Variables (Special functions and variables)
Solidity Advanced Concepts
- Inheritance
- Abstract Contracts
- Interface
- Polymorphism
- Visibility
- Require
- Modifier
- Payable Function
- Payable Address
- Fallback Function
There are many practice labs that you can use to try out the recent concepts you have learned along the way!!
Where to write Solidity Code?
You can use Codedamn’s Solidity Online Compiler (Playground). PlayGround lets you write and edit Solidity code which you can easily run and compile right in the browser. You just need to type your code and click on the Run Code
button on the bottom left of the screen and the output of your code will be displayed in the terminal. You can use the Codedamn Playground to write Smart Contracts for your Web3 projects as well.
Codedamn Compiler opens up a docker container in the backend of the website which then uses WebSocket to verify your code and then help run the code in the background and display the output to you in the terminal.
In simple terms, it opens a separate Linux computer in the background which compiles your Solidity Code checks for any errors or problems in your code, and shows the output to you on your computer in the Terminal of the Codedamn playground. Codedamn playground uses solc
, which has been rated as the best compiler for Solidity.
Making use of solc
compiles your code and displays the output in a matter of a few seconds. It produces various outputs ranging from assemblies and simple binaries over an abstract syntax tree to estimations of gas usage.
Why use Playground Compiler?
Installing a separate code editor for only one specific language can be a hassle. Compiling your code on Codedamn Playground is very easy as it opens up another computer for you that does all the work in the background without making your own Computer Lag and also compiles it faster than any other compiler available anywhere. The ease of use is another crucial factor that ensures that all your files are in one place and are always safe, due to the AutoSave function which saves every line of code you write ensuring that you never lose your work.
Sharing is caring
Did you like what Kshitij wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: