Skip to content

Commit

Permalink
Merge pull request #589 from comdex-official/liquidation-tx
Browse files Browse the repository at this point in the history
events added in vault, auction, liquidation
  • Loading branch information
dheerajkd30 authored Nov 14, 2022
2 parents dbd8244 + 4b810d2 commit 99feef4
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 6 deletions.
4 changes: 2 additions & 2 deletions scripts/comdex_local_setup/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@
"app_id": 1,
"target_value": {"amount": "200", "denom": "uharbor"},
"cool_off_period": 7200,
"asset_id": [4,13],
"rates": [1000000,1000000],
"asset_id": [4,3,13],
"rates": [1000000,1000000,1000000],
}
}
],
Expand Down
44 changes: 44 additions & 0 deletions x/auction/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package auction

import (
"fmt"

"github.com/comdex-official/comdex/x/auction/expected"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -25,6 +27,21 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, assetKeeper expected.AssetKe
_ = utils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
err1 := k.SurplusActivator(ctx, data, killSwitchParams, status)
if err1 != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSurplusActivatorErr,
sdk.NewAttribute(types.DataAppID, fmt.Sprintf("%d", data.AppId)),
sdk.NewAttribute(types.DataAssetID, fmt.Sprintf("%d", data.AssetId)),
sdk.NewAttribute(types.DataAssetOutOraclePrice, fmt.Sprintf("%t", data.AssetOutOraclePrice)),
sdk.NewAttribute(types.DataAssetOutPrice, fmt.Sprintf("%d", data.AssetOutPrice)),
sdk.NewAttribute(types.DatIsAuctionActive, fmt.Sprintf("%t", data.IsAuctionActive)),
sdk.NewAttribute(types.DataIsDebtAuction, fmt.Sprintf("%t", data.IsDebtAuction)),
sdk.NewAttribute(types.DataIsDistributor, fmt.Sprintf("%t", data.IsDistributor)),
sdk.NewAttribute(types.DataIsSurplusAuction, fmt.Sprintf("%t", data.IsSurplusAuction)),
sdk.NewAttribute(types.KillSwitchParamsBreakerEnabled, fmt.Sprintf("%t", killSwitchParams.BreakerEnable)),
sdk.NewAttribute(types.Status, fmt.Sprintf("%t", status)),
),
)
ctx.Logger().Error("error in surplus activator")
return err1
}
Expand All @@ -33,6 +50,21 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, assetKeeper expected.AssetKe
_ = utils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
err2 := k.DebtActivator(ctx, data, killSwitchParams, status)
if err2 != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDebtActivatorErr,
sdk.NewAttribute(types.DataAppID, fmt.Sprintf("%d", data.AppId)),
sdk.NewAttribute(types.DataAssetID, fmt.Sprintf("%d", data.AssetId)),
sdk.NewAttribute(types.DataAssetOutOraclePrice, fmt.Sprintf("%t", data.AssetOutOraclePrice)),
sdk.NewAttribute(types.DataAssetOutPrice, fmt.Sprintf("%d", data.AssetOutPrice)),
sdk.NewAttribute(types.DatIsAuctionActive, fmt.Sprintf("%t", data.IsAuctionActive)),
sdk.NewAttribute(types.DataIsDebtAuction, fmt.Sprintf("%t", data.IsDebtAuction)),
sdk.NewAttribute(types.DataIsDistributor, fmt.Sprintf("%t", data.IsDistributor)),
sdk.NewAttribute(types.DataIsSurplusAuction, fmt.Sprintf("%t", data.IsSurplusAuction)),
sdk.NewAttribute(types.KillSwitchParamsBreakerEnabled, fmt.Sprintf("%t", killSwitchParams.BreakerEnable)),
sdk.NewAttribute(types.Status, fmt.Sprintf("%t", status)),
),
)
ctx.Logger().Error("error in debt activator")
return err2
}
Expand All @@ -47,11 +79,23 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, assetKeeper expected.AssetKe
for _, app := range apps {
err4 := k.RestartDutch(ctx, app.Id)
if err4 != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRestartDutchErr,
sdk.NewAttribute(types.DataAppID, fmt.Sprintf("%d", app.Id)),
),
)
ctx.Logger().Error("error in restart dutch activator")
}

