Skip to content

Commit

Permalink
Merge pull request #395 from b-harvest/dongsam/add-cancel-events
Browse files Browse the repository at this point in the history
Add swap_transacted event for failure case, attributes
  • Loading branch information
dongsam authored Jun 7, 2021
2 parents 6a576a1 + 058db9d commit 634a84f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
59 changes: 43 additions & 16 deletions x/liquidity/keeper/liquidity_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,13 @@ func (k Keeper) TransactAndRefundSwapLiquidityPool(ctx sdk.Context, swapMsgState
}

if match, ok := matchResultMap[sms.MsgIndex]; ok {
sendCoin(batchEscrowAcc, poolReserveAcc, sdk.NewCoin(sms.Msg.OfferCoin.Denom, match.TransactedCoinAmt.TruncateInt()))
sendCoin(poolReserveAcc, sms.Msg.GetSwapRequester(), sdk.NewCoin(
sms.Msg.DemandCoinDenom, match.ExchangedDemandCoinAmt.Sub(match.ExchangedCoinFeeAmt).TruncateInt()))
sendCoin(batchEscrowAcc, poolReserveAcc, sdk.NewCoin(sms.Msg.OfferCoin.Denom, match.OfferCoinFeeAmt.TruncateInt()))
transactedAmt := match.TransactedCoinAmt.TruncateInt()
receiveAmt := match.ExchangedDemandCoinAmt.Sub(match.ExchangedCoinFeeAmt).TruncateInt()
offerCoinFeeAmt := match.OfferCoinFeeAmt.TruncateInt()

sendCoin(batchEscrowAcc, poolReserveAcc, sdk.NewCoin(sms.Msg.OfferCoin.Denom, transactedAmt))
sendCoin(poolReserveAcc, sms.Msg.GetSwapRequester(), sdk.NewCoin(sms.Msg.DemandCoinDenom, receiveAmt))
sendCoin(batchEscrowAcc, poolReserveAcc, sdk.NewCoin(sms.Msg.OfferCoin.Denom, offerCoinFeeAmt))

if sms.RemainingOfferCoin.IsPositive() && sms.OrderExpiryHeight == ctx.BlockHeight() {
sendCoin(batchEscrowAcc, sms.Msg.GetSwapRequester(), sms.RemainingOfferCoin.Add(sms.ReservedOfferCoinFee))
Expand All @@ -637,26 +640,50 @@ func (k Keeper) TransactAndRefundSwapLiquidityPool(ctx sdk.Context, swapMsgState
types.EventTypeSwapTransacted,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(pool.Id, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(batch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(match.SwapMsgState.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueSwapRequester, match.SwapMsgState.Msg.GetSwapRequester().String()),
sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(match.SwapMsgState.Msg.SwapTypeId), 10)),
sdk.NewAttribute(types.AttributeValueOfferCoinDenom, match.SwapMsgState.Msg.OfferCoin.Denom),
sdk.NewAttribute(types.AttributeValueOfferCoinAmount, match.SwapMsgState.Msg.OfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueOrderPrice, match.SwapMsgState.Msg.OrderPrice.String()),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(sms.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueSwapRequester, sms.Msg.GetSwapRequester().String()),
sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(sms.Msg.SwapTypeId), 10)),
sdk.NewAttribute(types.AttributeValueOfferCoinDenom, sms.Msg.OfferCoin.Denom),
sdk.NewAttribute(types.AttributeValueOfferCoinAmount, sms.Msg.OfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueDemandCoinDenom, sms.Msg.DemandCoinDenom),
sdk.NewAttribute(types.AttributeValueOrderPrice, sms.Msg.OrderPrice.String()),
sdk.NewAttribute(types.AttributeValueSwapPrice, batchResult.SwapPrice.String()),
sdk.NewAttribute(types.AttributeValueTransactedCoinAmount, match.TransactedCoinAmt.String()),
sdk.NewAttribute(types.AttributeValueRemainingOfferCoinAmount, match.SwapMsgState.RemainingOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueExchangedOfferCoinAmount, match.SwapMsgState.ExchangedOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueOfferCoinFeeAmount, match.OfferCoinFeeAmt.String()),
sdk.NewAttribute(types.AttributeValueReservedOfferCoinFeeAmount, match.SwapMsgState.ReservedOfferCoinFee.Amount.String()),
sdk.NewAttribute(types.AttributeValueOrderExpiryHeight, strconv.FormatInt(match.SwapMsgState.OrderExpiryHeight, 10)),
sdk.NewAttribute(types.AttributeValueTransactedCoinAmount, transactedAmt.String()),
sdk.NewAttribute(types.AttributeValueRemainingOfferCoinAmount, sms.RemainingOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueExchangedOfferCoinAmount, sms.ExchangedOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueExchangedDemandCoinAmount, receiveAmt.String()),
sdk.NewAttribute(types.AttributeValueOfferCoinFeeAmount, offerCoinFeeAmt.String()),
sdk.NewAttribute(types.AttributeValueExchangedCoinFeeAmount, match.ExchangedCoinFeeAmt.String()),
sdk.NewAttribute(types.AttributeValueReservedOfferCoinFeeAmount, sms.ReservedOfferCoinFee.Amount.String()),
sdk.NewAttribute(types.AttributeValueOrderExpiryHeight, strconv.FormatInt(sms.OrderExpiryHeight, 10)),
sdk.NewAttribute(types.AttributeValueSuccess, types.Success),
))
} else {
// Not matched, remaining
sendCoin(batchEscrowAcc, sms.Msg.GetSwapRequester(), sms.RemainingOfferCoin.Add(sms.ReservedOfferCoinFee))
sms.Succeeded = false
sms.ToBeDeleted = true

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSwapTransacted,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(pool.Id, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(batch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(sms.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueSwapRequester, sms.Msg.GetSwapRequester().String()),
sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(sms.Msg.SwapTypeId), 10)),
sdk.NewAttribute(types.AttributeValueOfferCoinDenom, sms.Msg.OfferCoin.Denom),
sdk.NewAttribute(types.AttributeValueOfferCoinAmount, sms.Msg.OfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueDemandCoinDenom, sms.Msg.DemandCoinDenom),
sdk.NewAttribute(types.AttributeValueOrderPrice, sms.Msg.OrderPrice.String()),
sdk.NewAttribute(types.AttributeValueSwapPrice, batchResult.SwapPrice.String()),
sdk.NewAttribute(types.AttributeValueRemainingOfferCoinAmount, sms.RemainingOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueExchangedOfferCoinAmount, sms.ExchangedOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueReservedOfferCoinFeeAmount, sms.ReservedOfferCoinFee.Amount.String()),
sdk.NewAttribute(types.AttributeValueOrderExpiryHeight, strconv.FormatInt(sms.OrderExpiryHeight, 10)),
sdk.NewAttribute(types.AttributeValueSuccess, types.Failure),
))

}
}
if err := k.bankKeeper.InputOutputCoins(ctx, inputs, outputs); err != nil {
Expand Down
26 changes: 23 additions & 3 deletions x/liquidity/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package keeper

import (
"fmt"
"strconv"

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

"github.com/tendermint/liquidity/x/liquidity/types"
)

// Execute Swap of the pool batch, Collect swap messages in batch for transact the same price for each batch and run them on endblock.
func (k Keeper) SwapExecution(ctx sdk.Context, liquidityPoolBatch types.PoolBatch) (uint64, error) {
func (k Keeper) SwapExecution(ctx sdk.Context, poolBatch types.PoolBatch) (uint64, error) {
// get all swap message batch states that are not executed, not succeeded, and not to be deleted.
swapMsgStates := k.GetAllNotProcessedPoolBatchSwapMsgStates(ctx, liquidityPoolBatch)
swapMsgStates := k.GetAllNotProcessedPoolBatchSwapMsgStates(ctx, poolBatch)
if len(swapMsgStates) == 0 {
return 0, nil
}

pool, found := k.GetPool(ctx, liquidityPoolBatch.PoolId)
pool, found := k.GetPool(ctx, poolBatch.PoolId)
if !found {
return 0, types.ErrPoolNotExists
}
Expand All @@ -36,6 +37,25 @@ func (k Keeper) SwapExecution(ctx sdk.Context, liquidityPoolBatch types.PoolBatc
}
if !sms.ToBeDeleted {
swapMsgStatesNotToBeDeleted = append(swapMsgStatesNotToBeDeleted, sms)
} else {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSwapTransacted,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(pool.Id, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(sms.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueSwapRequester, sms.Msg.GetSwapRequester().String()),
sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(sms.Msg.SwapTypeId), 10)),
sdk.NewAttribute(types.AttributeValueOfferCoinDenom, sms.Msg.OfferCoin.Denom),
sdk.NewAttribute(types.AttributeValueOfferCoinAmount, sms.Msg.OfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueDemandCoinDenom, sms.Msg.DemandCoinDenom),
sdk.NewAttribute(types.AttributeValueOrderPrice, sms.Msg.OrderPrice.String()),
sdk.NewAttribute(types.AttributeValueRemainingOfferCoinAmount, sms.RemainingOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueExchangedOfferCoinAmount, sms.ExchangedOfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueReservedOfferCoinFeeAmount, sms.ReservedOfferCoinFee.Amount.String()),
sdk.NewAttribute(types.AttributeValueOrderExpiryHeight, strconv.FormatInt(sms.OrderExpiryHeight, 10)),
sdk.NewAttribute(types.AttributeValueSuccess, types.Failure),
))
}
}
k.SetPoolBatchSwapMsgStatesByPointer(ctx, pool.Id, swapMsgStates)
Expand Down
3 changes: 3 additions & 0 deletions x/liquidity/spec/07_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ swap_transacted | swap_requester | {swapRequesterAddress}
swap_transacted | swap_type_id | {swapTypeId}
swap_transacted | offer_coin_denom | {offerCoinDenom}
swap_transacted | offer_coin_amount | {offerCoinAmount}
swap_transacted | exchanged_coin_denom | {exchangedCoinDenom}
swap_transacted | order_price | {orderPrice}
swap_transacted | swap_price | {swapPrice}
swap_transacted | transacted_coin_amount | {transactedCoinAmount}
swap_transacted | remaining_offer_coin_amount | {remainingOfferCoinAmount}
swap_transacted | exchanged_offer_coin_amount | {exchangedOfferCoinAmount}
swap_transacted | exchanged_demand_coin_amount | {exchangedDemandCoinAmount}
swap_transacted | offer_coin_fee_amount | {offerCoinFeeAmount}
swap_transacted | exchanged_coin_fee_amount | {exchangedCoinFeeAmount}
swap_transacted | reserved_offer_coin_fee_amount | {reservedOfferCoinFeeAmount}
swap_transacted | order_expiry_height | {orderExpiryHeight}
swap_transacted | success | {success}
Expand Down
12 changes: 7 additions & 5 deletions x/liquidity/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ const (

AttributeValueDepositCoins = "deposit_coins"

AttributeValueOfferCoinDenom = "offer_coin_denom"
AttributeValueOfferCoinAmount = "offer_coin_amount"
AttributeValueOfferCoinFeeAmount = "offer_coin_fee_amount"
AttributeValueDemandCoinDenom = "demand_coin_denom"
AttributeValueOrderPrice = "order_price"
AttributeValueOfferCoinDenom = "offer_coin_denom"
AttributeValueOfferCoinAmount = "offer_coin_amount"
AttributeValueOfferCoinFeeAmount = "offer_coin_fee_amount"
AttributeValueExchangedCoinFeeAmount = "exchanged_coin_fee_amount"
AttributeValueDemandCoinDenom = "demand_coin_denom"
AttributeValueOrderPrice = "order_price"

AttributeValueDepositor = "depositor"
AttributeValueRefundedCoins = "refunded_coins"
Expand All @@ -41,6 +42,7 @@ const (
AttributeValueTransactedCoinAmount = "transacted_coin_amount"
AttributeValueRemainingOfferCoinAmount = "remaining_offer_coin_amount"
AttributeValueExchangedOfferCoinAmount = "exchanged_offer_coin_amount"
AttributeValueExchangedDemandCoinAmount = "exchanged_demand_coin_amount"
AttributeValueReservedOfferCoinFeeAmount = "reserved_offer_coin_fee_amount"
AttributeValueOrderExpiryHeight = "order_expiry_height"

Expand Down

0 comments on commit 634a84f

Please sign in to comment.