🔶The Solidity burn() function
The burn()
function is a built-in function in the Solidity programming language that allows users to permanently destroy or remove tokens from circulation within a smart contract. This function is commonly used in token contracts to implement a mechanism for reducing the total supply of tokens, thereby creating scarcity and potentially increasing the value of the remaining tokens.
Syntax:
Parameters:
amount
(uint256): The amount of tokens to be burned. This parameter specifies the number of tokens to be permanently removed from circulation. It should be a non-negative integer value.
Return Type:
bool
: This function returns a boolean value indicating the success or failure of the burn operation. It returnstrue
if the burn is successful, andfalse
otherwise.
Modifiers:
public
: Theburn()
function is marked aspublic
, which means it can be called by any address interacting with the smart contract.
Function Details: The burn()
function is typically implemented within a token contract and is responsible for reducing the total supply of tokens by permanently removing a specified amount from circulation. When called, this function performs the following steps:
Checks and verifies that the caller of the function has a token balance equal to or greater than the amount they wish to burn. If the caller does not have a sufficient balance, the burn operation will fail, and the function will return
false
.Decreases the token balance of the caller by the specified amount. This step effectively subtracts the burned tokens from the caller's account.
Decreases the total supply of tokens held by the smart contract by the specified amount. This step ensures that the burned tokens are subtracted from the overall token supply.
Emits an event to notify the network and any subscribed listeners that a burn operation has taken place. The event typically includes relevant information such as the address of the caller, the amount of tokens burned, and any additional data deemed necessary.
Returns
true
to indicate that the burn operation was successful.
Example Usage:
In the example above, we have a token contract named MyToken
with a burn()
function. This function allows users to burn a specified amount of tokens from their own balance. The function checks if the caller's balance is sufficient, and if so, reduces the caller's balance and the total supply accordingly. It then emits a Burn
event to provide transparency about the burn operation.
Last updated