8000 checks for market price added in liquidation,auction by cgsingh33 · Pull Request #184 · comdex-official/comdex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

checks for market price added in liquidation,auction #184

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

Merged
merged 1 commit into from
Jun 3, 2022
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
15 changes: 8 additions & 7 deletions x/asset/keeper/pairs_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,14 @@ func (k *Keeper) WasmUpdateLsrInPairsVault(ctx sdk.Context, app_id, ex_pair_id u
return types.ErrorExtendedPairDoesNotExistForTheApp
}

var (
store = k.Store(ctx)
key = types.PairsKey(app_id)
value = k.cdc.MustMarshal(&ExtPairVaultData)
)

store.Set(key, value)
k.SetPairsVault(ctx, ExtPairVaultData)
// var (
// store = k.Store(ctx)
// key = types.PairsKey(app_id)
// value = k.cdc.MustMarshal(&ExtPairVaultData)
// )

// store.Set(key, value)
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions x/auction/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
assettypes "github.com/comdex-official/comdex/x/asset/types"
"github.com/comdex-official/comdex/x/collector/types"
liquidationtypes "github.com/comdex-official/comdex/x/liquidation/types"
vaultttypes "github.com/comdex-official/comdex/x/vault/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
Expand Down Expand Up @@ -38,6 +39,7 @@ type AssetKeeper interface {
GetAsset(ctx sdk.Context, id uint64) (assettypes.Asset, bool)
GetPair(ctx sdk.Context, id uint64) (assettypes.Pair, bool)
GetApps(ctx sdk.Context) (apps []assettypes.AppMapping, found bool)
GetApp(ctx sdk.Context, id uint64) (app assettypes.AppMapping, found bool)
GetPairsVault(ctx sdk.Context, id uint64) (pairs assettypes.ExtendedPairVault, found bool)
}

Expand All @@ -50,6 +52,8 @@ type VaultKeeper interface {
// assetOut assettypes.Asset,
//) (sdk.Dec, error)
//BurnCAssets(ctx sdk.Context, moduleName string, collateralDenom string, denom string, amount sdk.Int) error
GetAppExtendedPairVaultMapping(ctx sdk.Context, appMappingId uint64) (appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping, found bool)
SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping) error
}

