/
stake_transferownership.go
111 lines (94 loc) · 3.39 KB
/
stake_transferownership.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
// Copyright (c) 2020 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"math/big"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/pkg/version"
)
// TransferStake defines the action of transfering stake ownership ts the other
type TransferStake struct {
AbstractAction
voterAddress address.Address
bucketIndex uint64
payload []byte
}
// NewTransferStake returns a TransferStake instance
func NewTransferStake(
nonce uint64,
voterAddress string,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*TransferStake, error) {
voterAddr, err := address.FromString(voterAddress)
if err != nil {
return nil, errors.Wrap(err, "failed to load address from string")
}
return &TransferStake{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
voterAddress: voterAddr,
bucketIndex: bucketIndex,
payload: payload,
}, nil
}
// VoterAddress returns the address of recipient
func (ts *TransferStake) VoterAddress() address.Address { return ts.voterAddress }
// BucketIndex returns bucket index
func (ts *TransferStake) BucketIndex() uint64 { return ts.bucketIndex }
// Payload returns the payload bytes
func (ts *TransferStake) Payload() []byte { return ts.payload }
// Serialize returns a raw byte stream of the transfer stake action struct
func (ts *TransferStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(ts.Proto()))
}
// Proto converts transfer stake to protobuf
func (ts *TransferStake) Proto() *iotextypes.StakeTransferOwnership {
act := &iotextypes.StakeTransferOwnership{
VoterAddress: ts.voterAddress.String(),
BucketIndex: ts.bucketIndex,
Payload: ts.payload,
}
return act
}
// LoadProto loads transfer stake protobuf
func (ts *TransferStake) LoadProto(pbAct *iotextypes.StakeTransferOwnership) error {
if pbAct == nil {
return errors.New("empty action proto to load")
}
voterAddress, err := address.FromString(pbAct.GetVoterAddress())
if err != nil {
return errors.Wrap(err, "failed to load address from string")
}
ts.voterAddress = voterAddress
ts.bucketIndex = pbAct.GetBucketIndex()
ts.payload = pbAct.GetPayload()
return nil
}
// IntrinsicGas returns the intrinsic gas of a TransferStake
func (ts *TransferStake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(ts.Payload()))
return calculateIntrinsicGas(MoveStakeBaseIntrinsicGas, MoveStakePayloadGas, payloadSize)
}
// Cost returns the tstal cost of a TransferStake
func (ts *TransferStake) Cost() (*big.Int, error) {
intrinsicGas, err := ts.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed ts get intrinsic gas for the TransferStake")
}
transferStakeFee := big.NewInt(0).Mul(ts.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return transferStakeFee, nil
}