Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LUM-576] Close pool feature #69

Merged
merged 11 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ var (
ibcclientclient.UpgradeProposalHandler,
dfractclient.UpdateParamsProposalHandler,
millionsclient.RegisterPoolProposalHandler,
millionsclient.ClosePoolProposalHandler,
millionsclient.UpdatePoolProposalHandler,
millionsclient.UpdateParamsProposalHandler,
},
Expand Down Expand Up @@ -781,18 +782,6 @@ func (app *App) registerUpgradeHandlers() {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

app.UpgradeKeeper.SetUpgradeHandler("v1.4.5", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Kill the first pool that shouldn't be used anymore after that upgrade
_, err := app.MillionsKeeper.UnsafeKillPool(ctx, 1)
if err != nil {
return fromVM, err
}

// Continue normal upgrade processing
app.Logger().Info("Pool killed. v1.4.5 upgrade applied")
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

app.UpgradeKeeper.SetUpgradeHandler("v1.5.0", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
app.Logger().Info("Starting v1.5.0 upgrade")

Expand Down
9 changes: 9 additions & 0 deletions proto/lum/network/millions/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ message ProposalUpdatePool {
repeated FeeTaker fee_takers = 11 [ (gogoproto.nullable) = false ];
}

message ProposalClosePool {
option (gogoproto.goproto_stringer) = false;

string title = 1;
string description = 2;

uint64 pool_id = 3;
}

message ProposalUpdateParams {
option (gogoproto.goproto_stringer) = false;

Expand Down
5 changes: 3 additions & 2 deletions proto/lum/network/millions/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum PoolState {
POOL_STATE_CREATED = 1 [ (gogoproto.enumvalue_customname) = "Created" ];
POOL_STATE_READY = 2 [ (gogoproto.enumvalue_customname) = "Ready" ];
POOL_STATE_PAUSED = 3 [ (gogoproto.enumvalue_customname) = "Paused" ];
POOL_STATE_KILLED = 4 [ (gogoproto.enumvalue_customname) = "Killed" ];
POOL_STATE_CLOSING = 4 [ (gogoproto.enumvalue_customname) = "Closing" ];
POOL_STATE_CLOSED = 5 [ (gogoproto.enumvalue_customname) = "Closed" ];
}

// PoolType the type of Pool
Expand Down Expand Up @@ -119,8 +120,8 @@ message Pool {
cosmos.base.v1beta1.Coin available_prize_pool = 29
[ (gogoproto.nullable) = false ];
repeated FeeTaker fee_takers = 30 [ (gogoproto.nullable) = false ];
reserved 31;

reserved 31;
PoolState state = 32;

int64 created_at_height = 33;
Expand Down
84 changes: 84 additions & 0 deletions x/millions/client/cli/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ import (
"github.com/lum-network/chain/x/millions/types"
)

func parseClosePoolProposalFile(cdc codec.JSONCodec, proposalFile string) (proposal types.ProposalClosePool, err error) {
contents, err := os.ReadFile(proposalFile)
if err != nil {
return proposal, err
}

if err = cdc.UnmarshalJSON(contents, &proposal); err != nil {
return proposal, err
}
return proposal, nil
}

func parseRegisterPoolProposalFile(cdc codec.JSONCodec, proposalFile string) (proposal types.ProposalRegisterPool, err error) {
contents, err := os.ReadFile(proposalFile)
if err != nil {
Expand Down Expand Up @@ -235,6 +247,78 @@ Where proposal.json contains:
return cmd
}

func CmdProposalClosePool() *cobra.Command {
cmd := &cobra.Command{
Use: "millions-close-pool [proposal-file]",
Short: "Submit a millions close pool proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a ClosePool proposal along with an initial deposit.
The proposal details must be supplied via a JSON file.

Example:
$ %s tx gov submit-legacy-proposal millions-close-pool <path/to/proposal.json> --from=<key_or_address>

Where proposal.json contains:
{
"title": "Close my pool",
"description": "This is my close pool",
"pool_id": 1
}
`, version.AppName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Acquire the client context
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

// Parse the proposal file
proposal, err := parseClosePoolProposalFile(clientCtx.Codec, args[0])
if err != nil {
return err
}

if err := proposal.ValidateBasic(); err != nil {
return err
}

// Grab the parameters
from := clientCtx.GetFromAddress()

// Grab the deposit
depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}

deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}

msg, err := govtypes.NewMsgSubmitProposal(&proposal, deposit, from)
if err != nil {
return err
}

if err := msg.ValidateBasic(); err != nil {
return err
}

// Generate the transaction
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(govcli.FlagDeposit, "1ulum", "deposit of proposal")
if err := cmd.MarkFlagRequired(govcli.FlagDeposit); err != nil {
panic(err)
}
return cmd
}

func CmdProposalUpdateParams() *cobra.Command {
cmd := &cobra.Command{
Use: "millions-update-params [proposal-file]",
Expand Down
1 change: 1 addition & 0 deletions x/millions/client/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import (
var (
RegisterPoolProposalHandler = govclient.NewProposalHandler(cli.CmdProposalRegisterPool)
UpdatePoolProposalHandler = govclient.NewProposalHandler(cli.CmdProposalUpdatePool)
ClosePoolProposalHandler = govclient.NewProposalHandler(cli.CmdProposalClosePool)
UpdateParamsProposalHandler = govclient.NewProposalHandler(cli.CmdProposalUpdateParams)
)
4 changes: 2 additions & 2 deletions x/millions/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var testGenesis = millionstypes.GenesisState{
{PoolId: 3, PoolType: millionstypes.PoolType_Staking, TvlAmount: sdk.NewInt(601), DepositorsCount: 1, SponsorshipAmount: sdk.ZeroInt(), Denom: "denom-3", NativeDenom: "denom-3", NextDrawId: 1,
ChainId: "c1", Validators: defaultValidators, MinDepositAmount: sdk.NewInt(1_000_000), UnbondingDuration: time.Duration(millionstypes.DefaultUnbondingDuration), MaxUnbondingEntries: sdk.NewInt(millionstypes.DefaultMaxUnbondingEntries), AvailablePrizePool: sdk.NewCoin("denom-3", sdk.ZeroInt()),
DrawSchedule: defaultSchedule, PrizeStrategy: defaultPrizeStrat,
State: millionstypes.PoolState_Killed, Bech32PrefixAccAddr: "lum", Bech32PrefixValAddr: "lumvaloper",
State: millionstypes.PoolState_Closed, Bech32PrefixAccAddr: "lum", Bech32PrefixValAddr: "lumvaloper",
FeeTakers: []millionstypes.FeeTaker{
{Destination: authtypes.FeeCollectorName, Amount: sdk.NewDecWithPrec(millionstypes.DefaultFeeTakerAmount, 2), Type: millionstypes.FeeTakerType_LocalModuleAccount},
},
Expand All @@ -87,7 +87,7 @@ var testGenesis = millionstypes.GenesisState{
{PoolId: 5, PoolType: millionstypes.PoolType_Staking, TvlAmount: sdk.NewInt(0), DepositorsCount: 0, SponsorshipAmount: sdk.ZeroInt(), Denom: "denom-5", NativeDenom: "denom-5", NextDrawId: 1,
ChainId: "c1", Validators: defaultValidators, MinDepositAmount: sdk.NewInt(1_000_000), UnbondingDuration: time.Duration(millionstypes.DefaultUnbondingDuration), MaxUnbondingEntries: sdk.NewInt(millionstypes.DefaultMaxUnbondingEntries), AvailablePrizePool: sdk.NewCoin("denom-5", sdk.ZeroInt()),
DrawSchedule: defaultSchedule, PrizeStrategy: defaultPrizeStrat,
State: millionstypes.PoolState_Killed, Bech32PrefixAccAddr: "lum", Bech32PrefixValAddr: "lumvaloper",
State: millionstypes.PoolState_Closed, Bech32PrefixAccAddr: "lum", Bech32PrefixValAddr: "lumvaloper",
FeeTakers: []millionstypes.FeeTaker{
{Destination: authtypes.FeeCollectorName, Amount: sdk.NewDecWithPrec(millionstypes.DefaultFeeTakerAmount, 2), Type: millionstypes.FeeTakerType_LocalModuleAccount},
},
Expand Down
4 changes: 4 additions & 0 deletions x/millions/handler_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
func NewMillionsProposalHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.ProposalClosePool:
{
return k.ClosePool(ctx, c.PoolId)
}
case *types.ProposalUpdatePool:
{
return k.UpdatePool(ctx, c.PoolId, c.Validators, c.MinDepositAmount, c.UnbondingDuration, c.MaxUnbondingEntries, c.DrawSchedule, c.PrizeStrategy, c.State, c.FeeTakers)
Expand Down
9 changes: 9 additions & 0 deletions x/millions/keeper/keeper_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func (k Keeper) OnDelegateDepositOnRemoteZoneCompleted(ctx sdk.Context, poolID u
pool.ApplySplitDelegate(ctx, splits)
k.updatePool(ctx, &pool)
k.UpdateDepositStatus(ctx, poolID, depositID, types.DepositState_Success, false)

if pool.State == types.PoolState_Closing {
// Continue closing procedure
// voluntary ignore errors
if err := k.ClosePool(ctx, poolID); err != nil {
k.Logger(ctx).With("ctx", "deposit_completed", "pool_id", poolID).Error("Silently failed to continue close pool procedure: %v", err)
}
}

return nil
}

Expand Down
26 changes: 24 additions & 2 deletions x/millions/keeper/keeper_draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (k Keeper) ClaimYieldOnRemoteZone(ctx sdk.Context, poolID uint64, drawID ui
if err != nil {
return nil, err
}

// Acquire Draw
draw, err := k.GetPoolDraw(ctx, poolID, drawID)
if err != nil {
Expand Down Expand Up @@ -407,11 +408,23 @@ func (k Keeper) ExecuteDraw(ctx sdk.Context, poolID uint64, drawID uint64) (*typ
draw.PrizePoolRemainsAmount = pool.AvailablePrizePool.Amount
draw.PrizePool = draw.PrizePool.Add(pool.AvailablePrizePool)

prizeStrat := pool.PrizeStrategy

if pool.State == types.PoolState_Closing {
// Pool is unfortunately closing but some people will be lucky
// Modify prize distribution to distribute the full prize pool in one draw
// which is equal to making all batches not unique and with a 100% draw probability
for i := range prizeStrat.PrizeBatches {
prizeStrat.PrizeBatches[i].IsUnique = false
prizeStrat.PrizeBatches[i].DrawProbability = sdk.OneDec()
}
}

// Draw prizes
dRes, err := k.RunDrawPrizes(
ctx,
draw.PrizePool,
pool.PrizeStrategy,
prizeStrat,
depositorsTWB,
draw.RandSeed,
)
Expand Down Expand Up @@ -493,18 +506,27 @@ func (k Keeper) OnExecuteDrawCompleted(ctx sdk.Context, pool *types.Pool, draw *
k.updatePool(ctx, pool)
return draw, err
}

draw.State = types.DrawState_Success
draw.ErrorState = types.DrawState_Unspecified
draw.UpdatedAtHeight = ctx.BlockHeight()
draw.UpdatedAt = ctx.BlockTime()
k.SetPoolDraw(ctx, *draw)
pool.LastDrawState = draw.State
k.updatePool(ctx, pool)

if pool.State == types.PoolState_Closing {
// Continue closing procedure
// voluntary ignore errors
if err := k.ClosePool(ctx, pool.GetPoolId()); err != nil {
k.Logger(ctx).With("ctx", "draw_completed", "pool_id", pool.GetPoolId()).Error("Silently failed to continue close pool procedure: %v", err)
}
}
return draw, err
}

// ComputeDepositsTWB takes deposits and computes the weight based on their deposit time and the draw duration
// It essentially compute the Time Weighted Balance of each deposit for the DrawPrizes phase
// It essentially computes the Time Weighted Balance of each deposit for the DrawPrizes phase
func (k Keeper) ComputeDepositsTWB(ctx sdk.Context, depositStartAt time.Time, drawAt time.Time, deposits []types.Deposit) []DepositTWB {
params := k.GetParams(ctx)

Expand Down
Loading
Loading