err6 := k.RestartLendDutch(ctx, app.Id)
if err6 != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRestartLendDutchErr,
sdk.NewAttribute(types.DataAppID, fmt.Sprintf("%d", app.Id)),
),
)
ctx.Logger().Error("error in restart lend dutch activator")
}
}
Expand Down
33 changes: 30 additions & 3 deletions x/auction/keeper/dutch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"strconv"
"time"

liquidationtypes "github.com/comdex-official/comdex/x/liquidation/types"
Expand Down Expand Up @@ -144,6 +145,18 @@ func (k Keeper) StartDutchAuction(
return err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
auctiontypes.EventTypeDutchNewAuction,
sdk.NewAttribute(auctiontypes.DataAppID, strconv.FormatUint(auction.AppId, 10)),
sdk.NewAttribute(auctiontypes.AttributeKeyOwner, auction.VaultOwner.String()),
sdk.NewAttribute(auctiontypes.AttributeKeyCollateral, auction.OutflowTokenInitAmount.String()),
sdk.NewAttribute(auctiontypes.AttributeKeyDebt, auction.InflowTokenTargetAmount.String()),
sdk.NewAttribute(auctiontypes.AttributeKeyStartTime, auction.StartTime.String()),
sdk.NewAttribute(auctiontypes.AttributeKeyEndTime, auction.EndTime.String()),
),
)

return nil
}

Expand Down Expand Up @@ -510,7 +523,9 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appID uint64) error {
// if exists append in existing
// close auction func call
inflowLeft := dutchAuction.InflowTokenTargetAmount.Amount.Sub(dutchAuction.InflowTokenCurrentAmount.Amount)
vaultID, userExists := k.vault.GetUserAppExtendedPairMappingData(ctx, string(dutchAuction.VaultOwner), dutchAuction.AppId, lockedVault.ExtendedPairId)
penaltyCoin := sdk.NewCoin(dutchAuction.InflowTokenCurrentAmount.Denom, sdk.ZeroInt())
penaltyAmt := dutchAuction.InflowTokenTargetAmount.Amount.Sub(lockedVault.AmountOut)
vaultID, userExists := k.vault.GetUserAppExtendedPairMappingData(ctx, dutchAuction.VaultOwner.String(), dutchAuction.AppId, lockedVault.ExtendedPairId)
if userExists {
vaultData, _ := k.vault.GetVault(ctx, vaultID.VaultId)
if dutchAuction.OutflowTokenCurrentAmount.Amount.GT(sdk.ZeroInt()) {
Expand All @@ -521,7 +536,7 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appID uint64) error {
}
// append to existing vault
vaultData.AmountIn = vaultData.AmountIn.Add(dutchAuction.OutflowTokenCurrentAmount.Amount)
vaultData.AmountOut = vaultData.AmountOut.Add(inflowLeft)
vaultData.AmountOut = vaultData.AmountOut.Add(inflowLeft).Sub(penaltyAmt)
k.vault.SetVault(ctx, vaultData)
} else {
if dutchAuction.OutflowTokenCurrentAmount.Amount.GT(sdk.ZeroInt()) {
Expand All @@ -531,13 +546,25 @@ func (k Keeper) RestartDutchAuctions(ctx sdk.Context, appID uint64) error {
}
}
// create new vault done
err := k.vault.CreateNewVault(ctx, dutchAuction.VaultOwner.String(), lockedVault.AppId, lockedVault.ExtendedPairId, dutchAuction.OutflowTokenCurrentAmount.Amount, inflowLeft)
err := k.vault.CreateNewVault(ctx, dutchAuction.VaultOwner.String(), lockedVault.AppId, lockedVault.ExtendedPairId, dutchAuction.OutflowTokenCurrentAmount.Amount, inflowLeft.Sub(penaltyAmt))
if err != nil {
return err
}
length := k.vault.GetLengthOfVault(ctx)
k.vault.SetLengthOfVault(ctx, length+1)
}
// send penalty
if penaltyCoin.Amount.GT(sdk.ZeroInt()) {
err := k.bank.SendCoinsFromModuleToModule(ctx, auctiontypes.ModuleName, collectortypes.ModuleName, sdk.NewCoins(penaltyCoin))
if err != nil {
return err
}
}
// call increase function in collector
err := k.collector.SetNetFeeCollectedData(ctx, dutchAuction.AppId, dutchAuction.AssetInId, penaltyCoin.Amount)
if err != nil {
return err
}

dutchAuction.AuctionStatus = auctiontypes.AuctionEnded

Expand Down
24 changes: 24 additions & 0 deletions x/auction/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

const (
EventTypeSurplusActivatorErr = "surplus_activator_err"
EventTypeDebtActivatorErr = "debt_activator_err"
EventTypeRestartDutchErr = "restart_dutch_err"
EventTypeRestartLendDutchErr = "restart_lend_dutch_err"
EventTypeDutchNewAuction = "dutch_new_auction"
AttributeKeyOwner = "vault_owner"
AttributeKeyCollateral = "collateral_token"
AttributeKeyDebt = "debt_token"
AttributeKeyStartTime = "start_time"
AttributeKeyEndTime = "end_time"
DataAppID = "data_app_id"
DataAssetID = "data_asset_id"
DataAssetOutOraclePrice = "data_asset_out_oracle_price"
DataAssetOutPrice = "data_asset_out_price"
DatIsAuctionActive = "data_is_auction_active"
DataIsDebtAuction = "data_is_debt_auction"
DataIsDistributor = "data_is_distributor"
DataIsSurplusAuction = "data_is_surplus_auction"
KillSwitchParamsBreakerEnabled = "data_is_surplus_auction"
Status = "status"
)
2 changes: 1 addition & 1 deletion x/esm/keeper/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (k Keeper) SetUpDebtRedemptionForCollector(ctx sdk.Context, appID uint64) e
value, _ := k.GetAssetToAmount(ctx, data.AppId, data.AssetId)

if !value.IsCollateral && !data.NetFeesCollected.IsZero() {
// substract dollar value
// subtract dollar value
// burn token
// update collector data
assetData, found := k.asset.GetAsset(ctx, value.AssetID)
Expand Down
14 changes: 14 additions & 0 deletions x/liquidation/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package liquidation

import (
"fmt"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -15,9 +17,21 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
err := k.LiquidateVaults(ctx)
if err != nil {
ctx.Logger().Error("error in LiquidateVaults")
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeLiquidateVaultsErr,
sdk.NewAttribute(types.Error, fmt.Sprintf("%s", err)),
),
)
}
err = k.LiquidateBorrows(ctx)
if err != nil {
ctx.Logger().Error("error in LiquidateBorrows")
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeLiquidateBorrowsErr,
sdk.NewAttribute(types.Error, fmt.Sprintf("%s", err)),
),
)
}
}
7 changes: 7 additions & 0 deletions x/liquidation/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

