Last active
January 29, 2019 03:05
-
-
Save Hamzaahmed742/c0d99e752d7bb5bc067421e218742171 to your computer and use it in GitHub Desktop.
Airdrop script compatible only with ERC644 and use Couchbase as the DB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| var couchbase = require('couchbase') | |
| var cluster = new couchbase.Cluster('couchbase://ip/'); | |
| cluster.authenticate('Username', 'password'); | |
| var TokenLab_bucket = cluster.openBucket('bucket name') | |
| var Snapshot_bucket = cluster.openBucket('bucket name') | |
| var Web3 = require('web3'); | |
| const fs = require('fs'); | |
| var keythereum = require("keythereum"); | |
| var key = require('./UTC.json'); | |
| var web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.1.3:9656/")); | |
| var axios = require("axios"); | |
| var beautify = require("json-beautify"); | |
| var {airdropbytecode, airdropabi} = require("./airdrop.js") | |
| var {balancesbytecode, balancesabi} = require("./balances.js") | |
| var {adminbytecode, adminabi} = require("./admin.js") | |
| var {tokenbytecode, tokenabi} = require("./token.js") | |
| var failedTransactions = []; | |
| const EthereumTx = require('ethereumjs-tx') | |
| const privateKey = Buffer.from('private key', 'hex') | |
| var http = require('http'); | |
| var app = express(); | |
| var server = http.createServer(app); | |
| var io = require('socket.io').listen(server); | |
| var accounts = []; | |
| var old_list = []; | |
| var new_list = []; | |
| var list = []; | |
| var tempList = []; | |
| var contractData, rawTx; | |
| var index = 0; | |
| var stream; | |
| var totalLength = 0; | |
| var pexList = []; | |
| var balancesObject, adminObject, airdropObject, tokenObject; | |
| var balancesContract, adminContract, airdropContract, tokenContract; | |
| //testing Contract | |
| // var ownerAddress = "0xD033736d6DbC4b5DE792638B63561f1500eBc2C7"; | |
| // var balancesAddress = "0x7ae5f3d1761887823fb7392c3da85a3e3cd20e8e"; | |
| // var adminAddress = "0x7f3bd33a6f089696812002763a7383508dfd8589"; | |
| // var tokenAddress = "0xab6f7d2f841cf4c9fade3c6773d14a09d36d9178"; | |
| // var airdropAddress = "0xd65313f44436e33db8d38bb58ab27738d7dd3e0c"; | |
| // var reserveAddress = "0xD033736d6DbC4b5DE792638B63561f1500eBc2C7"; | |
| var tempArray = []; | |
| //Use this for initializing the contract instances and slicing the data | |
| app.get('/initializeContracts', (req, res) => { | |
| balancesContract = web3.eth.contract(balancesabi).at(balancesAddress); | |
| tokenContract = web3.eth.contract(tokenabi).at(tokenAddress) | |
| airdropContract = web3.eth.contract(airdropabi).at(airdropAddress); | |
| res.end("initializing Contracts...") | |
| }) | |
| //slice function for testing can be used for real purposes but have to change the variables | |
| app.get('/slice', (req, res) => { | |
| var i,j,chunk = 100; | |
| for (i=0,j= list.length; i < j; i+=chunk) { //Must set j = list.length | |
| tempArray.push(list.slice(i,i+chunk)); | |
| } | |
| res.end("slicing the data..."); | |
| }) | |
| //create list with dummy address generated from ganache but have real amounts | |
| app.get('/createTestList', (req, res) => { | |
| var totalAmount = 0; | |
| for(var i = 300; i < 1000; i++){ | |
| tempList.push({address: accounts[i], balance: web3.toWei(list[i].token, 'ether')}) | |
| totalAmount += list[i].token ; | |
| } | |
| res.end("Creating Test Data...") | |
| }) | |
| //extract private keys from the utc file using the password | |
| app.get('/getPrivateKey', (req, res) => { | |
| keythereum.recover("password", key, function (_privateKey) { | |
| privateKey = _privateKey | |
| res.json(privateKey) | |
| }); | |
| }) | |
| //Creates raw transactions for the airdrop must give a slice, address and respective amounts | |
| const createRawTransaction = (balances, addresses, balanceAddress, reserve) => { | |
| return new Promise ((resolve, reject) => { | |
| contractData = airdropContract.airdropTokenUsingBalances.getData(balances, addresses, balancesAddress, reserve); | |
| var gasPriceHex; | |
| var nonceHex; | |
| web3.eth.getGasPrice(function(err, result){ | |
| gasPriceHex = web3.toHex(result); | |
| // console.log("Gas Price ", result.toNumber()); | |
| // console.log("Gas Limit ", web3.eth.getBlock('latest').gasLimit) | |
| const gasLimitHex = web3.toHex(web3.eth.getBlock('latest').gasLimit); | |
| const nonce = web3.eth.getTransactionCount(ownerAddress,function(err, result){ | |
| // console.log("Transaction Count", web3.toHex(result),result) | |
| nonceHex = web3.toHex(result); | |
| resolve({ | |
| nonce: nonceHex, | |
| gasPrice: gasPriceHex, | |
| gasLimit: gasLimitHex, | |
| data: contractData, | |
| to: airdropAddress, | |
| chainId: 2, | |
| from: ownerAddress | |
| }) | |
| }); | |
| }); | |
| }) | |
| } | |
| //endpoint to start the airdrop | |
| app.get('/airdrop', (req, res) => { | |
| // sendTransaction(); | |
| sendTransaction(tempArray[index].map(arr => web3.toWei(arr.token), 'ether'), tempArray[index].map(arr => arr.address), balancesAddress, reserveAddress) | |
| res.end("Please see the log file"); | |
| }) | |
| // function to send transaction and log them respectively | |
| const sendTransaction = (balances, addresses, balancesAddress, reserve) => { | |
| var serializedTx; | |
| stream = fs.createWriteStream('logs/log(' + index +').txt'); | |
| createRawTransaction(balances, addresses, balancesAddress, reserve).then(res => { | |
| console.log("===================Start Of RawTx ================="); | |
| stream.write("===================Start Of RawTx =================\n"); | |
| console.log(res); | |
| stream.write(JSON.stringify(res) + '\n'); | |
| stream.write("===================End Of RawTx =================\n"); | |
| console.log("===================End Of RawTx ================="); | |
| const tx = new EthereumTx(res) | |
| tx.sign(privateKey) | |
| serializedTx = tx.serialize() | |
| web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), (err, hash) => { | |
| if (err) { console.log(err); return; } | |
| stream.write("===================TXN=================\n"); | |
| stream.write(hash + '\n'); | |
| stream.write("===================TXN=================\n"); | |
| getTransactionReceiptMined(hash).then(receipt => { | |
| console.log("=====================Receipt======================="); | |
| stream.write("===================Receipt=================\n"); | |
| console.log(receipt); | |
| stream.write(JSON.stringify(receipt) + '\n'); | |
| index++; | |
| stream.write("===================Receipt=================\n"); | |
| console.log("=====================Receipt======================="); | |
| if(receipt.status == "0x0"){ | |
| failedTransactions.push({rawtx: res, receipt: receipt}) | |
| } | |
| if(index < tempArray.length){ | |
| sendTransaction(tempArray[index].map(arr => web3.toWei(arr.token), 'ether'), tempArray[index].map(arr => arr.address), balancesAddress, reserveAddress); | |
| } else { | |
| if(failedTransactions.length > 0){ | |
| var tempStream = fs.createWriteStream('logs/failedTransactions.txt'); | |
| failedTransactions.forEach((txn, index) => { | |
| tempStream.write("====================TXN " + index + "======================\n"); | |
| tempStream.write(JSON.stringify(txn)); | |
| tempStream.write("====================END OF TXN " + index + "======================\n"); | |
| }) | |
| console.log("See failedTransactions file in logs for Transaction failure"); | |
| }else { | |
| console.log("All Good..."); | |
| } | |
| } | |
| }) | |
| }) | |
| }); | |
| stream.on('drain', function() { | |
| console.log('drain'); // never happen | |
| }); | |
| } | |
| //promise function to track the transaction using txn | |
| const getTransactionReceiptMined = (txHash) => { | |
| const transactionReceiptAsync = function(resolve, reject) { | |
| console.log("Waiting for the transaction tobe mined..."); | |
| stream.write("Waiting for the transaction tobe mined...\n"); | |
| web3.eth.getTransactionReceipt(txHash, (error, receipt) => { | |
| if (error) { | |
| reject(error); | |
| } else if (receipt == null) { | |
| setTimeout( | |
| () => transactionReceiptAsync(resolve, reject),1000); | |
| } else { | |
| resolve(receipt); | |
| } | |
| }); | |
| }; | |
| if (Array.isArray(txHash)) { | |
| return Promise.all(txHash.map( | |
| oneTxHash => getTransactionReceiptMined(oneTxHash, interval))); | |
| } else if (typeof txHash === "string") { | |
| return new Promise(transactionReceiptAsync); | |
| } else { | |
| throw new Error("Invalid Type: " + txHash); | |
| } | |
| }; | |
| // endpoint for initial test | |
| app.get('/test', (req, res) => { | |
| // res.json({wei: tempArray[0].map(arr => web3.toWei(arr.token), 'ether'), hex: tempArray[0].map(arr => web3.toHex(arr.token), 'ether')}) | |
| }) | |
| // endpoint, returns the slices, slice length, total length | |
| app.get('/list', (req, res) => { | |
| res.json(tempArray) | |
| }) | |
| app.get("/calculateDifference", (req, res) => { | |
| // old_list.forEach((old_obj) => { | |
| // new_list.map((new_obj) => { | |
| // if(new_obj.address == old_obj.address && Math.abs(parseInt(old_obj.balance.split(" ")[0]) - parseInt(new_obj.balance)) > 0) { | |
| // console.log({ | |
| // address: new_obj.address, | |
| // token: Math.abs(parseInt(old_obj.balance.split(" ")[0]) - parseInt(new_obj.balance)) * 1500, | |
| // old_balance: parseInt(old_obj.balance.split(" ")[0]), | |
| // new_balance: parseInt(new_obj.balance) | |
| // }) | |
| // list.push({address: new_obj.address, token: Math.abs(parseInt(old_obj.balance.split(" ")[0]) - parseInt(new_obj.balance)) * 1500}) | |
| // } | |
| // }) | |
| // }) | |
| old_list.forEach((old_obj) => { | |
| new_list.map((new_obj) => { | |
| if(old_obj.address == new_obj.address && parseInt(old_obj.balance) != parseInt(new_obj.balance)){ | |
| console.log(beautify({ | |
| address: new_obj.address, | |
| old_balance: parseInt(old_obj.balance), | |
| new_balance: parseInt(new_obj.balance) | |
| }, null, 2, 100)) | |
| list.push({address: new_obj.address, token: Math.abs(parseInt(old_obj.balance.split(" ")[0]) - parseInt(new_obj.balance)) * 1500}) | |
| } | |
| }) | |
| }) | |
| console.log(beautify(list.length, null, 2, 100)) | |
| res.end("see terminal") | |
| }) | |
| app.listen(3000, () => { | |
| console.log('Example app listening on port 3000!'); | |
| // fs.writeFile('address.txt', web3.eth.accounts, (err) => { | |
| // if (err) throw err; | |
| // console.log('The file has been saved!'); | |
| // }); | |
| // console.log("Reading addresses..."); | |
| // fs.readFile('address.txt', 'utf8', function(err, contents) { | |
| // var str = ""; | |
| // for(var i = 0; i < contents.length; i ++){ | |
| // if(contents[i] != ",") { | |
| // str += contents[i]; | |
| // }else{ | |
| // accounts.push(str); | |
| // str = ""; | |
| // } | |
| // } | |
| // console.log("accounts length", accounts.length) | |
| // console.log("Reading address complete."); | |
| // }); | |
| // var query = "select * from TokenLab USE KEYS "; | |
| // var finalQuery = query + "\'campaign:c769da8a-4496-4cfd-93c3-22e61895e2e1\'"; | |
| // TokenLab_bucket.query(couchbase.N1qlQuery.fromString(finalQuery), (err, result) => { | |
| // if(!err) { | |
| // // console.log(beautify(result, null, 2, 100)) | |
| // var { EXR } = result[0].TokenLab.data; | |
| // // console.log(beautify(EXP, null, 2, 100)) | |
| // EXR.forEach(id => { | |
| // finalQuery = query + "\'data:" + id + "\'"; | |
| // console.log(finalQuery) | |
| // TokenLab_bucket.query(couchbase.N1qlQuery.fromString(finalQuery), (err, result) => { | |
| // if(!err) { | |
| // const { data } = result[0].TokenLab; | |
| // console.log("Old EXR Length", beautify(data.length, null, 2, 100)) | |
| // old_list.push(...data) | |
| // console.log("Pushing old EXR Data To List") | |
| // }else{ | |
| // console.log("Token key", beautify(err, null, 80)) | |
| // } | |
| // }) | |
| // }) | |
| // } else { | |
| // console.log("Campaign key", beautify(err, null, 80)) | |
| // } | |
| // }) | |
| var snapshotQuery = 'Select snapshot From Snapshot WHERE Record="Snapshot" And UID="873d3fa3-e755-4e64-ba64-036929b238b8"' | |
| Snapshot_bucket.query(couchbase.N1qlQuery.fromString(snapshotQuery), (err, result) => { | |
| if(!err) { | |
| console.log("New EXR Length", result[0].snapshot.length) | |
| var { snapshot } = result[0]; | |
| old_list.push(...snapshot); | |
| console.log("Pushing new EXR Data to the list"); | |
| } | |
| }) | |
| var snapshotQuery = 'Select snapshot From Snapshot WHERE Record="Snapshot" And UID="09103d7a-095a-409f-b562-9b6f0cf6b05b"' | |
| Snapshot_bucket.query(couchbase.N1qlQuery.fromString(snapshotQuery), (err, result) => { | |
| if(!err) { | |
| console.log("New EXR Length", result[0].snapshot.length) | |
| var { snapshot } = result[0]; | |
| new_list.push(...snapshot); | |
| console.log("Pushing new EXR Data to the list"); | |
| } | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment