PAYABLE FUNCTIONS IN SOLIDITY

TABLE OF CONTENT

  • Introduction
  • Definition
  • Syntax: block of codes explaining payable function
  • Use Case: how can it be applied?
  • Summary
  • Glossary
  • Reference

INTRODUCTION

Here i would shortly give a detailed explanation of what a payable function is. We need to understand that Solidity provides us with a special modifier (payable) among others, which gives us the possibility of sending money in to function. In this article, the payable function will be explicitly explained to solidify your knowledge about payable modifiers.

DEFINITION

The payable modifier is added to a function such that it behaves in a particular way. Therefore, a Payable Function is a special type of function that can receive ether. It enables us send ether to a contract after it’s been called. This type of function is what makes Solidity and Ethereum so interesting.

This is what a payable function looks like.

payable function
payable function

Lets simplify this altogether. In a traditional settings, when you call a function on the normal web server, you cant send funds along with your function. This can only happen in Ethereum because money(ether), the data and contract code is all live on the Ethereum blockchain, which makes it possible to send ether to the contract at the same time.

USE CASE AND SYNTAX

Having explained the concept above, i would for example sake create a smart contract, a Marketplace for NFTs , and in this contract , users would practically buy NFTs. So we would be creating a function to buyNFT which takes a payable modifier. Taking it further, we would want to ensure that the money(ether) sent is equals to the NFT price. If this condition have been met, a transferNft function is called that then sends the NFT to the buyer (msg.sender: this is the sender of the transaction);

SUMMARY

In this article, i have been able to explain the payable modifier and its effects when used in a function. We have also looked at an example where it’s applicable. So basically, a payable function helps us receive ether, send ether to a contract on call.

GLOSSARY

  • Smart Contract: is a computerized code that executes all or parts of an agreement and it’s stored on the blockchain
  • Ether: this is the native cryptocurrency of the Ethereum blockchain.
  • msg.sender: This is the sender of the transaction i.e the originating account of the transaction.
  • msg.value: This shows how much ether was sent to a contract.
  • NFTs: Non-fungible tokens or NFTs are cryptographic assets on a blockchain with unique identification codes and metadata that distinguish them from each other

REFERENCES

Briefs

My next article would be on Modifiers in Solidity.. So watch out!!!

Hope you were able to get value out of this article! Thanks for reading and stay glued for more!!!

Subscribe to 0x9ae1…Fe35
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.