Skip to content

Commit

Permalink
refactor: use delegations inside genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Jul 1, 2024
1 parent c417060 commit 60b17a6
Show file tree
Hide file tree
Showing 9 changed files with 730 additions and 1,058 deletions.
8 changes: 4 additions & 4 deletions proto/milkyway/pools/v1/models.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ message Pool {

// Tokens define the delegated tokens.
string tokens = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
(gogoproto.nullable) = false
];

// DelegatorShares defines total shares issued to a pool's delegators.
string delegator_shares = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
(gogoproto.nullable) = false
];
}
51 changes: 4 additions & 47 deletions proto/milkyway/restaking/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package milkyway.restaking.v1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "milkyway/restaking/v1/models.proto";
import "milkyway/restaking/v1/params.proto";

option go_package = "github.com/milkyway-labs/milkyway/x/restaking/types";
Expand All @@ -13,58 +14,14 @@ message GenesisState {
Params params = 1 [ (gogoproto.nullable) = false ];

// PoolDelegations represents the delegations to pools.
repeated PoolDelegationEntry pools_delegations = 2
repeated PoolDelegation pools_delegations = 2
[ (gogoproto.nullable) = false ];

// ServiceDelegations represents the delegations to services.
repeated ServiceDelegationEntry services_delegations = 3
repeated ServiceDelegation services_delegations = 3
[ (gogoproto.nullable) = false ];

// OperatorDelegations represents the delegations to operators.
repeated OperatorDelegationEntry operators_delegations = 4
repeated OperatorDelegation operators_delegations = 4
[ (gogoproto.nullable) = false ];
}

// PoolDelegationEntry contains the data of a single restake delegation to a
// pool.
message PoolDelegationEntry {
// PoolID represents the ID of the pool to which the tokens have been restaked
uint32 pool_id = 1 [ (gogoproto.customname) = "PoolID" ];

// UserAddress represents the address of the user who has restaked the tokens
string user_address = 2;

// Amount represents the amount of tokens that have been restaked
cosmos.base.v1beta1.Coin amount = 3
[ (gogoproto.customname) = "Amount", (gogoproto.nullable) = false ];
}

// ServiceDelegationEntry contains the data of a single restake delegation to a
// service.
message ServiceDelegationEntry {
// ServiceID represents the ID of the service to which the tokens have been
// restaked
uint32 service_id = 1 [ (gogoproto.customname) = "ServiceID" ];

// UserAddress represents the address of the user who has restaked the tokens
string user_address = 2;

// Amount represents the amount of tokens that have been restaked
cosmos.base.v1beta1.Coin amount = 3
[ (gogoproto.customname) = "Amount", (gogoproto.nullable) = false ];
}

