Skip to content

Commit

Permalink
KIP-84 fix: reclaim
Browse files Browse the repository at this point in the history
  • Loading branch information
kobs30 committed Nov 18, 2024
1 parent ae084bf commit 1b8fb77
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions x/layer2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@ func (k msgServer) BondDappProposal(goCtx context.Context, msg *types.MsgBondDap
return nil, types.ErrInvalidDappBondDenom
}

dapp.TotalBond = dapp.TotalBond.Add(msg.Bond)

properties := k.keeper.gk.GetNetworkProperties(ctx)
target := sdk.NewInt(int64(properties.MaxDappBond))
if dapp.TotalBond.Amount.GT(target) {
return nil, types.ErrMaxDappBondReached
}

// send initial bond to module account
addr := sdk.MustAccAddressFromBech32(msg.Sender)
err := k.keeper.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, sdk.Coins{msg.Bond})
if err != nil {
return nil, err
}

properties := k.keeper.gk.GetNetworkProperties(ctx)
if dapp.TotalBond.Amount.GTE(sdk.NewInt(int64(properties.MaxDappBond)).Mul(sdk.NewInt(1000_000))) {
return nil, types.ErrMaxDappBondReached
}

dapp.TotalBond = dapp.TotalBond.Add(msg.Bond)
k.keeper.SetDapp(ctx, dapp)

userDappBond := k.keeper.GetUserDappBond(ctx, msg.DappName, msg.Sender)
Expand All @@ -114,6 +116,11 @@ func (k msgServer) BondDappProposal(goCtx context.Context, msg *types.MsgBondDap
func (k msgServer) ReclaimDappBondProposal(goCtx context.Context, msg *types.MsgReclaimDappBondProposal) (*types.MsgReclaimDappBondProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

dapp := k.keeper.GetDapp(ctx, msg.DappName)
if dapp.Name == "" {
return nil, types.ErrDappDoesNotExist
}

userDappBond := k.keeper.GetUserDappBond(ctx, msg.DappName, msg.Sender)
if userDappBond.DappName == "" {
return nil, types.ErrUserDappBondDoesNotExist
Expand All @@ -125,8 +132,7 @@ func (k msgServer) ReclaimDappBondProposal(goCtx context.Context, msg *types.Msg
return nil, types.ErrNotEnoughUserDappBond
}

userDappBond.Bond.Amount = userDappBond.Bond.Amount.Sub(msg.Bond.Amount)
k.keeper.SetUserDappBond(ctx, userDappBond)
dapp.TotalBond = dapp.TotalBond.Sub(msg.Bond)

// send tokens back to user
addr := sdk.MustAccAddressFromBech32(msg.Sender)
Expand All @@ -135,6 +141,11 @@ func (k msgServer) ReclaimDappBondProposal(goCtx context.Context, msg *types.Msg
return nil, err
}

k.keeper.SetDapp(ctx, dapp)

userDappBond.Bond.Amount = userDappBond.Bond.Amount.Sub(msg.Bond.Amount)
k.keeper.SetUserDappBond(ctx, userDappBond)

return &types.MsgReclaimDappBondProposalResponse{}, nil
}

Expand Down

0 comments on commit 1b8fb77

Please sign in to comment.