2

**I am developing a smart contract distributor in the solidity language that will accept BEP20 tokens from the owner with the special contract address: 0x0000000000000000000000000000. the owner of the contract can add addresses to the list of addresses that the contract stores. every 30 days, the contract must send the tokens it holds to addresses from the stored list, 1,000,000 tokens per address. **

ERRORS: : DeclarationError: Undeclared identifier. --> TokenDistributer.sol:42:33: | 42 | for (uint256 i = 0; i < recipientsList.length; i++) { | ^^^^^^^^^^^^^^, и вторая : DeclarationError: Undeclared identifier. --> TokenDistributer.sol:43:28: | 43 | if (recipients[recipientsList[i]]) { | ^^^^^^^^^^^^^^

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IBEP20 {
    function transfer(address to, uint256 value) external returns (bool);
    function balanceOf(address who) external view returns (uint256);
}

contract TokenDistributer {
    address public owner;
    IBEP20 public token;
    mapping(address => bool) public recipients;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor(address _tokenAddress) {
        owner = msg.sender;
        token = IBEP20(_tokenAddress);
    }

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

    function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0), "New owner cannot be the zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

    function addRecipient(address recipient) public onlyOwner {
        recipients[recipient] = true;
    }

    function removeRecipient(address recipient) public onlyOwner {
        recipients[recipient] = false;
    }

    function recipientsCount() public view returns (uint256 count) {
        for (uint256 i = 0; i < recipientsList.length; i++) {
            if (recipients[recipientsList[i]]) {
                count++;
            }
        }
    }

    function distributeTokens() public onlyOwner {
        require(token.balanceOf(address(this)) >= 1000000 * recipientsCount(), "Insufficient balance to distribute tokens");

        address[] memory recipientsList = new address[](recipientsCount());
        uint256 recipientIndex = 0;

        for (uint256 i = 0; i < recipientsList.length; i++) {
            if (recipients[recipientsList[i]]) {
                recipientsList[recipientIndex] = recipientsList[i];
                recipientIndex++;
            }
        }

        for (uint256 i = 0; i < recipientsList.length; i++) {
            if (recipients[recipientsList[i]]) {
                require(token.transfer(recipientsList[i], 1000000), "Token transfer failed");
            }
        }
    }
}

1 Answer 1

2

The error you're seeing is because you haven't declared a global variable called recipientsList. To fix this, you can declare a global array at the beginning of your contract like this:

address[] public recipientsList;

Once you've done that, you'll need to modify your distributeTokens() function accordingly. Here's an example of what that might look like:

function distributeTokens() public onlyOwner {
    require(token.balanceOf(address(this)) >= 1000000 * recipientsList.length, "Insufficient balance to distribute tokens");

    for (uint256 i = 0; i < recipientsList.length; i++) {
        if (recipientsList[i] != address(0)) {
            require(token.transfer(recipientsList[i], 1000000), "Token transfer failed");
        }
    }
}

This updated function checks the balance of the contract to ensure there are enough tokens to distribute, and then loops through the recipientsList array to transfer tokens to each recipient. Note that I also added a check to ensure that the recipient's address is not zero before attempting to transfer tokens to it.

I hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.