-
Notifications
You must be signed in to change notification settings - Fork 319
/
stake_adddeposit.go
236 lines (207 loc) · 6.29 KB
/
stake_adddeposit.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
// 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 didslaimed.
// 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/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
)
const (
// DepositToStakePayloadGas represents the DepositToStake payload gas per uint
DepositToStakePayloadGas = uint64(100)
// DepositToStakeBaseIntrinsicGas represents the base intrinsic gas for DepositToStake
DepositToStakeBaseIntrinsicGas = uint64(10000)
_depositToStakeInterfaceABI = `[
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "depositToStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
var (
// _depositToStakeMethod is the interface of the abi encoding of stake action
_depositToStakeMethod abi.Method
_ EthCompatibleAction = (*DepositToStake)(nil)
)
// DepositToStake defines the action of stake add deposit
type DepositToStake struct {
AbstractAction
bucketIndex uint64
amount *big.Int
payload []byte
}
func init() {
depositToStakeInterface, err := abi.JSON(strings.NewReader(_depositToStakeInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_depositToStakeMethod, ok = depositToStakeInterface.Methods["depositToStake"]
if !ok {
panic("fail to load the method")
}
}
// NewDepositToStake returns a DepositToStake instance
func NewDepositToStake(
nonce uint64,
index uint64,
amount string,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*DepositToStake, error) {
stake, ok := new(big.Int).SetString(amount, 10)
if !ok {
return nil, errors.Wrapf(ErrInvalidAmount, "amount %s", amount)
}
return &DepositToStake{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
bucketIndex: index,
amount: stake,
payload: payload,
}, nil
}
// Amount returns the amount
func (ds *DepositToStake) Amount() *big.Int { return ds.amount }
// Payload returns the payload bytes
func (ds *DepositToStake) Payload() []byte { return ds.payload }
// BucketIndex returns bucket indexs
func (ds *DepositToStake) BucketIndex() uint64 { return ds.bucketIndex }
// Serialize returns a raw byte stream of the DepositToStake struct
func (ds *DepositToStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(ds.Proto()))
}
// Proto converts to protobuf DepositToStake Action
func (ds *DepositToStake) Proto() *iotextypes.StakeAddDeposit {
act := &iotextypes.StakeAddDeposit{
BucketIndex: ds.bucketIndex,
Payload: ds.payload,
}
if ds.amount != nil {
act.Amount = ds.amount.String()
}
return act
}
// LoadProto converts a protobuf's Action to DepositToStake
func (ds *DepositToStake) LoadProto(pbAct *iotextypes.StakeAddDeposit) error {
if pbAct == nil {
return ErrNilProto
}
ds.bucketIndex = pbAct.GetBucketIndex()
ds.payload = pbAct.GetPayload()
if pbAct.GetAmount() == "" {
ds.amount = big.NewInt(0)
} else {
amount, ok := new(big.Int).SetString(pbAct.GetAmount(), 10)
if !ok {
return errors.Errorf("invalid amount %s", pbAct.GetAmount())
}
ds.amount = amount
}
return nil
}
// IntrinsicGas returns the intrinsic gas of a DepositToStake
func (ds *DepositToStake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(ds.Payload()))
return CalculateIntrinsicGas(DepositToStakeBaseIntrinsicGas, DepositToStakePayloadGas, payloadSize)
}
// Cost returns the total cost of a DepositToStake
func (ds *DepositToStake) Cost() (*big.Int, error) {
intrinsicGas, err := ds.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the DepositToStake")
}
depositToStakeFee := big.NewInt(0).Mul(ds.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return big.NewInt(0).Add(ds.Amount(), depositToStakeFee), nil
}
// SanityCheck validates the variables in the action
func (ds *DepositToStake) SanityCheck() error {
if ds.Amount().Sign() <= 0 {
return errors.Wrap(ErrInvalidAmount, "negative value")
}
return ds.AbstractAction.SanityCheck()
}
// EncodeABIBinary encodes data in abi encoding
func (ds *DepositToStake) EncodeABIBinary() ([]byte, error) {
return ds.encodeABIBinary()
}
func (ds *DepositToStake) encodeABIBinary() ([]byte, error) {
data, err := _depositToStakeMethod.Inputs.Pack(ds.bucketIndex, ds.amount, ds.payload)
if err != nil {
return nil, err
}
return append(_depositToStakeMethod.ID, data...), nil
}
// NewDepositToStakeFromABIBinary decodes data into depositToStake action
func NewDepositToStakeFromABIBinary(data []byte) (*DepositToStake, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
ds DepositToStake
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_depositToStakeMethod.ID[:], data[:4]) {
return nil, errDecodeFailure
}
if err := _depositToStakeMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if ds.bucketIndex, ok = paramsMap["bucketIndex"].(uint64); !ok {
return nil, errDecodeFailure
}
if ds.amount, ok = paramsMap["amount"].(*big.Int); !ok {
return nil, errDecodeFailure
}
if ds.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &ds, nil
}
// ToEthTx converts action to eth-compatible tx
func (ds *DepositToStake) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := ds.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: ds.Nonce(),
GasPrice: ds.GasPrice(),
Gas: ds.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
}