-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IIP-13] contract staking struct definition (1/3 split by 3853) (#3861)
* contractstaking bucket definition * address comments
- Loading branch information
Showing
5 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.