Skip to content

Commit

Permalink
feat: add import/export stablestake flow (#771)
Browse files Browse the repository at this point in the history
* feat: add import/export stablestake flow

* fix: add migrator to add block height field
  • Loading branch information
cosmic-vagabond authored Sep 5, 2024
1 parent fe1f2c2 commit 4f2d2c0
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 39 deletions.
4 changes: 4 additions & 0 deletions proto/elys/stablestake/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package elys.stablestake;

import "gogoproto/gogo.proto";
import "elys/stablestake/params.proto";
import "elys/stablestake/debt.proto";
import "elys/stablestake/types.proto";

option go_package = "github.com/elys-network/elys/x/stablestake/types";

// GenesisState defines the stablestake module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated Debt debt_list = 2 [(gogoproto.nullable) = false];
repeated InterestBlock interest_list = 3 [(gogoproto.nullable) = false];
}
9 changes: 9 additions & 0 deletions proto/elys/stablestake/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ message InterestBlock {
(gogoproto.nullable) = false
];
int64 block_time = 2;
uint64 block_height = 3;
}

message LegacyInterestBlock {
string interest_rate = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 block_time = 2;
}
3 changes: 2 additions & 1 deletion x/leveragelp/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"

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

Expand Down Expand Up @@ -48,7 +49,7 @@ func (gs GenesisState) Validate() error {
whitelistMap := make(map[string]struct{})
for _, elem := range gs.AddressWhitelist {
index := elem
if _, ok := positionIndexMap[index]; ok {
if _, ok := whitelistMap[index]; ok {
return fmt.Errorf("duplicated index for pool")
}
whitelistMap[index] = struct{}{}
Expand Down
13 changes: 13 additions & 0 deletions x/stablestake/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
// Set all the debts
for _, elem := range genState.DebtList {
k.SetDebt(ctx, elem)
}

// Set all the interests
for _, elem := range genState.InterestList {
k.SetInterest(ctx, elem.BlockHeight, elem)
}

// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}
Expand All @@ -17,6 +27,9 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

genesis.DebtList = k.AllDebts(ctx)
genesis.InterestList = k.GetAllInterest(ctx)

// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
21 changes: 21 additions & 0 deletions x/stablestake/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ func (k Keeper) GetAllInterest(ctx sdk.Context) []types.InterestBlock {
return interests
}

func (k Keeper) GetAllLegacyInterest(ctx sdk.Context) []types.InterestBlock {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.InterestPrefixKey)
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()

interests := []types.InterestBlock{}
for ; iterator.Valid(); iterator.Next() {
interest := types.LegacyInterestBlock{}
k.cdc.MustUnmarshal(iterator.Value(), &interest)

block := iterator.Key()
newInterest := types.InterestBlock{}
newInterest.InterestRate = interest.InterestRate
newInterest.BlockTime = interest.BlockTime
newInterest.BlockHeight = sdk.BigEndianToUint64(block)

interests = append(interests, newInterest)
}
return interests
}

func (k Keeper) GetInterest(ctx sdk.Context, startBlock uint64, startTime uint64, borrowed sdk.Dec) sdk.Int {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.InterestPrefixKey)
currentBlockKey := sdk.Uint64ToBigEndian(uint64(ctx.BlockHeight()))
Expand Down
11 changes: 7 additions & 4 deletions x/stablestake/migrations/v3_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
)

func (m Migrator) V3Migration(ctx sdk.Context) error {
params := m.keeper.GetParams(ctx)
params.InterestRateMin = sdk.NewDecWithPrec(10, 2) // 10%
params.InterestRateMax = sdk.NewDecWithPrec(50, 2) // 50%
m.keeper.SetParams(ctx, params)
// Migrate the interest blocks
interests := m.keeper.GetAllLegacyInterest(ctx)

for _, interest := range interests {
m.keeper.SetInterest(ctx, interest.BlockHeight, interest)
}

return nil
}
2 changes: 1 addition & 1 deletion x/stablestake/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
28 changes: 27 additions & 1 deletion x/stablestake/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package types

import (
// this line is used by starport scaffolding # genesis/types/import
sdk "github.com/cosmos/cosmos-sdk/types"

fmt "fmt"
)

// this line is used by starport scaffolding # genesis/types/import

// DefaultIndex is the default global index
const DefaultIndex uint64 = 1

// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
DebtList: []Debt{},
InterestList: []InterestBlock{},
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
}
Expand All @@ -18,6 +24,26 @@ func DefaultGenesis() *GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
// Check for duplicated index in debt
debtIndexMap := make(map[string]struct{})
for _, elem := range gs.DebtList {
index := elem.Address
if _, ok := debtIndexMap[index]; ok {
return fmt.Errorf("duplicated index for debt")
}
debtIndexMap[index] = struct{}{}
}

// Check for duplicated index in interest
interestIndexMap := make(map[string]struct{})
for _, elem := range gs.InterestList {
index := string(sdk.Uint64ToBigEndian(uint64(elem.BlockTime)))
if _, ok := interestIndexMap[index]; ok {
return fmt.Errorf("duplicated index for interest")
}
interestIndexMap[index] = struct{}{}
}

// this line is used by starport scaffolding # genesis/types/validate

return gs.Params.Validate()
Expand Down
151 changes: 140 additions & 11 deletions x/stablestake/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f2d2c0

Please sign in to comment.