Skip to content

Commit

Permalink
validate
Browse files Browse the repository at this point in the history
  • Loading branch information
kimurayu45z committed Nov 21, 2024
1 parent 968929f commit a4d5499
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 106 deletions.
4 changes: 2 additions & 2 deletions api/gluon/contract/tx.pulsar.go

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

4 changes: 2 additions & 2 deletions proto/gluon/contract/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ message MsgMatchOrder {
string price = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = true,
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
}
Expand All @@ -101,7 +101,7 @@ message MsgMatchLazyOrder {
string price = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = true,
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
}
Expand Down
39 changes: 38 additions & 1 deletion x/contract/keeper/msg_server_match_lazy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,44 @@ import (
func (k msgServer) MatchLazyOrder(goCtx context.Context, msg *types.MsgMatchLazyOrder) (*types.MsgMatchLazyOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

_ = ctx
earlier, found := k.GetOrder(ctx, msg.EarlierOrderId)
if !found {
return nil, types.ErrOrderNotFound
}

later, found := k.GetOrder(ctx, msg.LaterOrderId)
if !found {
return nil, types.ErrOrderNotFound
}

err := earlier.CrossValidate(later, msg.Price)
if err != nil {
return nil, err
}

err = k.ValidateContractAmount(ctx, earlier, msg.Amount)
if err != nil {
return nil, err
}

err = k.ValidateContractAmount(ctx, later, msg.Amount)
if err != nil {
return nil, err
}

err = k.SettleLazy(ctx, earlier, later, msg.Amount)
if err != nil {
return nil, err
}

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

return &types.MsgMatchLazyOrderResponse{}, nil
}
39 changes: 38 additions & 1 deletion x/contract/keeper/msg_server_match_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,44 @@ import (
func (k msgServer) MatchOrder(goCtx context.Context, msg *types.MsgMatchOrder) (*types.MsgMatchOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

_ = ctx
earlier, found := k.GetOrder(ctx, msg.EarlierOrderId)
if !found {
return nil, types.ErrOrderNotFound
}

later, found := k.GetOrder(ctx, msg.LaterOrderId)
if !found {
return nil, types.ErrOrderNotFound
}

err := earlier.CrossValidate(later, msg.Price)
if err != nil {
return nil, err
}

err = k.ValidateContractAmount(ctx, earlier, msg.Amount)
if err != nil {
return nil, err
}

err = k.ValidateContractAmount(ctx, later, msg.Amount)
if err != nil {
return nil, err
}

err = k.Settle(ctx, earlier, later, msg.Amount)
if err != nil {
return nil, err
}

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

return &types.MsgMatchOrderResponse{}, nil
}
11 changes: 11 additions & 0 deletions x/contract/keeper/settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO
func (k Keeper) Settle(
ctx sdk.Context,
earlier types.Order,
Expand Down Expand Up @@ -34,3 +35,13 @@ func (k Keeper) Settle(

return nil
}

// TODO
func (k Keeper) SettleLazy(
ctx sdk.Context,
earlier types.Order,
later types.Order,
amount sdkmath.Int,
) error {
return nil
}
14 changes: 14 additions & 0 deletions x/contract/keeper/sorted_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"

sdkmath "cosmossdk.io/math"
)

// SetSortedOrder set a specific sortedOrder in the store from its index
Expand Down Expand Up @@ -74,3 +76,15 @@ func (k Keeper) GetAllSortedOrder(ctx context.Context) (list []types.SortedOrder

return
}

func (k Keeper) AddContractedAmount(ctx context.Context, expiry uint64, id string, amount sdkmath.Int) error {
sortedOrder, found := k.GetSortedOrder(ctx, expiry, id)
if !found {
return types.ErrOrderNotFound
}

sortedOrder.ContractedAmount = sortedOrder.ContractedAmount.Add(amount)
k.SetSortedOrder(ctx, sortedOrder)

return nil
}
16 changes: 15 additions & 1 deletion x/contract/keeper/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"gluon/x/contract/types"

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

Expand All @@ -15,9 +16,22 @@ func (k Keeper) ValidateOrder(ctx sdk.Context, order types.Order, pairingId uint
if err != nil {
return err
}
err = order.Validate(pubKey, signature)
err = order.VerifySignature(pubKey, signature)
if err != nil {
return err
}
return nil
}

func (k Keeper) ValidateContractAmount(ctx sdk.Context, order types.Order, amount sdkmath.Int) error {
sortedOrder, found := k.GetSortedOrder(ctx, uint64(order.Expiry.UnixMilli()), order.Id)
if !found {
return types.ErrOrderNotFound
}

if sortedOrder.ContractedAmount.Add(amount).GT(order.Amount) {
return types.ErrContractAmountExceed
}

return nil
}
12 changes: 7 additions & 5 deletions x/contract/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ var (
ErrEmptySignature = sdkerrors.Register(ModuleName, 1103, "signature must not be empty")
ErrInvalidSignature = sdkerrors.Register(ModuleName, 1104, "invalid signature")
ErrPairingNotFound = sdkerrors.Register(ModuleName, 1105, "pairing not found")
ErrDenomMismatch = sdkerrors.Register(ModuleName, 1106, "denom mismatch")
ErrUnknownOrderDirection = sdkerrors.Register(ModuleName, 1107, "unknown order direction")
ErrSameOrderDirection = sdkerrors.Register(ModuleName, 1108, "same order direction")
ErrBothMarketPriceOrder = sdkerrors.Register(ModuleName, 1109, "both market price order")
ErrPriceMismatch = sdkerrors.Register(ModuleName, 1110, "price mismatch")
ErrOrderNotFound = sdkerrors.Register(ModuleName, 1106, "order not found")
ErrDenomMismatch = sdkerrors.Register(ModuleName, 1107, "denom mismatch")
ErrUnknownOrderDirection = sdkerrors.Register(ModuleName, 1108, "unknown order direction")
ErrSameOrderDirection = sdkerrors.Register(ModuleName, 1109, "same order direction")
ErrBothMarketPriceOrder = sdkerrors.Register(ModuleName, 1110, "both market price order")
ErrPriceMismatch = sdkerrors.Register(ModuleName, 1111, "price mismatch")
ErrContractAmountExceed = sdkerrors.Register(ModuleName, 1112, "contract amount exceed")
ErrInvalidPacketTimeout = sdkerrors.Register(ModuleName, 1500, "invalid packet timeout")
ErrInvalidVersion = sdkerrors.Register(ModuleName, 1501, "invalid version")
)
7 changes: 5 additions & 2 deletions x/contract/types/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (order Order) ValidateBasic() error {
return nil
}

func (order Order) Validate(pubKey cryptotypes.PubKey, signature []byte) error {
func (order Order) VerifySignature(pubKey cryptotypes.PubKey, signature []byte) error {
bodyBytes, err := order.Marshal()
if err != nil {
return err
Expand All @@ -46,7 +46,7 @@ func (order Order) Validate(pubKey cryptotypes.PubKey, signature []byte) error {
return nil
}

func (order Order) CrossValidate(other Order) error {
func (order Order) CrossValidate(other Order, price sdkmath.LegacyDec) error {
if order.BaseDenom != other.BaseDenom || order.QuoteDenom != other.QuoteDenom {
return ErrDenomMismatch
}
Expand All @@ -62,5 +62,8 @@ func (order Order) CrossValidate(other Order) error {
return ErrBothMarketPriceOrder
}

// TODO
_ = price

return nil
}
Loading

0 comments on commit a4d5499

Please sign in to comment.