-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeb3JS.js
More file actions
331 lines (300 loc) · 9.5 KB
/
Web3JS.js
File metadata and controls
331 lines (300 loc) · 9.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var Web3 = require('web3');
var web3 = new Web3();
var contractAddress = "0xc5A49Fbc12b8A40d8Ed6521d97a90c53b9Be9BfD";
//公开售卖账户的私钥
const privateKey = new Buffer('3889a27d7a34dd2b20da5b18925804ac6e7cb3b85e69fc8f1e964bf404269503','hex');
// 设置网上的全节点
// Mainnet--主网
// web3.setProvider(new Web3.providers.HttpProvider("https://mainnet.infura.io/b1bDV0dgCRvUaoSIBLlv"));
// Ropsten--测试网
web3.setProvider(new Web3.providers.HttpProvider("https://ropsten.infura.io/b1bDV0dgCRvUaoSIBLlv"));
// 获取公开售卖账户的交易nonce值
var nonce = web3.eth.getTransactionCount('0x09EE985E10128f401F579fAf5F4531FF5CD6E56e','pending');
console.log(nonce);
/**
* 获取总量totalsupply
*/
var result = web3.eth.call({
to: contractAddress, //合约地址
data: "0x" + "18160ddd" //
});
console.log(result);
console.log(web3.toDecimal(result));
/**
* 获取合约拥有者owner
*/
var result = web3.eth.call({
to: contractAddress, //合约地址
data: "0x" + "893d20e8" //
});
console.log(result);
console.log(web3.toDecimal(result));
/**
* 获取总量owner所持有的币的数量
*/
var address = "0x09EE985E10128f401F579fAf5F4531FF5CD6E56e";
var result = web3.eth.call({
to: contractAddress, //合约地址
data: "0x" + "70a08231000000000000000000000000"+address.substring(2) //
});
console.log(result);
console.log(web3.toDecimal(result));
/**
* 查看新的合约地址
*/
var result = web3.eth.call({
to: contractAddress, //合约地址
data: "0x" + "26976e3f"//
});
console.log(result);
/**
* 查看一个合约是否被废弃
*/
var result = web3.eth.call({
to: contractAddress, //合约地址
data: "0x" + "0e136b19"//
});
console.log(result);
/**
* 合约里面调用合约(交易)
*/
var address = "0xe22B697FEDf3f45521E79AE26bFae607a6Ec153c";
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"bb29998e000000000000000000000000"+address.substring(2)
};
/**
*
* 将合约废弃--模块(交易)
*/
var address = "0xc5A49Fbc12b8A40d8Ed6521d97a90c53b9Be9BfD";
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"0753c30c000000000000000000000000"+address.substring(2)
};
/**
*
* 设置每次空投的数量--模块(交易)
*/
//设置每次空投的数量,并组成32位字节
var num = 1000;
var num = web3.toHex(num*1e+18).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
num = num.substring(2);
var str = "0";
for(var i=1;i<(64-num.length);i++){
str = str.concat("0");
}
num = str.concat(num);
//设置每次空投的数量rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"3abcf319"+num
};
/**
*
* 设置空投时间--模块(交易)
*/
//空投的时间,并组成32位字节
var time = 1526029200; //Unix时间戳
var time = web3.toHex(time).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
time = time.substring(2)
var str = "0";
for(var i=1;i<(64-time.length);i++){
str = str.concat("0");
}
time = str.concat(time);
//设置空投时间的rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"ce6f1208"+time
};
/**
*
* 设置ico结束时间--模块(交易)
*/
//空投的时间,并组成32位字节
var time = 1526029200; //Unix时间戳
var time = web3.toHex(time).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
time = time.substring(2)
var str = "0";
for(var i=1;i<(64-time.length);i++){
str = str.concat("0");
}
time = str.concat(time);
//设置空投时间的rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"c7dab701"+time
};
/**
*
* 设置unlockTime时间--模块(交易)
*/
//空投的时间,并组成32位字节
var time = 1526029200; //Unix时间戳
var time = web3.toHex(time).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
time = time.substring(2)
var str = "0";
for(var i=1;i<(64-time.length);i++){
str = str.concat("0");
}
time = str.concat(time);
//设置空投时间的rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"dace4557"+time
};
/**
* 改变合约的owner--模块(交易)
*/
var newOwner = "0xe22B697FEDf3f45521E79AE26bFae607a6Ec153c";
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"f2fde38b000000000000000000000000"+newOwner.substring(2)
};
/**
* 销毁代币--模块(交易)
*/
//设置销毁代币的数量,并组成32位字节
var num = 60000;
var num = web3.toHex(num*1e+18).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
num = num.substring(2);
var str = "0";
for(var i=1;i<(64-num.length);i++){
str = str.concat("0");
}
num = str.concat(num);
//设置销毁代币的rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"42966c68"+num
};
/**
*
* 提币转账--模块(交易)
*/
// 设置提币转账的数量,并组成32位字节
var num = 10000;
var num = web3.toHex(num*1e+18).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
num = num.substring(2);
var str = "0";
for(var i=1;i<(64-num.length);i++){
str = str.concat("0");
}
num = str.concat(num);
// 提币转账的地址
var address = "0x0527fC08043Fa08f96e83C760439ac53f2ea25aD";
//设置每次空投的量rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"a9059cbb000000000000000000000000"+address.substring(2)+num
};
/**
*
* 调用approve函数(交易)
*/
// 设置提币转账的数量,并组成32位字节
var num = 10000;
var num = web3.toHex(num*1e+18).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
num = num.substring(2);
var str = "0";
for(var i=1;i<(64-num.length);i++){
str = str.concat("0");
}
num = str.concat(num);
// 提币转账的地址
var address = "0xA51e56633E526065Ea25DbeeebBC8d8C2200ee6C";
//设置每次空投的量rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"095ea7b3000000000000000000000000"+address.substring(2)+num
};
/**
*
* 调用transferfrom函数(交易)
*/
// 设置提币转账的数量,并组成32位字节
var num = 10000;
var num = web3.toHex(num*1e+18).toString();
//出去16进制开头的两个字母“0x”,并构造32字节的参数
num = num.substring(2)
var str = "0";
for(var i=1;i<(64-num.length);i++){
str = str.concat("0");
}
num = str.concat(num);
// 提币转账的地址
var addressFrom = "0xb0361E2FC9b553107BB16BeAec9dCB6D7353db87";
var addressTo = "0xc1496a03c66B800Ff0DA698b7fff510893538b90";
//设置每次空投的量rawTx
var rawTx ={"nonce":web3.toHex(nonce).toString(),
"gasPrice":'0x098bca5a00', //41000000000 == 0.000000041 Ether
"gasLimit":'0x3d090', //250000
"to":contractAddress,
"value":"0x00",
"data":"0x"+"23b872dd000000000000000000000000"+addressFrom.substring(2)+"000000000000000000000000"+addressTo.substring(2)+num
};
/**
*只要是发送一笔交易就会调用下面的模块
*/
//用私钥进行签名
var Tx = require('ethereumjs-tx');
var tx = new Tx(rawTx);
tx.sign(privateKey);
//发送交易
var serializedTx = tx.serialize();
var trascationHash = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'));
console.log(trascationHash);
// var trascationHash = web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
// if (!err) {
// console.log(hash);
// //获取blockhash用来判断是不是处于pending的状态
// var transaction = web3.eth.getTransaction(hash);
// console.log(transaction.blockHash);
// //当打包成功,判断status的状态
// if(transaction.blockHash!=null){
// if(transaction.blockHash!="0x0000000000000000000000000000000000000000000000000000000000000000"){
// var receipt = web3.eth.getTransactionReceipt(hash);
// console.log(receipt.status); // '0x1' indicates transaction succeeded.
// }
// }
// } else {
// console.log(err);
// }});