const (
EventTypeLiquidateVaultsErr = "liquidate_vaults_err"
EventTypeLiquidateBorrowsErr = "liquidate_borrows_err"
Error = "error"
)
14 changes: 14 additions & 0 deletions x/market/keeper/oracle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"strconv"

assetTypes "github.com/comdex-official/comdex/x/asset/types"
"github.com/comdex-official/comdex/x/market/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -126,10 +128,22 @@ func (k Keeper) UpdatePriceList(ctx sdk.Context, id, scriptID, rate, twaBatch ui

func (k Keeper) CalculateTwa(ctx sdk.Context, twa types.TimeWeightedAverage, twaBatch uint64) uint64 {
var sum uint64
oldTwa := twa.Twa
for i := 0; i < int(twaBatch); i++ {
sum = sum + twa.PriceValue[i]
}
twa.Twa = sum / twaBatch

if oldTwa != twa.Twa {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeTwaChange,
sdk.NewAttribute(types.AttributeKeyAssetID, strconv.FormatUint(twa.AssetID, 10)),
sdk.NewAttribute(types.AttributeKeyOldTwa, strconv.FormatUint(oldTwa, 10)),
sdk.NewAttribute(types.AttributeKeyNewTwa, strconv.FormatUint(twa.Twa, 10)),
),
})
}
return twa.Twa
}

Expand Down
10 changes: 10 additions & 0 deletions x/market/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

// Event types for the market module.
const (
EventTypeTwaChange = "twa_change"

AttributeKeyOldTwa = "old_twa"
AttributeKeyNewTwa = "new_twa"
AttributeKeyAssetID = "assetId"
)
71 changes: 71 additions & 0 deletions x/vault/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strconv"

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

