This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartMigration.js
78 lines (73 loc) · 2.01 KB
/
startMigration.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
const abi = require('./contractAbi.json')
const Web3 = require('web3')
const ws = require('ws')
const WebSocketServer = ws.Server
const web3Admin = require('web3admin')
const sleepSeconds = require('sleepjs').sleepSeconds
const requiredBalance = 9999999999
const web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'))
web3Admin.extend(web3)
const data = require('fs')
.readFileSync('deployData.txt')
function deployContract () {
try {
const account = web3.eth.accounts[0]
if (web3.eth.getBalance(account)
.toString(10) > requiredBalance) {
console.log('Starting deploying')
web3.miner.stop()
web3.eth.defaultAccount = account
web3.personal.unlockAccount(account, '1234567890')
const metascenarioContract = web3.eth.contract(abi)
metascenarioContract.new({
from: account,
data,
gas: '4700000',
}, (error, contract) => {
web3.miner.start()
if (error) {
console.log('&&&&&&&&&&&&&&&&&&&&&&&InContractError&&&&&&&&&&&&&&&&&&&&')
console.log(error)
sleepSeconds(5)
.then(deployContract)
}
else if (!contract.address) {
console.log('no address yet')
}
else {
startWebSocket(contract.address)
}
})
}
else {
sleepSeconds(10)
.then(deployContract)
}
}
catch (error) {
console.log(error)
sleepSeconds(10)
.then(deployContract)
}
}
function startWebSocket (contractAddress) {
try {
const wsServer = new WebSocketServer({port: 40000})
wsServer.on('connection', (connection) => {
connection.send(contractAddress)
})
ws.onerror = function (error) {
console.log('Contract address WebSocket not reachable', error)
ws.close()
}
ws.onclose = function () {
sleepSeconds(10)
.then(() => startWebSocket(contractAddress))
}
}
catch (error) {
sleepSeconds(10)
.then(() => startWebSocket(contractAddress))
}
}
deployContract()