What if your smart contract could instantly accept money without any extra steps?
Why Payable functions in Blockchain / Solidity? - Purpose & Use Cases
Imagine you want to build a digital vending machine on the blockchain that accepts payments in cryptocurrency. Without a special way to receive money, your contract can't accept any funds, making it impossible to sell anything.
Trying to accept payments without payable functions means your contract will reject any incoming cryptocurrency. You'd have to manually track and verify payments off-chain, which is slow, error-prone, and breaks the trustless nature of blockchain.
Payable functions let your smart contract directly receive cryptocurrency when called. This makes it easy and secure to handle payments on-chain, ensuring your contract can sell digital goods or services seamlessly.
function buy() public { /* no way to accept funds */ }function buy() public payable { /* accepts payment with call */ }Payable functions enable smart contracts to securely receive and manage cryptocurrency payments directly on the blockchain.
A crowdfunding contract where supporters send Ether to fund a project; the payable function automatically accepts and records their contributions.
Without payable functions, contracts can't receive cryptocurrency.
Manual payment tracking is slow and unreliable.
Payable functions make on-chain payments easy and secure.
Practice
payable function in a blockchain smart contract?Solution
Step 1: Understand the role of
Thepayablepayablekeyword marks a function that can accept cryptocurrency (like Ether) when called.Step 2: Compare other options
Options A, C, and D describe unrelated behaviors. Only To allow the function to receive cryptocurrency along with the call correctly describes the purpose ofpayable.Final Answer:
To allow the function to receive cryptocurrency along with the call -> Option AQuick Check:
Payable = Receive money [OK]
- Thinking payable makes function faster
- Confusing payable with access control
- Believing payable prevents state changes
Solution
Step 1: Recall Solidity function declaration order
In Solidity, the visibility modifier (e.g.,public) comes beforepayablekeyword.Step 2: Check each option
function deposit() public payable {} uses correct order:function deposit() public payable {}. Options A, B, and D have incorrect keyword order or syntax.Final Answer:
function deposit() public payable {} -> Option DQuick Check:
Correct order: visibility then payable [OK]
- Placing payable before function keyword
- Switching order of public and payable
- Using payable as a function name
contract Test {
uint public balance;
function deposit() public payable {
balance = msg.value;
}
}Solution
Step 1: Understand
msg.valuein Soliditymsg.valueholds the amount of wei (smallest Ether unit) sent with the transaction. 2 Ether equals 2 * 10^18 wei = 2000000000000000000 wei.Step 2: Analyze the assignment to
The function setsbalancebalance = msg.value, sobalancestores the wei amount sent, which is 2000000000000000000.Final Answer:
balance will be 2000000000000000000 (2 Ether in wei) -> Option AQuick Check:
msg.value stores wei amount sent [OK]
- Assuming msg.value is in Ether
- Expecting balance to be zero
- Thinking function won't compile
function receiveFunds() public {
require(msg.value > 0, "Send some Ether");
}Solution
Step 1: Check function declaration for payable
To receive Ether, a function must be declared with thepayablekeyword. This function lacks it.Step 2: Validate other options
The require condition is valid, visibility can be public, andmsg.valueis accessible in payable functions.Final Answer:
Function must be marked payable to receive Ether -> Option BQuick Check:
Missing payable causes Ether rejection [OK]
- Forgetting payable keyword
- Misusing require condition
- Changing visibility unnecessarily
Solution
Step 1: Check for payable keyword and require condition
function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } correctly marks the function aspayableand usesrequire(msg.value >= 1 ether)to enforce minimum Ether sent.Step 2: Validate balance update and syntax
function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } updatesbalances[msg.sender]by addingmsg.value. Other options either misspayable, use wrong comparison, or incorrect balance update.Final Answer:
function deposit() public payable { require(msg.value >= 1 ether, "Minimum 1 Ether required"); balances[msg.sender] += msg.value; } -> Option CQuick Check:
Payable + require with 1 ether check [OK]
- Omitting payable keyword
- Using incorrect Ether comparison
- Not updating balance correctly
Ready to prove it?
Pass the test and this pattern moves unknown
