forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
179 lines (152 loc) · 5.43 KB
/
tx.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
package helper
import (
"context"
"encoding/hex"
"math/big"
ethereum "github.com/maticnetwork/bor"
"github.com/maticnetwork/bor/accounts/abi/bind"
"github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/crypto"
"github.com/maticnetwork/bor/ethclient"
"github.com/maticnetwork/heimdall/contracts/erc20"
"github.com/maticnetwork/heimdall/contracts/rootchain"
"github.com/maticnetwork/heimdall/contracts/slashmanager"
"github.com/maticnetwork/heimdall/contracts/stakemanager"
)
func GenerateAuthObj(client *ethclient.Client, address common.Address, data []byte) (auth *bind.TransactOpts, err error) {
// generate call msg
callMsg := ethereum.CallMsg{
To: &address,
Data: data,
}
// get priv key
pkObject := GetPrivKey()
// create ecdsa private key
ecdsaPrivateKey, err := crypto.ToECDSA(pkObject[:])
if err != nil {
return
}
// from address
fromAddress := common.BytesToAddress(pkObject.PubKey().Address().Bytes())
// fetch gas price
gasprice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return
}
// fetch nonce
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return
}
// fetch gas limit
callMsg.From = fromAddress
gasLimit, err := client.EstimateGas(context.Background(), callMsg)
// create auth
auth = bind.NewKeyedTransactor(ecdsaPrivateKey)
auth.GasPrice = gasprice
auth.Nonce = big.NewInt(int64(nonce))
auth.GasLimit = uint64(gasLimit) // uint64(gasLimit)
return
}
// SendCheckpoint sends checkpoint to rootchain contract
// todo return err
func (c *ContractCaller) SendCheckpoint(signedData []byte, sigs []byte, rootChainAddress common.Address, rootChainInstance *rootchain.Rootchain) (er error) {
data, err := c.RootChainABI.Pack("submitHeaderBlock", signedData, sigs)
if err != nil {
Logger.Error("Unable to pack tx for submitHeaderBlock", "error", err)
return err
}
auth, err := GenerateAuthObj(GetMainClient(), rootChainAddress, data)
if err != nil {
Logger.Error("Unable to create auth object", "error", err)
Logger.Info("Setting custom gaslimit", "gaslimit", GetConfig().MainchainGasLimit)
auth.GasLimit = GetConfig().MainchainGasLimit
}
Logger.Debug("Sending new checkpoint",
"sigs", hex.EncodeToString(sigs),
"data", hex.EncodeToString(signedData),
)
tx, err := rootChainInstance.SubmitHeaderBlock(auth, signedData, sigs)
if err != nil {
Logger.Error("Error while submitting checkpoint", "error", err)
return err
}
Logger.Info("Submitted new checkpoint to rootchain successfully", "txHash", tx.Hash().String())
return
}
// SendTick sends slash tick to rootchain contract
func (c *ContractCaller) SendTick(signedData []byte, sigs []byte, slashManagerAddress common.Address, slashManagerInstance *slashmanager.Slashmanager) (er error) {
data, err := c.SlashManagerABI.Pack("updateSlashedAmounts", signedData, sigs)
if err != nil {
Logger.Error("Unable to pack tx for updateSlashedAmounts", "error", err)
return err
}
auth, err := GenerateAuthObj(GetMainClient(), slashManagerAddress, data)
if err != nil {
Logger.Error("Unable to create auth object", "error", err)
Logger.Info("Setting custom gaslimit", "gaslimit", GetConfig().MainchainGasLimit)
auth.GasLimit = GetConfig().MainchainGasLimit
}
Logger.Info("Sending new tick",
"sigs", hex.EncodeToString(sigs),
"data", hex.EncodeToString(signedData),
)
tx, err := slashManagerInstance.UpdateSlashedAmounts(auth, signedData, sigs)
if err != nil {
Logger.Error("Error while submitting tick", "error", err)
return err
}
Logger.Info("Submitted new tick to slashmanager successfully", "txHash", tx.Hash().String())
return
}
// StakeFor stakes for a validator
func (c *ContractCaller) StakeFor(val common.Address, stakeAmount *big.Int, feeAmount *big.Int, acceptDelegation bool, stakeManagerAddress common.Address, stakeManagerInstance *stakemanager.Stakemanager) error {
signerPubkey := GetPubKey()
signerPubkeyBytes := signerPubkey[1:] // remove 04 prefix
// pack data based on method definition
data, err := c.StakeManagerABI.Pack("stakeFor", val, stakeAmount, feeAmount, acceptDelegation, signerPubkeyBytes)
if err != nil {
Logger.Error("Unable to pack tx for stakeFor", "error", err)
return err
}
auth, err := GenerateAuthObj(GetMainClient(), stakeManagerAddress, data)
if err != nil {
Logger.Error("Unable to create auth object", "error", err)
return err
}
// stake for stake manager
tx, err := stakeManagerInstance.StakeFor(
auth,
val,
stakeAmount,
feeAmount,
acceptDelegation,
signerPubkeyBytes,
)
if err != nil {
Logger.Error("Error while submitting stake", "error", err)
return err
}
Logger.Info("Submitted stake sucessfully", "txHash", tx.Hash().String())
return nil
}
// ApproveTokens approves matic token for stake
func (c *ContractCaller) ApproveTokens(amount *big.Int, stakeManager common.Address, tokenAddress common.Address, maticTokenInstance *erc20.Erc20) error {
data, err := c.MaticTokenABI.Pack("approve", stakeManager, amount)
if err != nil {
Logger.Error("Unable to pack tx for approve", "error", err)
return err
}
auth, err := GenerateAuthObj(GetMainClient(), tokenAddress, data)
if err != nil {
Logger.Error("Unable to create auth object", "error", err)
return err
}
tx, err := maticTokenInstance.Approve(auth, stakeManager, amount)
if err != nil {
Logger.Error("Error while approving approve", "error", err)
return err
}
Logger.Info("Sent approve tx sucessfully", "txHash", tx.Hash().String())
return nil
}