0
votes

I have made a simple bep 20 token and I am trying to mint my wallet address 50% of the token supply and evenly distribute the remaining 50% of the supply between 10 different wallets that I would like to generate with code. I am not sure whether this can be done within the contract itself or has to be done separately through python after the contract is deployed.

here is the solidity code:


contract Token {
    mapping(address => uint) public balances;
    mapping(address => mapping(address => uint)) public allowance;
    uint public totalSupply = 10000000000;
    string public name = 'TestToken';
    string public symbol = 'TEST';
    uint public decimals = 9;
    
    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owener, address indexed spender, uint value);
    
    constructor() {
        balances[msg.sender] = totalSupply;
    }

    function balanceOf(address owner) public view returns(uint) {
        return balances[owner];
    }
    
    function transfer(address to, uint value) public returns(bool) {
        require(balanceOf(msg.sender)>= value, 'You are broke lol');
        balances[to] += value;
        balances[msg.sender] -= value;
        emit Transfer(msg.sender, to, value);
        return true;
        }
    
    function transferFrom(address from, address to, uint value) public returns(bool) {
        require(balanceOf(from) >= value, 'You broke');
        require(allowance[from][msg.sender] >= value, 'allowance too low');
        balances[to] += value;
        balances[from] -= value;
        emit Transfer(from, to, value);
        return true;
    }
    
    function approve(address spender, uint value) public returns(bool) {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }
}```
1

1 Answers

0
votes

You can implement a mint() function in a way that distributes the first 50% to the msg.sender (or a hardcoded address) and the other 50% to the 10 addresses that are passed in the function argument.

Your python script (or any other client, a wallet software, a JS script, etc.) than generates the 10 addresses and passes them in the argument. Example in JS using the web3 library:

const Web3 = require('web3');

const web3 = new Web3(providerUrl); // TODO fill your value
const contract = new web3.eth.Contract(abiJson, contractAddress); // TODO fill your values

const mintTokens = async () {
    const amount = web3.utils.toWei('1', 'ether');
    const addresses = await getRandomAddresses();

    await contract.methods.mint(amount, addresses).send();
}

const getRandomAddresses = async () {
    let addresses = [];

    for (let i = 0; i < 10; i++) {
        const account = web3.eth.accounts.create();
        addresses.push(account.address);
    }

    return addresses;
}

mintTokens();

Mind that this example requires the _amount to be divisible by 20. Otherwise, it would result in slightly incorrect results (up to 5% total).

Also, there's no authorization in my example, so anyone can execute this function. You'll probably want to implement an authorization as well (for example using the ownable pattern), or mark the function as internal (so that it's not executable by anyone) and call it just from the constructor().

pragma solidity ^0.8.4;

contract Token {
    mapping(address => uint) public balances;
    event Transfer(address indexed from, address indexed to, uint value);
    
    function mint(uint256 _amount, address[10] memory _receivers) external {
        // mint 50% of the _amount to one address
        balances[msg.sender] += _amount / 2;
        emit Transfer(address(0x0), msg.sender, _amount / 2);
        
        // mint the rest (another 50%) evenly to each receiver
        // i.e. each gets 5%
        for (uint i = 0; i < 10; i++) {
            balances[_receivers[i]] += _amount / 20;
            emit Transfer(address(0x0), _receivers[i], _amount / 20);
        }
    }
}