type CollectorKeeper interface {
Expand All @@ -59,6 +63,7 @@ type CollectorKeeper interface {
GetCollectorLookupTable(ctx sdk.Context, app_id uint64) (collectorLookup types.CollectorLookup, found bool)
SetCollectorAuctionLookupTable(ctx sdk.Context, records ...types.CollectorAuctionLookupTable) error
GetCollectorAuctionLookupTable(ctx sdk.Context, app_id uint64) (appIdToAuctionData types.CollectorAuctionLookupTable, found bool)
GetAuctionMappingForApp(ctx sdk.Context, appId uint64) (collectorAuctionLookupTable types.CollectorAuctionLookupTable, found bool)
GetNetFeeCollectedData(ctx sdk.Context, app_id uint64) (netFeeData types.NetFeeCollectedData, found bool)
GetAmountFromCollector(ctx sdk.Context, appId, asset_id uint64, amount sdk.Int) (sdk.Int, error)
SetNetFeeCollectedData(ctx sdk.Context, app_id, asset_id uint64, fee sdk.Int) error
Expand Down
16 changes: 16 additions & 0 deletions x/auction/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
assettypes "github.com/comdex-official/comdex/x/asset/types"
vaultttypes "github.com/comdex-official/comdex/x/vault/types"
"github.com/comdex-official/comdex/x/collector/types"
liquidationtypes "github.com/comdex-official/comdex/x/liquidation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -113,6 +114,9 @@ func (k *Keeper) GetNetFeeCollectedData(ctx sdk.Context, app_id uint64) (netFeeD
func (k *Keeper) GetApps(ctx sdk.Context) (apps []assettypes.AppMapping, found bool) {
return k.asset.GetApps(ctx)
}
func (k *Keeper) GetApp(ctx sdk.Context, id uint64) (app assettypes.AppMapping, found bool) {
return k.asset.GetApp(ctx, id)
}

func (k *Keeper) MintNewTokensForApp(ctx sdk.Context, appMappingId uint64, assetId uint64, address string, amount sdk.Int) error {
return k.tokenmint.MintNewTokensForApp(ctx, appMappingId, assetId, address, amount)
Expand Down Expand Up @@ -141,3 +145,15 @@ func (k *Keeper) SetLockedVault(ctx sdk.Context, locked_vault liquidationtypes.L
func (k *Keeper) GetPairsVault(ctx sdk.Context, id uint64) (pairs assettypes.ExtendedPairVault, found bool) {
return k.asset.GetPairsVault(ctx, id)
}

func (k *Keeper) GetAppExtendedPairVaultMapping(ctx sdk.Context, appMappingId uint64) (appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping, found bool) {
return k.vault.GetAppExtendedPairVaultMapping(ctx, appMappingId)
}

func (k *Keeper) SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPairVaultData vaultttypes.AppExtendedPairVaultMapping)error {
return k.vault.SetAppExtendedPairVaultMapping(ctx, appExtendedPairVaultData)
}

func (k *Keeper) GetAuctionMappingForApp(ctx sdk.Context, appId uint64) (collectorAuctionLookupTable types.CollectorAuctionLookupTable, found bool){
return k.collector.GetAuctionMappingForApp(ctx, appId)
}
93 changes: 77 additions & 16 deletions x/auction/keeper/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,39 @@ func (k Keeper) DecreaseLockedVaultAmountOut(ctx sdk.Context, lockedVaultId uint
return nil
}

func (k Keeper) getInflowTokenAmount(ctx sdk.Context, AssetInId, AssetOutId uint64, lotSize sdk.Int) (status uint64, outflowToken, inflowToken sdk.Coin) {
func (k Keeper) getInflowTokenAmount(ctx sdk.Context, appId, AssetInId, AssetOutId uint64, lotSize sdk.Int) (status uint64, outflowToken, inflowToken sdk.Coin) {
emptyCoin := sdk.NewCoin("empty", sdk.NewIntFromUint64(1))
outflowAsset, found1 := k.GetAsset(ctx, AssetOutId)
inflowAsset, found2 := k.GetAsset(ctx, AssetInId)
if !found1 || !found2 {
return auctiontypes.NoAuction, emptyCoin, emptyCoin
}

var inFlowTokenPrice uint64
collectorAuction, _ := k.GetAuctionMappingForApp(ctx, appId)
for _, data := range collectorAuction.AssetIdToAuctionLookup {

if data.AssetOutOraclePrice {
fmt.Println(data.AssetOutOraclePrice, "value bool price required")
//If oracle Price required for the assetOut
inFlowTokenPrice, _ = k.GetPriceForAsset(ctx, AssetInId)

fmt.Println(inFlowTokenPrice, "should be what is set dollar ")
} else {
//If oracle Price is not required for the assetOut
inFlowTokenPrice = data.AssetOutPrice

}

}

outflowToken = sdk.NewCoin(outflowAsset.Denom, lotSize)
outflowTokenPrice, found3 := k.GetPriceForAsset(ctx, outflowAsset.Id)
inflowTokenPrice, found4 := k.GetPriceForAsset(ctx, inflowAsset.Id)
if !found3 || !found4 {
// inflowTokenPrice, found4 := k.GetPriceForAsset(ctx, inflowAsset.Id)
if !found3 {
return auctiontypes.NoAuction, emptyCoin, emptyCoin
}
inflowTokenAmount := outflowToken.Amount.Mul(sdk.NewIntFromUint64(outflowTokenPrice)).Quo(sdk.NewIntFromUint64(inflowTokenPrice))
inflowTokenAmount := outflowToken.Amount.Mul(sdk.NewIntFromUint64(outflowTokenPrice)).Quo(sdk.NewIntFromUint64(inFlowTokenPrice))
inflowToken = sdk.NewCoin(inflowAsset.Denom, inflowTokenAmount)
return 5, outflowToken, inflowToken
}
Expand Down Expand Up @@ -86,7 +105,7 @@ func (k Keeper) checkStatusOfNetFeesCollectedAndStartAuction(ctx sdk.Context, ap
//calculate inflow token amount
assetInId := collector.CollectorAssetId
assetOutId := collector.SecondaryAssetId
status, inflowToken, outflowToken := k.getInflowTokenAmount(ctx, assetInId, assetOutId, sdk.NewIntFromUint64(collector.LotSize))
status, inflowToken, outflowToken := k.getInflowTokenAmount(ctx, appId, assetInId, assetOutId, sdk.NewIntFromUint64(collector.LotSize))
if status == auctiontypes.NoAuction {
return auctiontypes.NoAuction, nil
}
Expand All @@ -102,7 +121,7 @@ func (k Keeper) checkStatusOfNetFeesCollectedAndStartAuction(ctx sdk.Context, ap
//calculate inflow token amount
assetInId := collector.SecondaryAssetId
assetOutId := collector.CollectorAssetId
status, inflowToken, outflowToken := k.getInflowTokenAmount(ctx, assetInId, assetOutId, sdk.NewIntFromUint64(collector.LotSize))
status, inflowToken, outflowToken := k.getInflowTokenAmount(ctx, appId, assetInId, assetOutId, sdk.NewIntFromUint64(collector.LotSize))
if status == auctiontypes.NoAuction {
return auctiontypes.NoAuction, nil
}
Expand Down Expand Up @@ -269,7 +288,7 @@ func (k Keeper) CloseSurplusAuctions(ctx sdk.Context, appId uint64) error {
for _, surplusAuction := range surplusAuctions {
if ctx.BlockTime().After(surplusAuction.EndTime) {
if surplusAuction.AuctionStatus == auctiontypes.AuctionStartNoBids {
err := k.RestartSurplusAuction(ctx, surplusAuction)
err := k.RestartSurplusAuction(ctx, appId, surplusAuction)
if err != nil {
return err
}
Expand All @@ -290,7 +309,7 @@ func (k Keeper) CloseDebtAuctions(ctx sdk.Context, appId uint64) error {
for _, debtAuction := range debtAuctions {
if ctx.BlockTime().After(debtAuction.EndTime) {
if debtAuction.AuctionStatus == auctiontypes.AuctionStartNoBids {
err := k.RestartDebtAuction(ctx, debtAuction)
err := k.RestartDebtAuction(ctx, appId, debtAuction)
if err != nil {
return err
}
Expand All @@ -307,9 +326,10 @@ func (k Keeper) CloseDebtAuctions(ctx sdk.Context, appId uint64) error {

func (k Keeper) RestartDebtAuction(
ctx sdk.Context,
appId uint64,
debtAuction auctiontypes.DebtAuction,
) error {
status, inflowToken, _ := k.getInflowTokenAmount(ctx, debtAuction.AssetInId, debtAuction.AssetOutId, debtAuction.AuctionedToken.Amount)
status, inflowToken, _ := k.getInflowTokenAmount(ctx, appId, debtAuction.AssetInId, debtAuction.AssetOutId, debtAuction.AuctionedToken.Amount)
if status == auctiontypes.NoAuction {
return nil
}
Expand All @@ -325,9 +345,10 @@ func (k Keeper) RestartDebtAuction(

func (k Keeper) RestartSurplusAuction(
ctx sdk.Context,
appId uint64,
surplusAuction auctiontypes.SurplusAuction,
) error {
status, inflowToken, _ := k.getInflowTokenAmount(ctx, surplusAuction.AssetInId, surplusAuction.AssetOutId, surplusAuction.OutflowToken.Amount)
status, inflowToken, _ := k.getInflowTokenAmount(ctx, appId, surplusAuction.AssetInId, surplusAuction.AssetOutId, surplusAuction.OutflowToken.Amount)
if status == auctiontypes.NoAuction {
return nil
}
Expand Down Expand Up @@ -469,10 +490,28 @@ func (k Keeper) StartDutchAuction(
var (
inFlowTokenPrice uint64
outFlowTokenPrice uint64
found1 bool
found2 bool
// found1 bool
found2 bool
)

lockedVault, _ := k.GetLockedVault(ctx, lockedVaultId)

var extendedPairVault = lockedVault.ExtendedPairId

ExtendedPairVault, _ := k.GetPairsVault(ctx, extendedPairVault)

if ExtendedPairVault.AssetOutOraclePrice {
fmt.Println(ExtendedPairVault.AssetOutOraclePrice, "value bool price required")
//If oracle Price required for the assetOut
inFlowTokenPrice, _ = k.GetPriceForAsset(ctx, assetInId)

fmt.Println(inFlowTokenPrice, "should be what is set dollar ")
} else {
//If oracle Price is not required for the assetOut
inFlowTokenPrice = ExtendedPairVault.AssetOutPrice

}

err := k.SendCoinsFromModuleToModule(ctx, vaulttypes.ModuleName, auctiontypes.ModuleName, sdk.NewCoins(outFlowToken))
if err != nil {
fmt.Println("print err", err)
Expand All @@ -484,10 +523,10 @@ func (k Keeper) StartDutchAuction(
//need to get real price instead of hard coding
//calculate target amount of cmst to collect
if auctiontypes.TestFlag != 1 {
inFlowTokenPrice, found1 = k.GetPriceForAsset(ctx, assetInId)
if !found1 {
return auctiontypes.ErrorPrices
}
// inFlowTokenPrice, found1 = k.GetPriceForAsset(ctx, assetInId)
// if !found1 {
// return auctiontypes.ErrorPrices
// }
outFlowTokenPrice, found2 = k.GetPriceForAsset(ctx, assetOutId)
if !found2 {
return auctiontypes.ErrorPrices
Expand Down Expand Up @@ -811,6 +850,28 @@ func (k Keeper) CloseDutchAuction(
if err != nil {
return err
}
var appExtendedPairVaultData vaulttypes.AppExtendedPairVaultMapping
var extendedPairVaultMapping vaulttypes.ExtendedPairVaultMapping

appExtpair, _ := k.GetAppExtendedPairVaultMapping(ctx, lockedVault.AppMappingId)
appExtendedPairVaultData.AppMappingId = lockedVault.AppMappingId
appExtendedPairVaultData.Counter = appExtpair.Counter

for _, data := range appExtpair.ExtendedPairVaults {
if data.ExtendedPairId == lockedVault.ExtendedPairId {
extendedPairVaultMapping.ExtendedPairId = lockedVault.ExtendedPairId
extendedPairVaultMapping.VaultIds = data.VaultIds
extendedPairVaultMapping.CollateralLockedAmount = data.CollateralLockedAmount.Sub(outFlowToken.Amount)
extendedPairVaultMapping.TokenMintedAmount = data.TokenMintedAmount.Sub(burnToken.Amount)
}
}
appExtendedPairVaultData.ExtendedPairVaults = append(appExtendedPairVaultData.ExtendedPairVaults, &extendedPairVaultMapping)

err = k.SetAppExtendedPairVaultMapping(ctx, appExtendedPairVaultData)
if err != nil {
return err
}

return nil
}

Expand Down
15 changes: 8 additions & 7 deletions x/collector/keeper/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,14 @@ func (k *Keeper) WasmUpdateLsrInCollectorLookupTable(ctx sdk.Context, appId, ass
accmLookup.AppId = appId
accmLookup.AssetRateInfo = append(accmLookup.AssetRateInfo, Collector)

var (
store = ctx.KVStore(k.storeKey)
key = types.CollectorLookupTableMappingKey(appId)
value = k.cdc.MustMarshal(&accmLookup)
)

store.Set(key, value)
k.SetCollectorLookupTable(ctx, Collector)
// var (
// store = ctx.KVStore(k.storeKey)
// key = types.CollectorLookupTableMappingKey(appId)
// value = k.cdc.MustMarshal(&accmLookup)
// )

// store.Set(key, value)
return nil
}

Expand Down
26 changes: 24 additions & 2 deletions x/liquidation/keeper/liquidate_vaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (k Keeper) CreateLockedVault(ctx sdk.Context, vault vaulttypes.Vault, colla
}

func (k Keeper) UpdateLockedVaultsAppMapping(ctx sdk.Context, lockedVault types.LockedVault) {
LockedVaultToApp, _ := k.GetLockedVaultByAppId(ctx, lockedVault.LockedVaultId)
LockedVaultToApp, _ := k.GetLockedVaultByAppId(ctx, lockedVault.AppMappingId)
LockedVaultToApp.LockedVault = append(LockedVaultToApp.LockedVault, &lockedVault)

newLockedVaultToApp := types.LockedVaultToAppMapping{
Expand Down Expand Up @@ -163,8 +163,30 @@ func (k Keeper) UpdateLockedVaults(ctx sdk.Context) error {
}
fmt.Println("red3______________")
//Asset Price in Dollar Terms to find how how much is to be auctioned

extendedPairVault, found := k.GetPairsVault(ctx, lockedVault.ExtendedPairId)
if !found {
return types.ErrorExtendedPairVaultDoesNotExists
}
var assetOutPrice uint64

if extendedPairVault.AssetOutOraclePrice {
fmt.Println(extendedPairVault.AssetOutOraclePrice, "value bool price required")
//If oracle Price required for the assetOut
assetOutPrice, found = k.GetPriceForAsset(ctx, assetOut.Id)
fmt.Println(assetOutPrice, "should be what is set dollar ")

if !found {
return types.ErrorPriceDoesNotExist
}
} else {
//If oracle Price is not required for the assetOut
assetOutPrice = extendedPairVault.AssetOutPrice

}

assetInPrice, _ := k.GetPriceForAsset(ctx, assetIn.Id)
assetOutPrice, _ := k.GetPriceForAsset(ctx, assetOut.Id)
// assetOutPrice, _ := k.GetPriceForAsset(ctx, assetOut.Id)

totalIn := lockedVault.AmountIn.Mul(sdk.NewIntFromUint64(assetInPrice)).ToDec()
totalOut := lockedVault.AmountOut.Mul(sdk.NewIntFromUint64(assetOutPrice)).ToDec()
Expand Down
2 changes: 2 additions & 0 deletions x/liquidation/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

var (
LockedVaultDoesNotExist = sdkerrors.Register(ModuleName, 201, "locked vault does not exist with given id")
ErrorExtendedPairVaultDoesNotExists = sdkerrors.Register(ModuleName, 202, "Extended pair vault does not exists for the given id")
ErrorPriceDoesNotExist = sdkerrors.Register(ModuleName, 203, "Price does not exist")
ErrAppIdExists = sdkerrors.Register(ModuleName, 1101, "Asset Id does not exist in locker for App_Mapping")
ErrAppIdDoesNotExists = sdkerrors.Register(ModuleName, 1102, "Asset Id does not exist in locker for App_Mapping")
)
3 changes: 2 additions & 1 deletion x/vault/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (k *Keeper) CheckUserToAppMapping(ctx sdk.Context, userVaultAssetData types
}

//Set AppExtendedPairVaultMapping to check the current status of the vault by extended pair vault id
func (k *Keeper) SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPairVaultData types.AppExtendedPairVaultMapping) {
func (k *Keeper) SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPairVaultData types.AppExtendedPairVaultMapping)error {

var (
store = k.Store(ctx)
Expand All @@ -85,6 +85,7 @@ func (k *Keeper) SetAppExtendedPairVaultMapping(ctx sdk.Context, appExtendedPair
)

store.Set(key, value)
return nil

}

Expand Down
0