Skip to content

Instantly share code, notes, and snippets.

@jerry-sl
Created June 12, 2018 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jerry-sl/18089aacea9bf24ba8db2b75c7273508 to your computer and use it in GitHub Desktop.
Save jerry-sl/18089aacea9bf24ba8db2b75c7273508 to your computer and use it in GitHub Desktop.
ERC20 airdrop using web3
var fs = require('fs');
var parse = require('csv-parse');
var Web3 = require('web3');
var abi = require('./abi.json');
var _ = require('lodash');
const Tx = require('ethereumjs-tx');
var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/scure_key"));
//var inputFile = 'data.csv';
var inputFile = 'smalldata.csv';
const main = async (addrs, tokens) => {
console.log(`web3 version: ${web3.version}`)
var contractJson = abi.abi;
var tokenAddress = "0x57fdf8001d1c240d4cbfc82b2fede932960dc310";
var myAddress = "0x7b4d9ef0740f75dbc79222a323246a3659609896";
var privateKey = "81d4f6e4b9d628fa15a9ff98f610e87d2662348c5666a7bf0fc39edf74f27b38";
var abiArray = abi.abi;
var contractAddress = "0xCF3aFC7e21Da35559269F7503b843ac543392aE0";
var count = await web3.eth.getTransactionCount(myAddress);
console.log(`num transactions so far: ${count}`);
var contract = new web3.eth.Contract(abiArray, contractAddress, {
from: myAddress
});
var rawTransaction = {
"from": myAddress,
"nonce": "0x" + count.toString(16),
"gasPrice": "0x003B9ACA00",
"gasLimit": "0x200B20",
"to": contractAddress,
"value": "0x0",
"data": contract.methods.multisend(tokenAddress, addrs, tokens).encodeABI(),
"chainId": "0x03",
"gas": "0x30D40"
};
var privKey = new Buffer(privateKey, 'hex');
var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
console.log(`Attempting to send signed tx: ${serializedTx.toString('hex')}`);
var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
console.log(`Receipt info: ${JSON.stringify(receipt, null, '\t')}`);
}
var parser = parse({
delimiter: ','
}, function (err, data) {
let addressList = [];
let tokensList = [];
for (let i = 0; i < data.length; i++) {
addressList.push(data[i][0]);
tokensList.push(data[i][1]);
}
let addrsList = _.chunk(addressList, 2);
let tokenList = _.chunk(tokensList, 2);
main(addrsList[0], tokenList[0]);
});
fs.createReadStream(inputFile).pipe(parser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment