Skip to content

Commit

Permalink
[dex] Add events to msgs (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
udpatil authored Mar 6, 2023
1 parent 2a584b1 commit f6b4074
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
7 changes: 6 additions & 1 deletion x/dex/keeper/msgserver/msg_server_cancel_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (k msgServer) CancelOrders(goCtx context.Context, msg *types.MsgCancelOrder
return nil, err
}

events := []sdk.Event{}
for _, cancellation := range msg.GetCancellations() {
var allocation *types.Allocation
var found bool
Expand Down Expand Up @@ -49,8 +50,12 @@ func (k msgServer) CancelOrders(goCtx context.Context, msg *types.MsgCancelOrder
PositionDirection: cancellation.PositionDirection,
}
pairBlockCancellations.Add(&cancel)
events = append(events, sdk.NewEvent(
types.EventTypeCancelOrder,
sdk.NewAttribute(types.AttributeKeyCancellationID, fmt.Sprint(cancellation.Id)),
))
}
}

ctx.EventManager().EmitEvents(events)
return &types.MsgCancelOrdersResponse{}, nil
}
5 changes: 5 additions & 0 deletions x/dex/keeper/msgserver/msg_server_contract_deposit_rent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ func (k msgServer) ContractDepositRent(goCtx context.Context, msg *types.MsgCont
return nil, err
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeDepositRent,
sdk.NewAttribute(types.AttributeKeyContractAddress, msg.ContractAddr),
sdk.NewAttribute(types.AttributeKeyRentBalance, fmt.Sprint(contract.RentBalance)),
))
return &types.MsgContractDepositRentResponse{}, nil
}
8 changes: 6 additions & 2 deletions x/dex/keeper/msgserver/msg_server_place_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k msgServer) PlaceOrders(goCtx context.Context, msg *types.MsgPlaceOrders)
if err := k.transferFunds(goCtx, msg); err != nil {
return nil, err
}

events := []sdk.Event{}
nextID := k.GetNextOrderID(ctx, msg.ContractAddr)
idsInResp := []uint64{}
for _, order := range msg.GetOrders() {
Expand All @@ -73,10 +73,14 @@ func (k msgServer) PlaceOrders(goCtx context.Context, msg *types.MsgPlaceOrders)
order.ContractAddr = msg.GetContractAddr()
dexutils.GetMemState(ctx.Context()).GetBlockOrders(ctx, typesutils.ContractAddress(msg.GetContractAddr()), pairStr).Add(order)
idsInResp = append(idsInResp, nextID)
events = append(events, sdk.NewEvent(
types.EventTypePlaceOrder,
sdk.NewAttribute(types.AttributeKeyOrderID, fmt.Sprint(nextID)),
))
nextID++
}
k.SetNextOrderID(ctx, msg.ContractAddr, nextID)

ctx.EventManager().EmitEvents(events)
return &types.MsgPlaceOrdersResponse{
OrderIds: idsInResp,
}, nil
Expand Down
5 changes: 5 additions & 0 deletions x/dex/keeper/msgserver/msg_server_register_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (k msgServer) RegisterContract(goCtx context.Context, msg *types.MsgRegiste
return &types.MsgRegisterContractResponse{}, err
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeRegisterContract,
sdk.NewAttribute(types.AttributeKeyContractAddress, msg.Contract.ContractAddr),
))

return &types.MsgRegisterContractResponse{}, nil
}

Expand Down
9 changes: 9 additions & 0 deletions x/dex/keeper/msgserver/msg_server_register_pairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (k msgServer) RegisterPairs(goCtx context.Context, msg *types.MsgRegisterPa

ctx := sdk.UnwrapSDKContext(goCtx)

events := []sdk.Event{}
// Validation such that only the user who stored the code can register pairs
for _, batchPair := range msg.Batchcontractpair {
contractAddr := batchPair.ContractAddr
Expand All @@ -31,8 +32,16 @@ func (k msgServer) RegisterPairs(goCtx context.Context, msg *types.MsgRegisterPa
// tuple and register them individually
for _, pair := range batchPair.Pairs {
k.AddRegisteredPair(ctx, contractAddr, *pair)
events = append(events, sdk.NewEvent(
types.EventTypeRegisterPair,
sdk.NewAttribute(types.AttributeKeyContractAddress, contractAddr),
sdk.NewAttribute(types.AttributeKeyPriceDenom, pair.PriceDenom),
sdk.NewAttribute(types.AttributeKeyContractAddress, pair.AssetDenom),
))
}
}

ctx.EventManager().EmitEvents(events)

return &types.MsgRegisterPairsResponse{}, nil
}
5 changes: 5 additions & 0 deletions x/dex/keeper/msgserver/msg_server_unregister_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ func (k msgServer) UnregisterContract(goCtx context.Context, msg *types.MsgUnreg
k.DeleteNextOrderID(ctx, msg.ContractAddr)
k.DeleteAllRegisteredPairsForContract(ctx, msg.ContractAddr)
k.RemoveAllTriggeredOrders(ctx, msg.ContractAddr)

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUnregisterContract,
sdk.NewAttribute(types.AttributeKeyContractAddress, msg.ContractAddr),
))
return &types.MsgUnregisterContractResponse{}, nil
}
19 changes: 19 additions & 0 deletions x/dex/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

const (
EventTypePlaceOrder = "place_order"
EventTypeCancelOrder = "cancel_order"
EventTypeDepositRent = "deposit_rent"
EventTypeRegisterContract = "register_contract"
EventTypeUnregisterContract = "unregister_contract"
EventTypeRegisterPair = "register_pair"

AttributeKeyOrderID = "order_id"
AttributeKeyCancellationID = "cancellation_id"
AttributeKeyContractAddress = "contract_address"
AttributeKeyRentBalance = "rent_balance"
AttributeKeyPriceDenom = "price_denom"
AttributeKeyAssetDenom = "asset_denom"

AttributeValueCategory = ModuleName
)

0 comments on commit f6b4074

Please sign in to comment.