/
burn.go
77 lines (63 loc) · 1.81 KB
/
burn.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
package node
import (
"fmt"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/urfave/cli"
"github.com/Seb369888/smartnode/shared/services/gas"
"github.com/Seb369888/smartnode/shared/services/rocketpool"
cliutils "github.com/Seb369888/smartnode/shared/utils/cli"
"github.com/Seb369888/smartnode/shared/utils/math"
)
func nodeBurn(c *cli.Context, amount float64, token string) error {
// Get RP client
rp, err := rocketpool.NewClientFromCtx(c)
if err != nil {
return err
}
defer rp.Close()
// Check and assign the EC status
err = cliutils.CheckClientStatus(rp)
if err != nil {
return err
}
// Get amount in wei
amountWei := eth.EthToWei(amount)
// Check tokens can be burned
canBurn, err := rp.CanNodeBurn(amountWei, token)
if err != nil {
return err
}
if !canBurn.CanBurn {
fmt.Println("Cannot burn tokens:")
if canBurn.InsufficientBalance {
fmt.Printf("The node's %s balance is insufficient.\n", token)
}
if canBurn.InsufficientCollateral {
fmt.Printf("There is insufficient ETH collateral to trade %s for.\n", token)
}
return nil
}
// Assign max fees
err = gas.AssignMaxFeeAndLimit(canBurn.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm(fmt.Sprintf("Are you sure you want to burn %.6f %s for ETH?", math.RoundDown(eth.WeiToEth(amountWei), 6), token))) {
fmt.Println("Cancelled.")
return nil
}
// Burn tokens
response, err := rp.NodeBurn(amountWei, token)
if err != nil {
return err
}
fmt.Printf("Burning tokens...\n")
cliutils.PrintTransactionHash(rp, response.TxHash)
if _, err = rp.WaitForTransaction(response.TxHash); err != nil {
return err
}
// Log & return
fmt.Printf("Successfully burned %.6f %s for ETH.\n", math.RoundDown(eth.WeiToEth(amountWei), 6), token)
return nil
}