-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathairdrop.js
213 lines (194 loc) · 8.43 KB
/
airdrop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var $ = jQuery;
jQuery(document).ready(function($) {
let web3 = null;
let tokenContract = null;
let airdropContract = null;
setTimeout(init, 1000);
//$(window).on("load", init);
async function init(){
web3 = await loadWeb3();
if(web3 == null) {
setTimeout(init, 5000);
return;
}
loadContract('./build/contracts/BOBPToken.json', function(data){
tokenContract = data;
$('#tokenABI').text(JSON.stringify(data.abi));
});
loadContract('./build/contracts/BOBPAirdrop.json', function(data){
airdropContract = data;
$('#airdropABI').text(JSON.stringify(data.abi));
initAirdropForm();
});
}
function initAirdropForm(){
let airdropAddress = getUrlParam('airdrop');
if(web3.utils.isAddress(airdropAddress)){
$('input[name=airdropAddress]', '#airdropExecuteForm').val(airdropAddress);
$('#loadInfo').click();
}
}
$('#publishAirdrop').click(function(){
printError('');
let form = $('#airdropContractForm');
let airdropObj = new web3.eth.Contract(airdropContract.abi);
airdropObj.deploy({
data: airdropContract.bytecode,
})
.send({
from: web3.eth.defaultAccount,
})
.on('error',function(error){
console.log('Publishing failed: ', error);
printError(error);
})
.on('transactionHash',function(tx){
$('input[name=publishedTx]',form).val(tx);
})
.on('receipt',function(receipt){
$('input[name=publishedAddress]',form).val(receipt.contractAddress);
$('input[name=airdropAddress]','#airdropExecuteForm').val(receipt.contractAddress);
})
.then(function(contractInstance){
//console.log(contractInstance.options.address) // instance with the new contract address
return contractInstance.methods.token().call()
.then(function(result){
$('input[name=tokenAddress]',form).val(result);
});
});
});
$('#loadInfo').click(function(){
let form = $('#airdropExecuteForm');
let airdropInstance = loadContractInstance(airdropContract, $('input[name=airdropAddress]',form).val());
airdropInstance.methods.token().call()
.then(function(result){
$('input[name=tokenAddress]',form).val(result);
return result;
})
.then(function(tokenAddress){
let tokenInstance = loadContractInstance(tokenContract, tokenAddress);
return tokenInstance.methods.totalSupply().call()
.then(function(result){
$('input[name=totalSupply]',form).val(web3.utils.fromWei(result));
});
});
});
$('#parseList').click(function(){
let form = $('#airdropExecuteForm');
let addressesText = $('#unparsedAddresses').val();
let parsed = new Array();
let parseLog = $('#parseLog');
parseLog.html('');
addressesText.split('\n').forEach(function(elem, idx){
let addr = elem.trim();
if(web3.utils.isAddress(addr)){
parsed.push(addr);
}else{
if(!addr.startsWith('0x') || addr.length != 42){
parseLog.append('<div>Line '+(idx+1)+': <i>"'+elem+'"</i> is not an ethereum address</div>')
}else{
let addrFix = addr.toLowerCase();
if(web3.utils.isAddress(addrFix)){
parseLog.append('<div>Line '+(idx+1)+': <i>'+addr+'</i> has wrong checksumm</div>')
}
}
}
});
parseLog.append('Correctly parsed: '+parsed.length);
$('#parsedAddresses').val(JSON.stringify(parsed));
});
$('#executeAirdrop').click(function(){
let form = $('#airdropExecuteForm');
let airdropLog = $('#airdropLog');
airdropLog.html('');
let sendStart = Number($('input[name=sendStart]', form).val());
let sendLimit = Number($('input[name=sendLimit]', form).val());
if(Number(sendLimit) <= 0) {
console.log('Bad send limit: '+sendLimit); return;
}
let airdropInstance = loadContractInstance(airdropContract, $('input[name=airdropAddress]',form).val());
let airdropAmount = web3.utils.toWei($('input[name=amount]', form).val(), 'ether');
let addresses = JSON.parse($('#parsedAddresses').val());
if(typeof addresses != 'object' || addresses.length == 0){
console.error('Can not parse addresses');
return;
}
airdropLog.append('<div>Starting airdrop '+web3.utils.fromWei(airdropAmount) +' BOBP to '+addresses.length+' addresses in batches of '+sendLimit+' addresses per transaction, starting from address '+sendStart+'.</div>');
function sendTokens(start) {
let end = start+sendLimit;
if(end > addresses.length) end = addresses.length;
if(start >= end) {
console.error('Start >= End!', start, sendLimit, end); return;
}
let addressList = addresses.slice(start, start+sendLimit);
console.log('Send '+airdropAmount+' tokens to addresses '+start+' - '+end, addressList);
let tx = null;
airdropInstance.methods.airdrop(airdropAmount, addressList).send({
from: web3.eth.defaultAccount,
})
.on('transactionHash', function(hash){
tx = hash;
airdropLog.append('<div>Transaction <i>'+tx+'</i>: addresses '+start+' - '+end+' published.</div>');
if(end != addresses.length){
sendTokens(end);
}
})
.on('receipt', function(receipt){
airdropLog.append('<div>Transaction <i>'+receipt.transactionHash+'</i> ('+start+' - '+end+') mined.</div>');
})
.on('error', function(error){
console.log('Error sending to addresses '+start+' - '+end+((tx==null)?'':', tx '+tx), error);
});
}
sendTokens(0);
});
//====================================================
async function loadWeb3(){
printError('');
if(typeof window.web3 == "undefined"){
printError('No MetaMask found');
return null;
}
// let Web3 = require('web3');
// let web3 = new Web3();
// web3.setProvider(window.web3.currentProvider);
let web3 = new Web3(window.web3.currentProvider);
let accounts = await web3.eth.getAccounts();
if(typeof accounts[0] == 'undefined'){
printError('Please, unlock MetaMask');
return null;
}
// web3.eth.getBlock('latest', function(error, result){
// console.log('Current latest block: #'+result.number+' '+timestmapToString(result.timestamp), result);
// });
web3.eth.defaultAccount = accounts[0];
window.web3 = web3;
return web3;
}
function loadContract(url, callback){
$.ajax(url,{'dataType':'json', 'cache':'false', 'data':{'t':Date.now()}}).done(callback);
}
function loadContractInstance(contractDef, address){
if(typeof contractDef == 'undefined' || contractDef == null) return null;
printError('');
if(!web3.utils.isAddress(address)){printError('Contract '+contractDef.contract_name+' address '+address+'is not an Ethereum address'); return null;}
return new web3.eth.Contract(contractDef.abi, address);
}
/**
* Take GET parameter from current page URL
*/
function getUrlParam(name){
if(window.location.search == '') return null;
let params = window.location.search.substr(1).split('&').map(function(item){return item.split("=").map(decodeURIComponent);});
let found = params.find(function(item){return item[0] == name});
return (typeof found == "undefined")?null:found[1];
}
function printError(msg){
if(msg == null || msg == ''){
$('#errormsg').html('');
}else{
console.error(msg);
$('#errormsg').html(msg);
}
}
});