-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
126 lines (103 loc) · 3.91 KB
/
index.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
const { exec } = require('child_process')
const path = require('path')
const shell = require('shelljs')
const Web3 = require('web3')
const assert = require('assert')
const chalk = require('chalk')
const kill = require('tree-kill')
const configDir = path.join(__dirname, 'config')
const deployContractsDir = path.join(__dirname, 'submodules/poa-bridge-contracts/deploy')
const abisDir = path.join(__dirname, 'submodules/poa-bridge-contracts/build/contracts')
const bridgeDir = path.join(__dirname, 'submodules/bridge-nodejs')
const privateKeyUser = '0x63e48a8ba0b99e0377c6b483af4a072cbca5ffbcfdac77be72e69f4960125800'
const homeWeb3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
const foreignWeb3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8555'))
const { toBN } = foreignWeb3.utils
homeWeb3.eth.accounts.wallet.add(privateKeyUser)
const tokenAbi = require(path.join(abisDir, 'POA20.json')).abi
const token = new foreignWeb3.eth.Contract(tokenAbi, '0xdbeE25CbE97e4A5CC6c499875774dc7067E9426B')
const sleep = timeout => new Promise(res => setTimeout(res, timeout))
function waitUntil(fn, timeoutMs) {
function tryIt(fn, cb) {
Promise.resolve(fn())
.then(result => {
if (result) {
cb(true)
} else {
setTimeout(() => {
tryIt(fn, cb)
}, 50)
}
})
}
return new Promise(resolve => {
const timeout = setTimeout(() => resolve(false), timeoutMs)
tryIt(fn, (result) => {
resolve(result)
clearTimeout(timeout)
})
})
}
function waitForOutput(proc, str) {
return new Promise((resolve, reject) => {
proc.stdout.on('data', (data) => {
if (data.includes(str)) {
resolve()
}
})
proc.stderr.on('data', (data) => {
if (data.includes(str)) {
resolve()
}
})
proc.on('error', reject)
})
}
async function main() {
shell.rm('-rf', './data')
// start parity nodes
console.log(chalk.blue('start parity nodes'))
const homeParity = exec('parity --chain ./config/chain.json -d ./data/data-home --reseal-min-period 0')
const foreignParity = exec('parity --chain ./config/chain.json -d ./data/data-foreign --reseal-min-period 0 --ports-shift 10')
await Promise.all([waitForOutput(homeParity, 'Public node URL'), waitForOutput(foreignParity, 'Public node URL')])
// deploy bridge contracts
shell.cp(path.join(configDir, 'contracts-deploy.env'), path.join(deployContractsDir, '.env'))
console.log(chalk.blue('deploy contracts'))
shell.cd(deployContractsDir)
shell.exec('node deploy.js')
// start bridge
shell.cp(path.join(configDir, 'bridge.env'), path.join(bridgeDir, '.env'))
console.log(chalk.blue('start bridge'))
shell.cd(bridgeDir)
shell.exec('git checkout db')
const bridge = shell.exec('node src/start.js', { async: true })
// check that account has zero tokens in the foreign chain
console.log(chalk.blue('check balance before tx'))
let balance = await token.methods.balanceOf('0xbb140FbA6242a1c3887A7823F7750a73101383e3').call()
assert(toBN(balance).isZero(), 'Account should not have tokens yet')
// send transaction to home chain
console.log(chalk.blue('send transaction'))
await homeWeb3.eth.sendTransaction({
from: '0xbb140FbA6242a1c3887A7823F7750a73101383e3',
to: '0x32198D570fffC7033641F8A9094FFDCaAEF42624',
gasPrice: '1',
gasLimit: 50000,
value: '1000000000000000000'
})
// check that account has tokens in the foreign chain
console.log(chalk.blue('check balance after transaction'))
const satisfied = await waitUntil(async () => {
const balance = await token.methods.balanceOf('0xbb140FbA6242a1c3887A7823F7750a73101383e3').call()
return toBN(balance).gt(toBN(0))
}, 30000)
assert(satisfied, 'Account should have tokens')
console.log(chalk.blue('kill child processes'))
kill(homeParity.pid)
kill(foreignParity.pid)
kill(bridge.pid)
}
try {
main()
} catch (e) {
console.error(e)
}