1

I am developing a contract that distributes tokens and has already implemented the main functionality. The contract accepts tokens from the owner and distributes them by 1KK to each address from the list. But now I want the contract to have two lists and that the addresses from the second list receive 1KK tokens each. How best to implement this without spoiling the security and performance of the contract.

// SPDX-License-Identifier: AGPL

pragma solidity ^0.8.17;

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

contract TokenDistributerGBR {
address[] public recipientsList;
uint256 public recipientsCount;
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 {
    if (!recipients[recipient]) {
        recipients[recipient] = true;
        recipientsCount++;
    }
}

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

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

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

1 Answer 1

0

I think the answer here comes down to design choice, as there is more than 1 way to address this problem.

You could implement a function which redefines your recipient list, e.g.

function redefineRecipientlist(address[] memory newList) public onlyOwner {
recipientsList = newList;
}

...and then call the distributeTokens() function again.

Alternatively, you could create an array of arrays, defining the index value at which to iterate through the list and undertake the transfers. This would require another function to add 'lists' into the array. after which you would just slightly modify your distributeTokens() function to take an index value as a parameter:

function addRecipientsListToArray(address[] memory anotherList) public onlyOwner {
recipientsList.push(anotherList)
}

&

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

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

..I haven't run the code/debugged, and am on mobile currently, but this is an approach to solve your issue.

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.