ReasonJun

Solidity : payable 본문

Blockchain/Solidity

Solidity : payable

ReasonJun 2023. 10. 18. 15:43
728x90

The Payable keyword in Solidity is used to declare a function or address that can receive Ether.

When you declare a function as payable, it means that the function can be called with Ether attached. The Ether that is sent to the function will be stored in the contract's balance and can be used by other functions in the contract.

 

You can also declare an address as payable. This means that Ether can be sent to that address. The Ether that is sent to the address will be stored in the contract's balance and can be used by other functions in the contract.

 

Here is an example of a payable function:

function deposit() public payable {
  // ...
}

This function can be called with Ether attached, and the Ether will be stored in the contract's balance.

Here is an example of a payable address:

address payable public owner;

Ether can be sent to this address, and the Ether will be stored in the contract's balance.

Payable functions and addresses are commonly used in smart contracts that need to receive or send Ether. For example, a smart contract that implements a decentralized exchange will need to have payable functions to allow users to deposit and withdraw Ether.

 

Here are some examples of how Payable is used in smart contracts:

  • A crowdfunding platform might use a payable function to allow users to contribute to a campaign.
  • A decentralized exchange might use payable functions to allow users to deposit and withdraw Ether and other tokens.
  • A non-fungible token (NFT) marketplace might use a payable function to allow users to purchase NFTs.

It is important to note that payable functions and addresses can be used for malicious purposes as well. For example, a scammer could create a smart contract with a payable function that promises to return high returns on investment, but then simply steal the Ether that is sent to the function.

 

It is important to be careful when interacting with payable functions and addresses, and to only interact with contracts that you trust.

 

https://docs.alchemy.com/docs/solidity-payable-functions

 

What are Payable Functions in Solidity?

Learn about payable functions in Solidity, their importance in handling Ether deposits, and how to create and use them in smart contracts.

docs.alchemy.com

 

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : transfer()  (0) 2023.10.18
Solidity : send()  (0) 2023.10.18
Solidity : try / catch  (0) 2023.10.17
Solidity : require  (0) 2023.10.17
Solidity : revert  (0) 2023.10.17
Comments