Skip to content

Commit

Permalink
[IIP-13] contract staking struct definition (1/3 split by 3853) (#3861)
Browse files Browse the repository at this point in the history
* contractstaking bucket definition

* address comments
  • Loading branch information
envestcc committed May 22, 2023
1 parent f899c86 commit 20e6921
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 0 deletions.
26 changes: 26 additions & 0 deletions action/protocol/staking/contractstaking/bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 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 contractstaking

import (
"math/big"

"github.com/iotexproject/iotex-address/address"
)

// Bucket defines the bucket struct for contract staking
type Bucket struct {
Index uint64
Candidate address.Address
Owner address.Address
StakedAmount *big.Int
StakedDurationBlockNumber uint64
CreateBlockHeight uint64
StakeStartBlockHeight uint64
UnstakeStartBlockHeight uint64
AutoStake bool
ContractAddress string // contract address for the bucket
}
70 changes: 70 additions & 0 deletions action/protocol/staking/contractstaking/bucket_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2023 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 contractstaking

import (
"github.com/iotexproject/iotex-address/address"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol/staking/contractstaking/contractstakingpb"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

type (
// bucketInfo is the bucket information
bucketInfo struct {
TypeIndex uint64
CreatedAt uint64
UnlockedAt uint64
UnstakedAt uint64
Delegate address.Address // owner address of the delegate
Owner address.Address
}
)

// Serialize serializes the bucket info
func (bi *bucketInfo) Serialize() []byte {
return byteutil.Must(proto.Marshal(bi.toProto()))
}

// Deserialize deserializes the bucket info
func (bi *bucketInfo) Deserialize(b []byte) error {
m := contractstakingpb.BucketInfo{}
if err := proto.Unmarshal(b, &m); err != nil {
return err
}
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 {
delegate, err := address.FromString(p.Delegate)
if err != nil {
return err
}
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
}
58 changes: 58 additions & 0 deletions action/protocol/staking/contractstaking/bucket_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023 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 contractstaking

import (
"math/big"

"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol/staking/contractstaking/contractstakingpb"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

type (
// BucketType defines the type of contract staking bucket
BucketType struct {
Amount *big.Int
Duration uint64 // block numbers
ActivatedAt uint64 // block height
}
)

// 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(),
Duration: bt.Duration,
ActivatedAt: bt.ActivatedAt,
}
}

func (bt *BucketType) loadProto(p *contractstakingpb.BucketType) error {
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
}
Loading

0 comments on commit 20e6921

Please sign in to comment.