Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auction: fix abci aution #81

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions x/auction/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (

func (k *Keeper) BeginBlocker(ctx context.Context) error {
// get allowed mint denom
allowedMintDenoms := k.vaultKeeper.GetAllowedMintDenoms(ctx)

for _, mintDenom := range allowedMintDenoms {
err := k.handleLiquidation(ctx, mintDenom)
if err != nil {
return err
}
err := k.handleLiquidation(ctx)
if err != nil {
return err
}

return nil
Expand Down
78 changes: 43 additions & 35 deletions x/auction/keeper/liquidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
vaultstypes "github.com/onomyprotocol/reserve/x/vaults/types"
)

func (k Keeper) handleLiquidation(ctx context.Context, mintDenom string) error {
func (k Keeper) handleLiquidation(ctx context.Context) error {
params := k.GetParams(ctx)
allowedMintDenoms := k.vaultKeeper.GetAllowedMintDenoms(ctx)

currentTime := sdk.UnwrapSDKContext(ctx).BlockHeader().Time
lastestAuctionPeriod, err := k.LastestAuctionPeriods.Get(ctx, "LastestAuctionPeriods")
Expand All @@ -26,58 +27,61 @@ func (k Keeper) handleLiquidation(ctx context.Context, mintDenom string) error {
if err != nil {
return err
}

liquidations, err := k.vaultKeeper.GetLiquidations(ctx, mintDenom)
if err != nil {
return err
}

liquidatedVaults := make([]*vaultstypes.Vault, 0)
for _, liq := range liquidations {
liquidatedVaults = append(liquidatedVaults, liq.LiquidatingVaults...)
}

// create new auction for this vault
for _, vault := range liquidatedVaults {
//calcualte initial price and target price
auction, isCreate, err := k.GetNewAuction(ctx, currentTime, vault.LiquidationPrice, vault.CollateralLocked, vault.Debt, vault.Id)
for _, mintDenom := range allowedMintDenoms {
liquidations, err := k.vaultKeeper.GetLiquidations(ctx, mintDenom)
if err != nil {
return err
}

if isCreate {
err = k.Auctions.Set(ctx, auction.AuctionId, *auction)
liquidatedVaults := make([]*vaultstypes.Vault, 0)
for _, liq := range liquidations {
liquidatedVaults = append(liquidatedVaults, liq.LiquidatingVaults...)
}

// create new auction for this vault
for _, vault := range liquidatedVaults {
//calcualte initial price and target price
auction, isCreate, err := k.GetNewAuction(ctx, currentTime, vault.LiquidationPrice, vault.CollateralLocked, vault.Debt, vault.Id)
if err != nil {
return err
}
err = k.Bids.Set(ctx, auction.AuctionId, types.BidQueue{AuctionId: auction.AuctionId, Bids: []types.Bid{}})
if err != nil {
return err

if isCreate {
err = k.Auctions.Set(ctx, auction.AuctionId, *auction)
if err != nil {
return err
}
err = k.Bids.Set(ctx, auction.AuctionId, types.BidQueue{AuctionId: auction.AuctionId, Bids: []types.Bid{}})
if err != nil {
return err
}
err = k.BidIdSeq.Set(ctx, auction.AuctionId, 0)
if err != nil {
return err
}
}
err = k.BidIdSeq.Set(ctx, auction.AuctionId, 0)
if err != nil {
return err
}
}
if err != nil {
return err
}
}
}

// loop through all auctions
// get liquidations data then distribute debt & collateral remain
liquidationMap, err := k.newLiquidateMap(ctx, mintDenom, params)
if err != nil {
return err
}

// Loop through liquidationMap and liquidate
for _, liq := range liquidationMap {
err := k.vaultKeeper.Liquidate(ctx, *liq, mintDenom)
for _, mintDenom := range allowedMintDenoms {
// loop through all auctions
// get liquidations data then distribute debt & collateral remain
liquidationMap, err := k.newLiquidateMap(ctx, mintDenom, params)
if err != nil {
return err
}

// Loop through liquidationMap and liquidate
for _, liq := range liquidationMap {
err := k.vaultKeeper.Liquidate(ctx, *liq, mintDenom)
if err != nil {
return err
}
}
}

return nil
Expand All @@ -102,6 +106,10 @@ func (k Keeper) newLiquidateMap(ctx context.Context, mintDenom string, params ty
return true, err
}

if vault.Debt.Denom != mintDenom {
return false, nil
}

needCleanup := false
currentRate := math.LegacyMustNewDecFromStr(auction.CurrentRate)
lowestRate := math.LegacyMustNewDecFromStr(params.LowestRate)
Expand Down
Loading