-
Notifications
You must be signed in to change notification settings - Fork 15
/
exit-eth.js
132 lines (112 loc) · 4.83 KB
/
exit-eth.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
/*
Copyright 2019 OmiseGO Pte Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const Web3 = require('web3')
const RootChain = require('../packages/omg-js-rootchain/src/rootchain')
const ChildChain = require('../packages/omg-js-childchain/src/childchain')
const { transaction, waitForRootchainTransaction } = require('../packages/omg-js-util/src')
const config = require('./config.js')
const wait = require('./wait.js')
const getFlags = require('./parse-args')
const web3 = new Web3(new Web3.providers.HttpProvider(config.eth_node), null, { transactionConfirmationBlocks: 1 })
const rootChain = new RootChain({ web3, plasmaContractAddress: config.plasmaframework_contract_address })
const childChain = new ChildChain({ watcherUrl: config.watcher_url, watcherProxyUrl: config.watcher_proxy_url, plasmaContractAddress: config.plasmaframework_contract_address })
async function logBalances (address) {
const rootchainBalance = await web3.eth.getBalance(address)
const childchainBalanceArray = await childChain.getBalance(address)
const ethObject = childchainBalanceArray.find(i => i.currency === transaction.ETH_CURRENCY)
const childchainEthBalance = ethObject
? `${web3.utils.fromWei(String(ethObject.amount))} ETH`
: '0 ETH'
console.log(`Rootchain balance: ${web3.utils.fromWei(String(rootchainBalance), 'ether')} ETH`)
console.log(`Childchain balance: ${childchainEthBalance}`)
}
async function exitEth () {
try {
const { owner } = getFlags('owner')
const address = config[`${owner}_eth_address`]
const pk = config[`${owner}_eth_address_private_key`]
const rootchainBalance = await web3.eth.getBalance(address)
const etherBalance = web3.utils.fromWei(String(rootchainBalance), 'ether')
if (etherBalance < 0.001) {
console.log(`${owner} does not have enough ETH on the rootchain to start an exit`)
return
}
await logBalances(address)
console.log('-----')
// get ETH UTXO and exit data
const utxos = await childChain.getUtxos(address)
const utxoToExit = utxos.find(i => i.currency === transaction.ETH_CURRENCY)
if (!utxoToExit) {
console.log(`${owner} doesnt have any ETH UTXOs to exit`)
return
}
console.log(`${owner} wants to exit ${web3.utils.fromWei(String(utxoToExit.amount), 'ether')} ETH with this UTXO:\n${JSON.stringify(utxoToExit, undefined, 2)}`)
// check if queue exists for this token
const hasToken = await rootChain.hasToken(transaction.ETH_CURRENCY)
if (!hasToken) {
console.log(`Adding a ${transaction.ETH_CURRENCY} exit queue`)
await rootChain.addToken({
token: transaction.ETH_CURRENCY,
txOptions: { from: address, privateKey: pk }
})
}
// start a standard exit
const exitData = await childChain.getExitData(utxoToExit)
const standardExitReceipt = await rootChain.startStandardExit({
utxoPos: exitData.utxo_pos,
outputTx: exitData.txbytes,
inclusionProof: exitData.proof,
txOptions: {
privateKey: pk,
from: address
}
})
console.log('Started a standard exit: ', standardExitReceipt.transactionHash)
const exitId = await rootChain.getStandardExitId({
txBytes: exitData.txbytes,
utxoPos: exitData.utxo_pos,
isDeposit: utxoToExit.blknum % 1000 !== 0
})
console.log('Exit id: ', exitId)
const { msUntilFinalization } = await rootChain.getExitTime({
exitRequestBlockNumber: standardExitReceipt.blockNumber,
submissionBlockNumber: utxoToExit.blknum
})
await wait.wait(msUntilFinalization)
const processExitReceipt = await rootChain.processExits({
token: transaction.ETH_CURRENCY,
exitId: 0,
maxExitsToProcess: 20,
txOptions: {
privateKey: pk,
from: address
}
})
if (processExitReceipt) {
console.log(`ETH exits processing: ${processExitReceipt.transactionHash}`)
await waitForRootchainTransaction({
web3,
transactionHash: processExitReceipt.transactionHash,
checkIntervalMs: config.millis_to_wait_for_next_block,
blocksToWait: config.blocks_to_wait_for_txn,
onCountdown: (remaining) => console.log(`${remaining} blocks remaining before confirmation`)
})
}
console.log('Exits processed')
console.log('-----')
await logBalances(address)
} catch (error) {
console.log('Error: ', error.message)
}
}
exitEth()