Skip to content

Commit

Permalink
contract
Browse files Browse the repository at this point in the history
  • Loading branch information
kimurayu45z committed Nov 20, 2024
1 parent 1dd1f13 commit 8dc4101
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 18 deletions.
11 changes: 7 additions & 4 deletions x/contract/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type (
ibcKeeperFn func() *ibckeeper.Keeper
capabilityScopedFn func(string) capabilitykeeper.ScopedKeeper

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
customAuthKeeper types.CustomAuthKeeper
}
)

Expand All @@ -48,6 +49,7 @@ func NewKeeper(

accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
customAuthKeeper types.CustomAuthKeeper,
) Keeper {
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Sprintf("invalid authority address: %s", authority))
Expand All @@ -61,8 +63,9 @@ func NewKeeper(
ibcKeeperFn: ibcKeeperFn,
capabilityScopedFn: capabilityScopedFn,

accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
customAuthKeeper: customAuthKeeper,
}
}

Expand Down
6 changes: 4 additions & 2 deletions x/contract/keeper/msg_server_match_lazy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
func (k msgServer) MatchLazyOrder(goCtx context.Context, msg *types.MsgMatchLazyOrder) (*types.MsgMatchLazyOrderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
err := k.ValidateOrderPair(ctx, msg.Earlier, msg.Later)
if err != nil {
return nil, err
}

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

// TODO: Handling the message
_ = ctx
err := k.ValidateOrderPair(ctx, msg.Earlier, msg.Later)
if err != nil {
return nil, err
}

return &types.MsgMatchOrderResponse{}, nil
}
1 change: 1 addition & 0 deletions x/contract/keeper/settlement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package keeper
43 changes: 43 additions & 0 deletions x/contract/keeper/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper

import (
"gluon/x/contract/types"

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

func (k Keeper) ValidateOrderPair(ctx sdk.Context, earlier types.Order, later types.Order) error {
pairingEarlier, found := k.customAuthKeeper.GetPairing(ctx, earlier.Body.Address, earlier.ParingId)
if !found {
return types.ErrPairingNotFound
}
pairingLater, found := k.customAuthKeeper.GetPairing(ctx, later.Body.Address, later.ParingId)
if !found {
return types.ErrPairingNotFound
}

pubKeyEarlier, err := k.customAuthKeeper.GetPairingPubKey(ctx, pairingEarlier)
if err != nil {
return err
}
pubKeyLater, err := k.customAuthKeeper.GetPairingPubKey(ctx, pairingLater)
if err != nil {
return err
}

err = earlier.Validate(pubKeyEarlier)
if err != nil {
return err
}
err = later.Validate(pubKeyLater)
if err != nil {
return err
}

err = earlier.CrossValidate(later)
if err != nil {
return err
}

return nil
}
6 changes: 4 additions & 2 deletions x/contract/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ type ModuleInputs struct {
Config *modulev1.Module
Logger log.Logger

AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
CustomAuthKeeper types.CustomAuthKeeper

IBCKeeperFn func() *ibckeeper.Keeper `optional:"true"`
CapabilityScopedFn func(string) capabilitykeeper.ScopedKeeper `optional:"true"`
Expand Down Expand Up @@ -221,6 +222,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.CapabilityScopedFn,
in.AccountKeeper,
in.BankKeeper,
in.CustomAuthKeeper,
)
m := NewAppModule(
in.Cdc,
Expand Down
2 changes: 2 additions & 0 deletions x/contract/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var (
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrNotPositiveAmount = sdkerrors.Register(ModuleName, 1101, "amount must be positive")
ErrEmptySignature = sdkerrors.Register(ModuleName, 1102, "signature must not be empty")
ErrInvalidSignature = sdkerrors.Register(ModuleName, 1103, "invalid signature")
ErrPairingNotFound = sdkerrors.Register(ModuleName, 1104, "pairing not found")
ErrInvalidPacketTimeout = sdkerrors.Register(ModuleName, 1500, "invalid packet timeout")
ErrInvalidVersion = sdkerrors.Register(ModuleName, 1501, "invalid version")
)
9 changes: 9 additions & 0 deletions x/contract/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"

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

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

customauthtypes "gluon/x/customauth/types"
)

// AccountKeeper defines the expected interface for the Account module.
Expand All @@ -23,3 +27,8 @@ type ParamSubspace interface {
Get(context.Context, []byte, interface{})
Set(context.Context, []byte, interface{})
}

type CustomAuthKeeper interface {
GetPairing(ctx context.Context, address string, id uint64) (val customauthtypes.Pairing, found bool)
GetPairingPubKey(ctx context.Context, pairing customauthtypes.Pairing) (cryptotypes.PubKey, error)
}
4 changes: 2 additions & 2 deletions x/contract/types/message_match_lazy_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func NewMsgMatchLazyOrder(earlier Order, later Order) *MsgMatchLazyOrder {
}

func (msg *MsgMatchLazyOrder) ValidateBasic() error {
err := msg.Earlier.Validate()
err := msg.Earlier.ValidateBasic()
if err != nil {
return err
}
err = msg.Later.Validate()
err = msg.Later.ValidateBasic()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/contract/types/message_match_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func NewMsgMatchOrder(earlier Order, later Order) *MsgMatchOrder {
}

func (msg *MsgMatchOrder) ValidateBasic() error {
err := msg.Earlier.Validate()
err := msg.Earlier.ValidateBasic()
if err != nil {
return err
}
err = msg.Later.Validate()
err = msg.Later.ValidateBasic()
if err != nil {
return err
}
Expand Down
28 changes: 24 additions & 4 deletions x/contract/types/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

errorsmod "cosmossdk.io/errors"

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

func NewOrderBody(address string, direction OrderDirection, type_ OrderType, amount sdkmath.Int, limitPrice *sdkmath.LegacyDec, stopPrice *sdkmath.LegacyDec) OrderBody {
func NewOrderBody(address string, baseDenom string, quoteDenom string, direction OrderDirection, type_ OrderType, amount sdkmath.Int, limitPrice *sdkmath.LegacyDec, stopPrice *sdkmath.LegacyDec) OrderBody {
return OrderBody{
Address: address,
BaseDenom: baseDenom,
QuoteDenom: quoteDenom,
Direction: direction,
Type: type_,
Amount: amount,
Expand All @@ -18,7 +22,7 @@ func NewOrderBody(address string, direction OrderDirection, type_ OrderType, amo
}
}

func (orderBody OrderBody) Validate() error {
func (orderBody OrderBody) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(orderBody.Address)
if err != nil {
return err
Expand All @@ -30,8 +34,8 @@ func (orderBody OrderBody) Validate() error {
return nil
}

func (order Order) Validate() error {
err := order.Body.Validate()
func (order Order) ValidateBasic() error {
err := order.Body.ValidateBasic()
if err != nil {
return err
}
Expand All @@ -42,3 +46,19 @@ func (order Order) Validate() error {

return nil
}

func (order Order) Validate(pubKey cryptotypes.PubKey) error {
bodyBytes, err := order.Body.Marshal()
if err != nil {
return err
}

if !pubKey.VerifySignature(bodyBytes, order.Signature) {
return errorsmod.Wrapf(ErrInvalidSignature, "address: %s", order.Body.Address)
}
return nil
}

func (order Order) CrossValidate(others Order) error {
return nil
}

0 comments on commit 8dc4101

Please sign in to comment.