ERC20 – How to Create an ERC20 Faucet

erc-20faucetsjson-rpctestingtestnets

I'm having some trouble wrapping my mind around the creation of ERC20 faucets. How exactly is this done? I have created a token (https://ropsten.etherscan.io/token/0x90de67a867b18535ad558fec0c418921340ffc91?a=0x18c59adbf99be137b3eeaffd84b083fd623a4c36) but I'm not sure how I would go about distributing tokens utilizing a faucet. This is purely for testing purposes but I'd like to be able to give away diminishing values of this test token based on the previous amount given out.

I'm assuming I'll need to use Transfer(), which I used in my getFromFaucet() function, but I'm extremely confused as to how to even get these tokens to be sent to a different address.

function getFromFaucet(address _to) returns (bool success) {
        uint256 giveaway_value;

        if (previous_giveaway == 0){
            giveaway_value = starting_giveaway;
            previous_giveaway = starting_giveaway;
        }
        previous_giveaway = previous_giveaway / 2;

        if (balances[msg.sender] >= giveaway_value){
            balances[msg.sender] -= giveaway_value;
            balances[_to] += giveaway_value;
            Transfer(msg.sender, _to, giveaway_value);
            return true;
        }
        else return false;
    }

Like, how do I call this getFromFaucet and pass the address to it? I'm planning on distributing from a website.

Thanks in advance!

Best Answer

You are writting a complex transfer function, the only difference is the sender cannot control the amount given away.

If you want to create a token faucet I'd make the recipient to send the transaction. Also having a cool down period of two minutes to limit possible abuse.

uint previous_giveaway;
uint last_giveaway;

function drip() returns (bool success) {
    // Only allow to drip every two minutes to limit abuse
    if (now - last_giveaway < 2 minutes) {
        return false;
    }
    last_giveaway = now;
    // deliver half than last time
    uint giveaway_value = previous_giveaway / 2;
    if (giveaway_value == 0){
        giveaway_value = starting_giveaway;
        previous_giveaway = starting_giveaway;
    }
    // It is a faucet mint new tokens
    balances[msg.sender] += giveaway_value;
    totalSupply += giveaway_value;
    Transfer(0x0, msg.sender, giveaway_value);
    return true;
}

To call that contract from a webapp using web3 v1.0 something like this should work

const token = new eth.Contract(abiToken, addressToken);
await token.methods.drip().send({ from: "address" });

The contract will deposit a few tokens to "address".

Related Topic