Skip to content

Commit

Permalink
settle
Browse files Browse the repository at this point in the history
  • Loading branch information
kimurayu45z committed Nov 21, 2024
1 parent b97ec82 commit 71e5caf
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
11 changes: 1 addition & 10 deletions x/contract/keeper/msg_server_match_lazy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k msgServer) MatchLazyOrder(goCtx context.Context, msg *types.MsgMatchLazy
return nil, types.ErrOrderNotFound
}

err := buy.CrossValidate(sell, msg.Price)
err := buy.CrossValidate(sell, msg.Price, ctx.BlockTime())
if err != nil {
return nil, err
}
Expand All @@ -41,14 +41,5 @@ func (k msgServer) MatchLazyOrder(goCtx context.Context, msg *types.MsgMatchLazy
return nil, err
}

err = k.AddContractedAmount(ctx, uint64(buy.Expiry.UnixMilli()), buy.Id, msg.Amount)
if err != nil {
return nil, err
}
err = k.AddContractedAmount(ctx, uint64(sell.Expiry.UnixMilli()), sell.Id, msg.Amount)
if err != nil {
return nil, err
}

return &types.MsgMatchLazyOrderResponse{}, nil
}
11 changes: 1 addition & 10 deletions x/contract/keeper/msg_server_match_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k msgServer) MatchOrder(goCtx context.Context, msg *types.MsgMatchOrder) (
return nil, types.ErrOrderNotFound
}

err := buy.CrossValidate(sell, msg.Price)
err := buy.CrossValidate(sell, msg.Price, ctx.BlockTime())
if err != nil {
return nil, err
}
Expand All @@ -41,14 +41,5 @@ func (k msgServer) MatchOrder(goCtx context.Context, msg *types.MsgMatchOrder) (
return nil, err
}

err = k.AddContractedAmount(ctx, uint64(buy.Expiry.UnixMilli()), buy.Id, msg.Amount)
if err != nil {
return nil, err
}
err = k.AddContractedAmount(ctx, uint64(sell.Expiry.UnixMilli()), sell.Id, msg.Amount)
if err != nil {
return nil, err
}

return &types.MsgMatchOrderResponse{}, nil
}
40 changes: 38 additions & 2 deletions x/contract/keeper/settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,52 @@ func (k Keeper) Settle(
return err
}

err = k.AddContractedAmount(ctx, uint64(buy.Expiry.UnixMilli()), buy.Id, amount)
if err != nil {
return err
}
err = k.AddContractedAmount(ctx, uint64(sell.Expiry.UnixMilli()), sell.Id, amount)
if err != nil {
return err
}

return nil
}

// TODO
func (k Keeper) SettleLazy(
ctx sdk.Context,
earlier types.Order,
later types.Order,
buy types.Order,
sell types.Order,
amount sdkmath.Int,
price sdkmath.LegacyDec,
) error {
denomBase := buy.DenomBase
denomQuote := buy.DenomQuote

addressBuy, err := sdk.AccAddressFromBech32(buy.Address)
if err != nil {
return err
}
addressSell, err := sdk.AccAddressFromBech32(sell.Address)
if err != nil {
return err
}

_ = denomBase
_ = denomQuote
_ = addressBuy
_ = addressSell
_ = price

err = k.AddContractedAmount(ctx, uint64(buy.Expiry.UnixMilli()), buy.Id, amount)
if err != nil {
return err
}
err = k.AddContractedAmount(ctx, uint64(sell.Expiry.UnixMilli()), sell.Id, amount)
if err != nil {
return err
}

return nil
}
3 changes: 2 additions & 1 deletion x/contract/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var (
ErrInvalidOrderDirection = sdkerrors.Register(ModuleName, 1108, "invalid order direction")
ErrBothMarketPriceOrder = sdkerrors.Register(ModuleName, 1109, "both market price order")
ErrPriceMismatch = sdkerrors.Register(ModuleName, 1110, "price mismatch")
ErrContractAmountExceed = sdkerrors.Register(ModuleName, 1111, "contract amount exceed")
ErrOrderExpired = sdkerrors.Register(ModuleName, 1111, "order expired")
ErrContractAmountExceed = sdkerrors.Register(ModuleName, 1112, "contract amount exceed")
ErrInvalidPacketTimeout = sdkerrors.Register(ModuleName, 1500, "invalid packet timeout")
ErrInvalidVersion = sdkerrors.Register(ModuleName, 1501, "invalid version")
)
29 changes: 26 additions & 3 deletions x/contract/types/order.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -46,7 +48,7 @@ func (order Order) VerifySignature(pubKey cryptotypes.PubKey, signature []byte)
return nil
}

func (buy Order) CrossValidate(sell Order, price sdkmath.LegacyDec) error {
func (buy Order) CrossValidate(sell Order, price sdkmath.LegacyDec, blockTime time.Time) error {
if buy.DenomBase != sell.DenomBase || buy.DenomQuote != sell.DenomQuote {
return ErrDenomMismatch
}
Expand All @@ -62,8 +64,29 @@ func (buy Order) CrossValidate(sell Order, price sdkmath.LegacyDec) error {
return ErrBothMarketPriceOrder
}

// TODO
_ = price
if buy.LimitPrice != nil && price.GT(*buy.LimitPrice) {
return errorsmod.Wrapf(ErrPriceMismatch, "price: %s, buy limit price: %s", price.String(), buy.LimitPrice.String())
}

if sell.LimitPrice != nil && price.LT(*sell.LimitPrice) {
return errorsmod.Wrapf(ErrPriceMismatch, "price: %s, sell limit price: %s", price.String(), sell.LimitPrice.String())
}

if buy.StopPrice != nil && price.LT(*buy.StopPrice) {
return errorsmod.Wrapf(ErrPriceMismatch, "price: %s, buy stop price: %s", price.String(), buy.StopPrice.String())
}

if sell.StopPrice != nil && price.GT(*sell.StopPrice) {
return errorsmod.Wrapf(ErrPriceMismatch, "price: %s, sell stop price: %s", price.String(), sell.StopPrice.String())
}

if blockTime.After(buy.Expiry) {
return ErrOrderExpired
}

if blockTime.After(sell.Expiry) {
return ErrOrderExpired
}

return nil
}

0 comments on commit 71e5caf

Please sign in to comment.