Expand Down Expand Up @@ -195,6 +196,21 @@ func (k msgServer) MsgCreate(c context.Context, msg *types.MsgCreateRequest) (*t

ctx.GasMeter().ConsumeGas(types.CreateVaultGas, "CreateVaultGas")

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCreateVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(newVault.Id, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyAmountIn, newVault.AmountIn.String()),
sdk.NewAttribute(types.AttributeKeyAmountOut, newVault.AmountOut.String()),
sdk.NewAttribute(types.AttributeKeyCreatedAt, ctx.BlockTime().String()),
sdk.NewAttribute(types.AttributeKeyInterestAccumulated, newVault.InterestAccumulated.String()),
sdk.NewAttribute(types.AttributeKeyClosingFeeAccumulated, newVault.ClosingFeeAccumulated.String()),
),
})

return &types.MsgCreateResponse{}, nil
}

Expand Down Expand Up @@ -290,6 +306,16 @@ func (k msgServer) MsgDeposit(c context.Context, msg *types.MsgDepositRequest) (
k.UpdateCollateralLockedAmountLockerMapping(ctx, appExtendedPairVaultData.AppId, appExtendedPairVaultData.ExtendedPairId, msg.Amount, true)

ctx.GasMeter().ConsumeGas(types.DepositVaultGas, "DepositVaultGas")
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDepositVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(msg.UserVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyAmountIn, msg.Amount.String()),
),
})

return &types.MsgDepositResponse{}, nil
}
Expand Down Expand Up @@ -398,6 +424,16 @@ func (k msgServer) MsgWithdraw(c context.Context, msg *types.MsgWithdrawRequest)
k.UpdateCollateralLockedAmountLockerMapping(ctx, appExtendedPairVaultData.AppId, appExtendedPairVaultData.ExtendedPairId, msg.Amount, false)

ctx.GasMeter().ConsumeGas(types.WithdrawVaultGas, "WithdrawVaultGas")
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeWithdrawVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(msg.UserVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyAmountIn, msg.Amount.String()),
),
})

return &types.MsgWithdrawResponse{}, nil
}
Expand Down Expand Up @@ -544,6 +580,17 @@ func (k msgServer) MsgDraw(c context.Context, msg *types.MsgDrawRequest) (*types

ctx.GasMeter().ConsumeGas(types.DrawVaultGas, "DrawVaultGas")

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDrawVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(msg.UserVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyAmountOut, msg.Amount.String()),
),
})

return &types.MsgDrawResponse{}, nil
}

Expand Down Expand Up @@ -696,6 +743,17 @@ func (k msgServer) MsgRepay(c context.Context, msg *types.MsgRepayRequest) (*typ

ctx.GasMeter().ConsumeGas(types.RepayVaultGas, "RepayVaultGas")

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRepayVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(msg.UserVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyAmountOut, msg.Amount.String()),
),
})

return &types.MsgRepayResponse{}, nil
}

Expand Down Expand Up @@ -838,6 +896,19 @@ func (k msgServer) MsgClose(c context.Context, msg *types.MsgCloseRequest) (*typ

ctx.GasMeter().ConsumeGas(types.CloseVaultGas, "CloseVaultGas")

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCloseVault,
sdk.NewAttribute(types.AttributeKeyVaultID, strconv.FormatUint(userVault.Id, 10)),
sdk.NewAttribute(types.AttributeKeyAppID, strconv.FormatUint(msg.AppId, 10)),
sdk.NewAttribute(types.AttributeKeyExtendedPairID, strconv.FormatUint(msg.ExtendedPairVaultId, 10)),
sdk.NewAttribute(types.AttributeKeyCreator, msg.From),
sdk.NewAttribute(types.AttributeKeyCreatedAt, userVault.CreatedAt.String()),
sdk.NewAttribute(types.AttributeKeyInterestAccumulated, userVault.InterestAccumulated.String()),
sdk.NewAttribute(types.AttributeKeyClosingFeeAccumulated, userVault.ClosingFeeAccumulated.String()),
),
})

return &types.MsgCloseResponse{}, nil
}

Expand Down
Loading

0 comments on commit 99feef4

Please sign in to comment.