/
down.go
259 lines (231 loc) · 7.28 KB
/
down.go
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
package main
import (
"bufio"
"context"
"fmt"
"math"
"math/big"
"os"
"path"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/renproject/libeth-go"
"github.com/republicprotocol/darknode-go/adapter/ethcontract"
"github.com/republicprotocol/darknode-go/adapter/ethcontract/bindings"
"github.com/republicprotocol/darknode-go/adapter/ethcontract/dnr"
"github.com/republicprotocol/darknode-go/cmd/darknode/config"
"github.com/republicprotocol/ren-go/foundation/addr"
"github.com/urfave/cli"
)
// destroyNode tears down the deployed darknode by its name.
func destroyNode(ctx *cli.Context) error {
force := ctx.Bool("force")
name := ctx.Args().First()
if name == "" {
cli.ShowCommandHelp(ctx, "down")
return ErrEmptyNodeName
}
// Read the node config
nodePath := nodePath(name)
config, err := config.NewConfigFromJSONFile(path.Join(nodePath, "config.json"))
if err != nil {
return err
}
network := config.Ethereum.Network
address := addr.New(config.Address)
// Connect to Ethereum
_, dnr, err := connect(network)
if err != nil {
return err
}
// Check if the node is registered
if err := checkRegistered(dnr, network, address); err != nil {
return err
}
// Check if the node is in pending registration/deregistration stage
if err := checkPendingStage(dnr, address); err != nil {
return err
}
// Check if the darknode has been refunded
context, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
refunded, err := dnr.Refunded(context, address)
if err != nil {
return err
}
if !refunded {
fmt.Println("You haven't refund your darknode. Please refund your darknode from the command center")
return nil
}
// Check if user want to process without extra confirmation
if !force {
fmt.Println("Do you really want to destroy your darknode? (Yes/No)")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
input := strings.ToLower(strings.TrimSpace(text))
if input != "yes" && input != "y" {
return nil
}
}
fmt.Printf("%sDestroying your darknode ...%s\n", RESET, RESET)
destroy := fmt.Sprintf("cd %v && terraform destroy --force && find . -type f -not -name 'config.json' -delete", nodePath)
return run("bash", "-c", destroy)
}
// Withdraw ETH and REN in the darknode address to the provided receiver address
func withdraw(ctx *cli.Context) error {
name := ctx.Args().First()
withdrawAddress := ctx.String("address")
// Validate the name and received ethereum address
nodePath, err := validateDarknodeName(name)
if err != nil {
return err
}
receiverAddr, err := stringToEthereumAddress(withdrawAddress)
if err != nil {
return err
}
// Read config of the specified darknode
config, err := config.NewConfigFromJSONFile(path.Join(nodePath, "config.json"))
if err != nil {
return err
}
network := config.Ethereum.Network
// Connect to Ethereum
client, _, err := connect(network)
if err != nil {
return err
}
account, err := libeth.NewAccount(client, config.Keystore.EcdsaKey.PrivateKey)
if err != nil {
return err
}
darknodeEthAddress, err := republicAddressToEthAddress(config.Address)
if err != nil {
return err
}
auth := bind.NewKeyedTransactor(config.Keystore.EcdsaKey.PrivateKey)
auth.GasPrice = big.NewInt(5000000000) // Set GasPrise to 5 Gwei
// Check REN balance first
renAddress := renAddress(network)
if renAddress == "" {
return ErrUnknownNetwork
}
tokenContract, err := bindings.NewERC20(common.HexToAddress(renAddress), bind.ContractBackend(client.EthClient()))
if err != nil {
return err
}
renBalance, err := tokenContract.BalanceOf(&bind.CallOpts{}, darknodeEthAddress)
if err != nil {
return err
}
// Withdraw REN if the darknode has more than 1 REN.
oneREN := big.NewInt(int64(math.Pow10(18)))
if renBalance.Cmp(oneREN) > 0 {
tx, err := tokenContract.Transfer(auth, receiverAddr, renBalance)
if err != nil {
return err
}
minedCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, err = bind.WaitMined(minedCtx, client.EthClient(), tx)
if err != nil {
return err
}
fmt.Printf("%sYour REN has been withdrawn from your darknode to [%v]. TxHash: %v.%s\n", GREEN, receiverAddr.Hex(), tx.Hash().Hex(), RESET)
}
ethCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// Check the ETH balance
balance, err := account.BalanceAt(ethCtx, nil)
if err != nil {
return err
}
if balance.Cmp(big.NewInt(0)) == 0 {
return nil
}
tx, err := account.Transfer(ethCtx, receiverAddr, nil, libeth.Fast, 0, true)
if err != nil {
return err
}
fmt.Printf("%sYour ETH has been withdrawn from your darknode to [%v]. TxHash: %v.%s\n", GREEN, receiverAddr.Hex(), tx.Hash().Hex(), RESET)
return nil
}
// renAddress on different testnet
func renAddress(network string) string {
switch network {
case "mainnet":
return "0x408e41876cCCDC0F92210600ef50372656052a38"
case "kovan", "testnet":
return "0x2CD647668494c1B15743AB283A0f980d90a87394"
default:
return ""
}
}
func connect(network string) (libeth.Client, dnr.Caller, error) {
client, err := libeth.NewMercuryClient(network, "dcc")
if err != nil {
return libeth.Client{}, nil, err
}
contract, err := ethcontract.NewCaller(client).DarknodeRegistry()
if err != nil {
return libeth.Client{}, nil, err
}
return client, contract, nil
}
func checkRegistered(dnr dnr.Caller, network string, address addr.Addr) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
registered, err := dnr.IsRegistered(ctx, address)
if err != nil {
return err
}
if registered {
var url string
switch network {
case "testnet", "kovan":
url = fmt.Sprintf("https://dcc-testnet.republicprotocol.com/darknode/%v?action=deregister", address.String())
case "mainnet":
url = fmt.Sprintf("https://dcc.republicprotocol.com/darknode/%v?action=deregister", address.String())
default:
return ErrUnknownNetwork
}
fmt.Printf("%sYour node hasn't been deregistered%s\n", RED, RESET)
fmt.Printf("%sDeregister your darknode at %s%s\n", RED, url, RESET)
for i := 5; i >= 0; i-- {
time.Sleep(time.Second)
fmt.Printf("\r%sYou will be redirected to deregister your node in %v seconds%s", RED, i, RESET)
}
redirect, err := redirectCommand()
if err != nil {
return err
}
if err := run(redirect, url); err != nil {
return err
}
return fmt.Errorf("%s\nPlease try again after you fully deregister your node%s\n", RED, RESET)
}
return nil
}
func checkPendingStage(dnr dnr.Caller, address addr.Addr) error {
reCtx, reCancel := context.WithTimeout(context.Background(), time.Minute)
defer reCancel()
pendingRegistration, err := dnr.PendingRegistration(reCtx, address)
if err != nil {
return err
}
if pendingRegistration {
return fmt.Errorf("%sYour node is currently in pending registration stage, please deregister your node after next epoch shuffle%s\n", RED, RESET)
}
deCtx, deCancel := context.WithTimeout(context.Background(), time.Minute)
defer deCancel()
pendingDeregistration, err := dnr.PendingDeregistration(deCtx, address)
if err != nil {
return err
}
if pendingDeregistration {
return fmt.Errorf("%sYour node is currently in pending deregistration stage, please wait for next epoch shuffle and try again%s\n", RED, RESET)
}
return nil
}