-
Notifications
You must be signed in to change notification settings - Fork 319
/
stake_create.go
277 lines (244 loc) · 7.22 KB
/
stake_create.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2020 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package action
import (
"bytes"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
)
const (
// CreateStakePayloadGas represents the CreateStake payload gas per uint
CreateStakePayloadGas = uint64(100)
// CreateStakeBaseIntrinsicGas represents the base intrinsic gas for CreateStake
CreateStakeBaseIntrinsicGas = uint64(10000)
_createStakeInterfaceABI = `[
{
"inputs": [
{
"internalType": "string",
"name": "candName",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint32",
"name": "duration",
"type": "uint32"
},
{
"internalType": "bool",
"name": "autoStake",
"type": "bool"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "createStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
var (
// _createStakeMethod is the interface of the abi encoding of stake action
_createStakeMethod abi.Method
_ EthCompatibleAction = (*CreateStake)(nil)
errDecodeFailure = errors.New("failed to decode the data")
)
// CreateStake defines the action of CreateStake creation
type CreateStake struct {
AbstractAction
candName string
amount *big.Int
duration uint32
autoStake bool
payload []byte
}
func init() {
createStakeInterface, err := abi.JSON(strings.NewReader(_createStakeInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_createStakeMethod, ok = createStakeInterface.Methods["createStake"]
if !ok {
panic("fail to load the method")
}
}
// NewCreateStake returns a CreateStake instance
func NewCreateStake(
nonce uint64,
candidateName, amount string,
duration uint32,
autoStake bool,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*CreateStake, error) {
stake, ok := new(big.Int).SetString(amount, 10)
if !ok {
return nil, errors.Wrapf(ErrInvalidAmount, "amount %s", amount)
}
return &CreateStake{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
candName: candidateName,
amount: stake,
duration: duration,
autoStake: autoStake,
payload: payload,
}, nil
}
// Amount returns the amount
func (cs *CreateStake) Amount() *big.Int { return cs.amount }
// Payload returns the payload bytes
func (cs *CreateStake) Payload() []byte { return cs.payload }
// Candidate returns the candidate name
func (cs *CreateStake) Candidate() string { return cs.candName }
// Duration returns the CreateStaked duration
func (cs *CreateStake) Duration() uint32 { return cs.duration }
// AutoStake returns the flag of AutoStake s
func (cs *CreateStake) AutoStake() bool { return cs.autoStake }
// Serialize returns a raw byte stream of the CreateStake struct
func (cs *CreateStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(cs.Proto()))
}
// Proto converts to protobuf CreateStake Action
func (cs *CreateStake) Proto() *iotextypes.StakeCreate {
act := iotextypes.StakeCreate{
CandidateName: cs.candName,
StakedDuration: cs.duration,
AutoStake: cs.autoStake,
}
if cs.amount != nil {
act.StakedAmount = cs.amount.String()
}
if len(cs.payload) > 0 {
act.Payload = make([]byte, len(cs.payload))
copy(act.Payload, cs.payload)
}
return &act
}
// LoadProto converts a protobuf's Action to CreateStake
func (cs *CreateStake) LoadProto(pbAct *iotextypes.StakeCreate) error {
if pbAct == nil {
return ErrNilProto
}
cs.candName = pbAct.GetCandidateName()
cs.duration = pbAct.StakedDuration
cs.autoStake = pbAct.AutoStake
if len(pbAct.GetStakedAmount()) > 0 {
var ok bool
if cs.amount, ok = new(big.Int).SetString(pbAct.StakedAmount, 10); !ok {
return errors.Errorf("invalid amount %s", pbAct.StakedAmount)
}
}
cs.payload = nil
if len(pbAct.Payload) > 0 {
cs.payload = make([]byte, len(pbAct.Payload))
copy(cs.payload, pbAct.Payload)
}
return nil
}
// IntrinsicGas returns the intrinsic gas of a CreateStake
func (cs *CreateStake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(cs.Payload()))
return CalculateIntrinsicGas(CreateStakeBaseIntrinsicGas, CreateStakePayloadGas, payloadSize)
}
// Cost returns the total cost of a CreateStake
func (cs *CreateStake) Cost() (*big.Int, error) {
intrinsicGas, err := cs.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the CreateStake creates")
}
CreateStakeFee := big.NewInt(0).Mul(cs.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return big.NewInt(0).Add(cs.Amount(), CreateStakeFee), nil
}
// SanityCheck validates the variables in the action
func (cs *CreateStake) SanityCheck() error {
if cs.Amount().Sign() <= 0 {
return errors.Wrap(ErrInvalidAmount, "negative value")
}
if !IsValidCandidateName(cs.candName) {
return ErrInvalidCanName
}
return cs.AbstractAction.SanityCheck()
}
// EncodeABIBinary encodes data in abi encoding
func (cs *CreateStake) EncodeABIBinary() ([]byte, error) {
return cs.encodeABIBinary()
}
func (cs *CreateStake) encodeABIBinary() ([]byte, error) {
data, err := _createStakeMethod.Inputs.Pack(cs.candName, cs.amount, cs.duration, cs.autoStake, cs.payload)
if err != nil {
return nil, err
}
return append(_createStakeMethod.ID, data...), nil
}
// NewCreateStakeFromABIBinary decodes data into createStake action
func NewCreateStakeFromABIBinary(data []byte) (*CreateStake, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
cs CreateStake
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_createStakeMethod.ID, data[:4]) {
return nil, errDecodeFailure
}
if err := _createStakeMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if cs.candName, ok = paramsMap["candName"].(string); !ok {
return nil, errDecodeFailure
}
if cs.amount, ok = paramsMap["amount"].(*big.Int); !ok {
return nil, errDecodeFailure
}
if cs.duration, ok = paramsMap["duration"].(uint32); !ok {
return nil, errDecodeFailure
}
if cs.autoStake, ok = paramsMap["autoStake"].(bool); !ok {
return nil, errDecodeFailure
}
if cs.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &cs, nil
}
// ToEthTx converts action to eth-compatible tx
func (cs *CreateStake) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := cs.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: cs.Nonce(),
GasPrice: cs.GasPrice(),
Gas: cs.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
}