diff --git a/action/protocol/staking/contractstaking/bucket.go b/action/protocol/staking/contractstaking/bucket.go index 0e34336acc..b663c8dd92 100644 --- a/action/protocol/staking/contractstaking/bucket.go +++ b/action/protocol/staking/contractstaking/bucket.go @@ -22,7 +22,7 @@ type Bucket struct { StakeStartBlockHeight uint64 UnstakeStartBlockHeight uint64 AutoStake bool - ContractAddress string + ContractAddress string // contract address for the bucket } func assembleBucket(token uint64, bi *bucketInfo, bt *BucketType) (*Bucket, error) { diff --git a/action/protocol/staking/contractstaking/bucket_info.go b/action/protocol/staking/contractstaking/bucket_info.go index ab23043c5f..8683a66880 100644 --- a/action/protocol/staking/contractstaking/bucket_info.go +++ b/action/protocol/staking/contractstaking/bucket_info.go @@ -25,18 +25,6 @@ type ( } ) -func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo { - pb := &contractstakingpb.BucketInfo{ - TypeIndex: bi.TypeIndex, - Delegate: bi.Delegate.String(), - CreatedAt: bi.CreatedAt, - Owner: bi.Owner.String(), - UnlockedAt: bi.UnlockedAt, - UnstakedAt: bi.UnstakedAt, - } - return pb -} - // Serialize serializes the bucket info func (bi *bucketInfo) Serialize() []byte { return byteutil.Must(proto.Marshal(bi.toProto())) @@ -51,19 +39,32 @@ func (bi *bucketInfo) Deserialize(b []byte) error { return bi.loadProto(&m) } +func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo { + pb := &contractstakingpb.BucketInfo{ + TypeIndex: bi.TypeIndex, + Delegate: bi.Delegate.String(), + CreatedAt: bi.CreatedAt, + Owner: bi.Owner.String(), + UnlockedAt: bi.UnlockedAt, + UnstakedAt: bi.UnstakedAt, + } + return pb +} + func (bi *bucketInfo) loadProto(p *contractstakingpb.BucketInfo) error { - var err error - bi.TypeIndex = p.TypeIndex - bi.CreatedAt = p.CreatedAt - bi.UnlockedAt = p.UnlockedAt - bi.UnstakedAt = p.UnstakedAt - bi.Delegate, err = address.FromString(p.Delegate) + delegate, err := address.FromString(p.Delegate) if err != nil { return err } - bi.Owner, err = address.FromString(p.Owner) + owner, err := address.FromString(p.Owner) if err != nil { return err } + bi.TypeIndex = p.TypeIndex + bi.CreatedAt = p.CreatedAt + bi.UnlockedAt = p.UnlockedAt + bi.UnstakedAt = p.UnstakedAt + bi.Delegate = delegate + bi.Owner = owner return nil } diff --git a/action/protocol/staking/contractstaking/bucket_type.go b/action/protocol/staking/contractstaking/bucket_type.go index f26fc5ee14..98fca73346 100644 --- a/action/protocol/staking/contractstaking/bucket_type.go +++ b/action/protocol/staking/contractstaking/bucket_type.go @@ -24,6 +24,20 @@ type ( } ) +// Serialize serializes the bucket type +func (bt *BucketType) Serialize() []byte { + return byteutil.Must(proto.Marshal(bt.toProto())) +} + +// Deserialize deserializes the bucket type +func (bt *BucketType) Deserialize(b []byte) error { + m := contractstakingpb.BucketType{} + if err := proto.Unmarshal(b, &m); err != nil { + return err + } + return bt.loadProto(&m) +} + func (bt *BucketType) toProto() *contractstakingpb.BucketType { return &contractstakingpb.BucketType{ Amount: bt.Amount.String(), @@ -33,26 +47,12 @@ func (bt *BucketType) toProto() *contractstakingpb.BucketType { } func (bt *BucketType) loadProto(p *contractstakingpb.BucketType) error { - var ok bool - bt.Amount, ok = big.NewInt(0).SetString(p.Amount, 10) + amount, ok := big.NewInt(0).SetString(p.Amount, 10) if !ok { return errors.New("failed to parse amount") } + bt.Amount = amount bt.Duration = p.Duration bt.ActivatedAt = p.ActivatedAt return nil } - -// Serialize serializes the bucket type -func (bt *BucketType) Serialize() []byte { - return byteutil.Must(proto.Marshal(bt.toProto())) -} - -// Deserialize deserializes the bucket type -func (bt *BucketType) Deserialize(b []byte) error { - m := contractstakingpb.BucketType{} - if err := proto.Unmarshal(b, &m); err != nil { - return err - } - return bt.loadProto(&m) -}