Code Monkey home page Code Monkey logo

airdrop's Introduction

ERC20 Token Airdrop

Build Status license

Airdrop effect ๐Ÿฌ

Multiple airdrop consumes one time gas FYI : https://etherscan.io/tx/0x061e701609d23e6755253fdb55ed1a31d2b253610b9f478600a0c1ad8b1f178e

source code

pragma solidity ^0.4.21;

import "../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";


contract Airdrop is Ownable {
  /**
   * @dev daAirdrop to address
   * @param _tokenAddr address the erc20 token address
   * @param dests address[] addresses to airdrop
   * @param values uint256[] value(in ether) to airdrop
   */
  function doAirdrop(address _tokenAddr, address[] dests, uint256[] values) onlyOwner public
    returns (uint256) {
    uint256 i = 0;
    while (i < dests.length) {
      ERC20(_tokenAddr).transferFrom(msg.sender, dests[i], values[i] * (10 ** 18));
      i += 1;
    }
    return(i);
  }

usage

const SimpleToken = artifacts.require('SimpleToken');
const Airdrop = artifacts.require('Airdrop');
const BigNumber = web3.BigNumber;
require('chai')
  .use(require('chai-as-promised'))
  .use(require('chai-bignumber')(BigNumber))
  .should();

contract('Airdrop', function (accounts) {
  before(async function () {
    this.simpleToken = await SimpleToken.new({ from: accounts[0] });
    this.airdrop = await Airdrop.new({ from: accounts[0] });
    this.airdropAmount = 666
    this.mockdata = {
      addresses: accounts,
      tokenAmounts: Array(10).fill(this.airdropAmount.toString()),
    }
  });

  describe('Airdrop basic test', function () {
    it('should airdrop to addresses', async function () {
      await this.simpleToken.approve(this.airdrop.address, 1e27);
      await this.airdrop.doAirdrop(
        this.simpleToken.address,
        this.mockdata.addresses,
        this.mockdata.tokenAmounts
      );
      const balance = (await this.simpleToken.balanceOf(accounts[1])).toNumber();
      await balance.should.be.equal(parseInt(await web3.toWei(this.airdropAmount)));
    });
  });
})
# test

npm run test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.