/
contracts.js
169 lines (131 loc) · 4.62 KB
/
contracts.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
const Web3 = require('web3');
var fs = require('fs');
var config = require('./truffle.js');
var request = require('request');
var Tx = require('ethereumjs-tx');
var jsonDutchAuction = JSON.parse(fs.readFileSync('./build/contracts/DutchAuction.json'));
var jsonToken = JSON.parse(fs.readFileSync('./build/contracts/LetsbetToken.json'));
var txSender = config.auction.contract.owner;
var web3 = new Web3();
var httpProvider = config.provider.url;
console.log(httpProvider);
web3.setProvider(new Web3.providers.HttpProvider(httpProvider));
function DutchAuction(address){
this.address = address;
this.txManager = new TxManager();
}
DutchAuction.prototype._contract = function(){
var self = this;
var abi = jsonDutchAuction.abi;
return new web3.eth.Contract(abi,self.address);
}
DutchAuction.prototype.setup = function(tokenAddress){
var self = this;
var contract = self._contract();
console.log("Setup XBET token contract address: " + tokenAddress);
self.txManager.nextNonce(txSender,function (nonceNumber) {
console.log(nonceNumber);
var setupData = contract.methods.setup(tokenAddress).encodeABI();
var value = web3.utils.toWei("0",'ether');
var gasPrice = web3.utils.toWei("60".toString(),'gwei');
var rawTx = {
nonce: web3.utils.toHex(nonceNumber),
from: txSender,
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: web3.utils.toHex(1500000),
to: self.address,
value: web3.utils.toHex(value),
data: setupData
}
self.txManager.signTransaction(rawTx);
});
}
DutchAuction.prototype.finalize = function(){
var self = this;
var contract = self._contract();
self.txManager.nextNonce(txSender,function (nonceNumber) {
console.log(nonceNumber);
var finalizeData = contract.methods.finalizeAuction().encodeABI();
var value = web3.utils.toWei("0",'ether');
var gasPrice = web3.utils.toWei("21".toString(),'gwei');
var rawTx = {
nonce: web3.utils.toHex(nonceNumber),
from: txSender,
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: web3.utils.toHex(1500000),
to: self.address,
value: web3.utils.toHex(value),
data: finalizeData
}
self.txManager.signTransaction(rawTx);
});
}
DutchAuction.prototype.start = function(){
var self = this;
var contract = self._contract();
console.log("Start auction");
self.txManager.nextNonce(txSender,function (nonceNumber) {
console.log("Nonce" + nonceNumber);
var startData = contract.methods.startAuction().encodeABI();
var value = web3.utils.toWei("0",'ether');
var gasPrice = web3.utils.toWei("80".toString(),'gwei');
var rawTx = {
nonce: web3.utils.toHex(nonceNumber),
from: txSender,
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: web3.utils.toHex(1500000),
to: self.address,
value: web3.utils.toHex(value),
data: startData
}
self.txManager.signTransaction(rawTx);
});
}
DutchAuction.prototype.remainingFunds = function(callback){
var self = this;
var contract = self._contract();
contract.methods.missingFundsToEndAuction().call(function(error,result){
callback(error,result);
});
}
DutchAuction.prototype.endTimeOfBids = function(callback){
var self = this;
var contract = self._contract();
contract.methods.endTimeOfBids().call(function(error,result){
callback(error,result);
});
}
DutchAuction.prototype.price = function(callback){
var self = this;
var contract = self._contract();
contract.methods.price().call(function(error,result){
callback(error,result);
});
}
function TxManager(){
this.nonceMap ={};
}
TxManager.prototype.signTransaction = function(rawTx,key){
console.log(rawTx);
var privateKey = key || config.keys.private;
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);
}
TxManager.prototype.nextNonce = function (address, callback) {
var self = this;
web3.eth.getTransactionCount(address).then(function (nonce) {
var lastNonce = self.nonceMap[address];
if(lastNonce){
while (nonce <= lastNonce) {
nonce++;
}
}
self.nonceMap[address]=nonce;
callback(nonce);
});
}
module.exports = {
DutchAuction: DutchAuction
};