// OperatorDelegationEntry contains the data of a single restake delegation to
// an operator.
message OperatorDelegationEntry {
// OperatorID represents the ID of the operator to which the tokens have been
// restaked
uint32 operator_id = 1 [ (gogoproto.customname) = "OperatorID" ];

// UserAddress represents the address of the user who has restaked the tokens
string user_address = 2;

// Amount represents the amount of tokens that have been restaked
cosmos.base.v1beta1.Coin amount = 3
[ (gogoproto.customname) = "Amount", (gogoproto.nullable) = false ];
}
45 changes: 43 additions & 2 deletions proto/milkyway/restaking/v1/models.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,53 @@ message PoolDelegation {
option (gogoproto.goproto_getters) = false;

// UserAddress is the encoded address of the user.
string user_address = 1
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string user_address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// PoolID is the id of the pool.
uint32 pool_id = 2 [ (gogoproto.customname) = "PoolID" ];

// Shares define the delegation shares received.
string shares = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

// OperatorDelegation represents the bond with tokens held by an account with a
// given operator. It is owned by one delegator, and is associated with a
// operator.
message OperatorDelegation {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// UserAddress is the encoded address of the user.
string user_address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// OperatorID is the id of the operator.
uint32 operator_id = 2 [ (gogoproto.customname) = "OperatorID" ];

// Shares define the delegation shares received.
string shares = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

// ServiceDelegation represents the bond with tokens held by an account with a
// given service. It is owned by one delegator, and is associated with a
// service.
message ServiceDelegation {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// UserAddress is the encoded address of the user.
string user_address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// ServiceID is the id of the service.
uint32 service_id = 2 [ (gogoproto.customname) = "ServiceID" ];

// Shares define the delegation shares received.
string shares = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
Expand Down
4 changes: 2 additions & 2 deletions x/restaking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (

var (
ErrInvalidGenesis = errors.Register(ModuleName, 1, "invalid genesis state")
ErrDelegationNotFound = errors.Register(ModuleName, 2, "delegation not found")
ErrDelegatorShareExRateInvalid = errors.Register(ModuleName, 34, "cannot delegate to pool/operator/service with invalid (zero) ex-rate")
ErrInvalidShares = errors.Register(ModuleName, 2, "invalid shares amount")
ErrDelegatorShareExRateInvalid = errors.Register(ModuleName, 3, "cannot delegate to pool/operator/service with invalid (zero) ex-rate")
)
101 changes: 6 additions & 95 deletions x/restaking/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewGenesis creates a new genesis state
func NewGenesis(
poolsDelegations []PoolDelegationEntry,
servicesDelegations []ServiceDelegationEntry,
operatorsDelegations []OperatorDelegationEntry,
poolsDelegations []PoolDelegation,
servicesDelegations []ServiceDelegation,
operatorsDelegations []OperatorDelegation,
params Params,
) *GenesisState {
return &GenesisState{
Expand All @@ -32,23 +30,23 @@ func (g *GenesisState) Validate() error {
for _, entry := range g.PoolsDelegations {
err := entry.Validate()
if err != nil {
return fmt.Errorf("invalid pool delegation entry: %w", err)
return fmt.Errorf("invalid pool delegation: %w", err)
}
}

// Validate services delegations
for _, entry := range g.ServicesDelegations {
err := entry.Validate()
if err != nil {
return fmt.Errorf("invalid service delegation entry: %w", err)
return fmt.Errorf("invalid service delegation: %w", err)
}
}

// Validate operators delegations
for _, entry := range g.OperatorsDelegations {
err := entry.Validate()
if err != nil {
return fmt.Errorf("invalid operator delegation entry: %w", err)
return fmt.Errorf("invalid operator delegation: %w", err)
}
}

Expand All @@ -60,90 +58,3 @@ func (g *GenesisState) Validate() error {

return nil
}

// --------------------------------------------------------------------------------------------------------------------

// NewPoolDelegationEntry returns a new PoolDelegationEntry
func NewPoolDelegationEntry(poolID uint32, userAddress string, amount sdk.Coin) PoolDelegationEntry {
return PoolDelegationEntry{
PoolID: poolID,
UserAddress: userAddress,
Amount: amount,
}
}

// Validate performs basic validation of a pool delegation entry
func (e *PoolDelegationEntry) Validate() error {
if e.PoolID == 0 {
return fmt.Errorf("invalid pool id: %d", e.PoolID)
}

_, err := sdk.AccAddressFromBech32(e.UserAddress)
if err != nil {
return fmt.Errorf("invalid user address: %s", e.UserAddress)
}

if !e.Amount.IsValid() || e.Amount.IsZero() {
return fmt.Errorf("invalid amount: %s", e.Amount)
}

return nil
}

// --------------------------------------------------------------------------------------------------------------------

// NewServiceDelegationEntry returns a new ServiceDelegationEntry
func NewServiceDelegationEntry(serviceID uint32, userAddress string, amount sdk.Coin) ServiceDelegationEntry {
return ServiceDelegationEntry{
ServiceID: serviceID,
UserAddress: userAddress,
Amount: amount,
}
}

// Validate performs basic validation of a service delegation entry
func (e *ServiceDelegationEntry) Validate() error {
if e.ServiceID == 0 {
return fmt.Errorf("invalid service id: %d", e.ServiceID)
}

_, err := sdk.AccAddressFromBech32(e.UserAddress)
if err != nil {
return fmt.Errorf("invalid user address: %s", e.UserAddress)
}

if !e.Amount.IsValid() || e.Amount.IsZero() {
return fmt.Errorf("invalid amount: %s", e.Amount)
}

return nil
}

// --------------------------------------------------------------------------------------------------------------------

// NewOperatorDelegationEntry returns a new OperatorDelegationEntry
func NewOperatorDelegationEntry(operatorID uint32, userAddress string, amount sdk.Coin) OperatorDelegationEntry {
return OperatorDelegationEntry{
OperatorID: operatorID,
UserAddress: userAddress,
Amount: amount,
}
}

// Validate performs basic validation of an operator delegation entry
func (e *OperatorDelegationEntry) Validate() error {
if e.OperatorID == 0 {
return fmt.Errorf("invalid operator id: %d", e.OperatorID)
}

_, err := sdk.AccAddressFromBech32(e.UserAddress)
if err != nil {
return fmt.Errorf("invalid user address: %s", e.UserAddress)
}

if !e.Amount.IsValid() || e.Amount.IsZero() {
return fmt.Errorf("invalid amount: %s", e.Amount)
}

return nil
}
Loading

0 comments on commit 60b17a6

Please sign in to comment.