Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Payable
  • Payable Address
  • Payable Types
  1. STEP 4

32: Payable

Payable

Payable is a keyword in Solidity that enables a function to receive Ether. When declaring a function as payable, it indicates that the function is able to receive Ether from external calls.

Examples of Payable

  • Example 1 This is an example of a payable function:

function sendEther() public payable {    // Function body}

In this example, the sendEther() function is declared as a payable function, meaning that it can receive Ether from external calls.

  • Example 2 This is an example of a payable function with a value parameter:

function sendEther(uint256 value) public payable {    // Function body}

In this example, the sendEther() function is declared as a payable function with a parameter of type uint256 representing the amount of Ether to be sent. When this function is called, the value parameter must be specified in order for the function to receive Ether.

Tips: for receiving ethers on/in smartcontract, use receive() here and/or fallback() here.


Payable Address

A payable address is an address on the Ethereum blockchain that can receive Ether (ETH) payments. It is important to note that regular addresses are not able to receive payments, and need to be converted into payable addresses to receive ETH.

Examples

  • Online Wallets Online wallets are a popular way to store, send and receive Ether. These wallets usually come with a feature that allows users to convert a regular address into a payable one. For example, Metamask offers a feature for converting an address into a payable address.

  • Smart Contracts Smart contracts are programs that can be deployed to the Ethereum blockchain. They can be used to create payable addresses that can receive payments from other users. For example, the following Solidity code creates a payable address that can receive Ether payments from other users:

contract PayableAddress {  
  address payable public payableAddress; 
  constructor() public {    payableAddress = msg.sender;  } 
  fallback() external payable {  // Do something with the received Ether  }
}
  • MyEtherWallet MyEtherWallet (MEW) is a web-based Ethereum wallet that allows users to easily create, manage and store Ether. It also allows users to convert a regular address into a payable address, which can be used to receive ETH. For example, users can use the Convert to Payable feature in MEW to convert an address into a payable one.


Payable Types

Payable types are a special type of data in Solidity that allows for sending Ether to and from smart contracts. This type of data can be used to facilitate transactions between users and contracts, and to enable the transfer of Ether to and from the contract.

Examples

  • Example 1: Sending Ether to the Contract The following example shows how to use the payable keyword to allow users to send Ether to a contract.

contract ExampleContract {    
  address public owner;        
  constructor() public {        owner = msg.sender;    }        
  fallback() external payable {  // Logic for contract goes here     }
}

In the above code, the fallback() is marked as payable. This means that users can send Ether to the contract, and the contract can access it.

Tips: function() in oldest version, like fallback() in newst versions

  • Example 2: Sending Ether from the Contract The following example shows how to use the payable keyword to allow a contract to send Ether to a user.

contract ExampleContract {    
  address public owner;    

  constructor() public {       
     owner = msg.sender;    
  }   

  function withdrawFunds() public payable {        
    address payable recipient = msg.sender;        
    recipient.transfer(msg.value);     
  }
}

In the above code, the withdrawFunds() is marked as payable. This means that when a user calls this function, the contract can send Ether to the user.

That's it for the lesson 32! In the next lesson, Type Data

Previous31: Abstract ContractNext33: Using type()

Last updated 1 year ago