___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Smart Contract for Token Distribution: Code and Explanation

BY: @alfredo24 | CREATED: Nov. 20, 2024, 12:19 p.m. | VOTES: 13 | PAYOUT: $0.14 | [ VOTE ]

[IMAGE: https://images.ecency.com/DQmPzFBMxBKq9pWnkMRCo9mwbpDojBTZdw1tjsWSoWJEyyx/security.jpeg]source

Here is a sample smart contract for token distribution on the Ethereum blockchain using the Solidity programming language. Then I explain each line.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TokenDistribution {
    address public owner;
    mapping(address => uint256) public balances;

    event TokensDistributed(address indexed to, uint256 amount);

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can execute this function");
        _;
    }

    function distributeTokens(address[] memory recipients, uint256[] memory amounts) public onlyOwner {
        require(recipients.length == amounts.length, "Recipients and amounts arrays must have the same length");
        for (uint256 i = 0; i < recipients.length; i++) {
            balances[recipients[i]] += amounts[i];
            emit TokensDistributed(recipients[i], amounts[i]);
        }
    }

    function getBalance(address account) public view returns (uint256) {
        return balances[account];
    }
}
  1. // SPDX-License-Identifier: MIT
    - This line specifies the license under which the contract is released. Here, it is the MIT license.

  2. pragma solidity ^0.8.0;
    - This line specifies the version of the Solidity compiler to be used. It ensures compatibility with Solidity version 0.8.0 or higher.

  3. contract TokenDistribution {
    - This line declares a new contract named TokenDistribution.

  4. address public owner;
    - This line declares a public variable owner of type address to store the address of the contract owner.

  5. mapping(address => uint256) public balances;
    - This line declares a public mapping named balances that maps addresses to their token balances.

  6. event TokensDistributed(address indexed to, uint256 amount);
    - This line declares an event named TokensDistributed that logs the distribution of tokens.

  7. constructor() {
    - This line declares a constructor function that is executed once when the contract is deployed.

  8. owner = msg.sender;
    - This line sets the owner variable to the address that deployed the contract.

  9. modifier onlyOwner() {
    - This line declares a modifier named onlyOwner to restrict access to certain functions.

  10. require(msg.sender == owner, "Only the owner can execute this function");

    • This line checks if the caller of the function is the owner. If not, it throws an error.
  11. _

    • This line is a placeholder for the function body that uses the onlyOwner modifier.
  12. function distributeTokens(address[] memory recipients, uint256[] memory amounts) public onlyOwner {

    • This line declares a public function named distributeTokens that takes two arrays as parameters: recipients and amounts.
  13. require(recipients.length == amounts.length, "Recipients and amounts arrays must have the same length");

    • This line checks if the lengths of the recipients and amounts arrays are equal. If not, it throws an error.
  14. for (uint256 i = 0; i < recipients.length; i++) {

    • This line starts a for loop to iterate over the recipients array.
  15. balances[recipients[i]] += amounts[i];

    • This line updates the balance of each recipient by adding the corresponding amount.
  16. emit TokensDistributed(recipients[i], amounts[i]);

    • This line emits the TokensDistributed event for each recipient.
  17. function getBalance(address account) public view returns (uint256) {

    • This line declares a public view function named getBalance that returns the token balance of a given account.
  18. return balances[account];

    • This line returns the balance of the specified account.
TAGS: [ #Programming & Dev ] [ #development ] [ #programming ] [ #chessbrothers ] [ #neoxian ] [ #hive-engine ] [ #stem ] [ #leofinance ] [ #ecency ]

Replies

NO REPLIES FOUND.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>