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

Prices integration in liquidity module #190

Merged
merged 1 commit into from
Jun 4, 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
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ func New(
app.GetSubspace(liquiditytypes.ModuleName),
app.accountKeeper,
app.bankKeeper,
&app.assetKeeper,
&app.marketKeeper,
&app.rewardskeeper,
)

Expand All @@ -627,6 +629,7 @@ func New(
&app.assetKeeper,
app.bankKeeper,
app.liquidityKeeper,
&app.marketKeeper,
)

wasmDir := filepath.Join(homePath, "wasm")
Expand Down
10 changes: 10 additions & 0 deletions x/liquidity/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package expected
import (
"time"

assettypes "github.com/comdex-official/comdex/x/asset/types"
rewardstypes "github.com/comdex-official/comdex/x/rewards/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -30,6 +31,15 @@ type BankKeeper interface {
InputOutputCoins(ctx sdk.Context, inputs []banktypes.Input, outputs []banktypes.Output) error
}

type AssetKeeper interface {
HasAssetForDenom(ctx sdk.Context, denom string) bool
GetAssetForDenom(ctx sdk.Context, denom string) (asset assettypes.Asset, found bool)
}

type MarketKeeper interface {
GetPriceForAsset(ctx sdk.Context, id uint64) (uint64, bool)
}

type RewardsKeeper interface {
GetAllGaugesByGaugeTypeID(ctx sdk.Context, gaugeTypeID uint64) (gauges []rewardstypes.Gauge)
GetEpochInfoByDuration(ctx sdk.Context, duration time.Duration) (epochInfo rewardstypes.EpochInfo, found bool)
Expand Down
6 changes: 6 additions & 0 deletions x/liquidity/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Keeper struct {
accountKeeper expected.AccountKeeper
bankKeeper expected.BankKeeper

assetKeeper expected.AssetKeeper
marketKeeper expected.MarketKeeper
rewardsKeeper expected.RewardsKeeper
}

Expand All @@ -31,6 +33,8 @@ func NewKeeper(
paramSpace paramstypes.Subspace,
accountKeeper expected.AccountKeeper,
bankKeeper expected.BankKeeper,
assetKeeper expected.AssetKeeper,
marketKeeper expected.MarketKeeper,
rewardsKeeper expected.RewardsKeeper,
) Keeper {
if !paramSpace.HasKeyTable() {
Expand All @@ -43,6 +47,8 @@ func NewKeeper(
paramSpace: paramSpace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
assetKeeper: assetKeeper,
marketKeeper: marketKeeper,
rewardsKeeper: rewardsKeeper,
}
}
Expand Down
8 changes: 8 additions & 0 deletions x/liquidity/keeper/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (k Keeper) getNextOrderIDWithUpdate(ctx sdk.Context, pair types.Pair) uint6

// ValidateMsgCreatePair validates types.MsgCreatePair.
func (k Keeper) ValidateMsgCreatePair(ctx sdk.Context, msg *types.MsgCreatePair) error {
if !k.assetKeeper.HasAssetForDenom(ctx, msg.BaseCoinDenom) {
return sdkerrors.Wrapf(types.ErrAssetNotWhiteListed, "asset with denom %s is not white listed", msg.BaseCoinDenom)
}

if !k.assetKeeper.HasAssetForDenom(ctx, msg.QuoteCoinDenom) {
return sdkerrors.Wrapf(types.ErrAssetNotWhiteListed, "asset with denom %s is not white listed", msg.QuoteCoinDenom)
}

if _, found := k.GetPairByDenoms(ctx, msg.BaseCoinDenom, msg.QuoteCoinDenom); found {
return types.ErrPairAlreadyExists
}
Expand Down
28 changes: 14 additions & 14 deletions x/liquidity/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ func (k Keeper) CalculateXYFromPoolCoin(ctx sdk.Context, ammPool *amm.BasicPool,
return x, y, nil
}

func oracle(denom string) (uint64, bool) {
if denom == "ucmdx" {
return 2000000, true
} else if denom == "ucgold" {
return 1800000000, true
} else if denom == "ucsilver" {
return 25000000, true
} else if denom == "ucoil" {
return 120000000, true
}
return 0, false
func (k Keeper) OraclePrice(ctx sdk.Context, denom string) (uint64, bool) {

asset, found := k.assetKeeper.GetAssetForDenom(ctx, denom)
if !found {
return 0, false
}

price, found := k.marketKeeper.GetPriceForAsset(ctx, asset.Id)
if !found {
return 0, false
}
return price, true
}

func (k Keeper) GetOraclePrices(ctx sdk.Context, quoteCoinDenom, baseCoinDenom string) (sdk.Dec, string, error) {
//nolint TODO : Use market module to get the prices
oraclePrice, found := oracle(quoteCoinDenom) // for quote coin
oraclePrice, found := k.OraclePrice(ctx, quoteCoinDenom)
denom := quoteCoinDenom
if !found {
oraclePrice, found = oracle(baseCoinDenom) // for base coin
oraclePrice, found = k.OraclePrice(ctx, baseCoinDenom)
denom = baseCoinDenom
if !found {
return sdk.NewDec(0), "", types.ErrOraclePricesNotFound
Expand Down
1 change: 1 addition & 0 deletions x/liquidity/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ var (
ErrSupplyValueCalculationInvalid = sdkerrors.Register(ModuleName, 26, "something went wrong while calculation supply values")
ErrInsufficientAvailableBalance = sdkerrors.Register(ModuleName, 27, "insufficient available balance")
ErrInvalidPairId = sdkerrors.Register(ModuleName, 28, "invalid pair id")
ErrAssetNotWhiteListed = sdkerrors.Register(ModuleName, 29, "asset not whitelisted")
)
8 changes: 8 additions & 0 deletions x/rewards/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ type AccountKeeper interface {
}

type LiquidityKeeper interface {
GetPair(ctx sdk.Context, id uint64) (pair liquiditytypes.Pair, found bool)
GetPool(ctx sdk.Context, id uint64) (pool liquiditytypes.Pool, found bool)
GetFarmingRewardsData(ctx sdk.Context, coinToDistribute sdk.Coin, liquidityGaugeData types.LiquidtyGaugeMetaData) ([]types.RewardDistributionDataCollector, error)
TransferFundsForSwapFeeDistribution(ctx sdk.Context, poolId uint64) (sdk.Coin, error)
}

type AssetKeeper interface {
GetPairsVault(ctx sdk.Context, id uint64) (pairs assettypes.ExtendedPairVault, found bool)
HasAssetForDenom(ctx sdk.Context, denom string) bool
GetAssetForDenom(ctx sdk.Context, denom string) (asset assettypes.Asset, found bool)
}

type MarketKeeper interface {
GetPriceForAsset(ctx sdk.Context, id uint64) (uint64, bool)
}

type LockerKeeper interface {
GetLockerProductAssetMapping(ctx sdk.Context, appMappingId uint64) (lockerProductMapping lockertypes.LockerProductAssetMapping, found bool)
GetLocker(ctx sdk.Context, lockerId string) (locker lockertypes.Locker, found bool)
Expand Down
46 changes: 41 additions & 5 deletions x/rewards/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,49 @@ func (k Keeper) ValidateMsgCreateCreateGauge(ctx sdk.Context, msg *types.MsgCrea
return nil
}

func (k Keeper) OraclePrice(ctx sdk.Context, denom string) (uint64, bool) {

asset, found := k.asset.GetAssetForDenom(ctx, denom)
if !found {
return 0, false
}

price, found := k.marketKeeper.GetPriceForAsset(ctx, asset.Id)
if !found {
return 0, false
}
return price, true
}

func (k Keeper) ValidateIfOraclePricesExists(ctx sdk.Context, pairId uint64) error {
pair, found := k.liquidityKeeper.GetPair(ctx, pairId)
if !found {
return sdkerrors.Wrapf(types.ErrPairNotExists, "pair does not exists for given pool id")
}

_, baseCoinPriceFound := k.OraclePrice(ctx, pair.BaseCoinDenom)
_, quoteCoinPricefound := k.OraclePrice(ctx, pair.QuoteCoinDenom)
if !(baseCoinPriceFound || quoteCoinPricefound) {
return sdkerrors.Wrapf(types.ErrPriceNotFound, "oracle price required for atleast %s or %s but not found", pair.QuoteCoinDenom, pair.BaseCoinDenom)
}

return nil
}

// ValidateMsgCreateGaugeLiquidityMetaData validates types.MsgCreateGauge_LiquidityMetaData.
func (k Keeper) ValidateMsgCreateGaugeLiquidityMetaData(ctx sdk.Context, kind *types.MsgCreateGauge_LiquidityMetaData) error {
_, found := k.liquidityKeeper.GetPool(ctx, kind.LiquidityMetaData.PoolId)
func (k Keeper) ValidateMsgCreateGaugeLiquidityMetaData(ctx sdk.Context, kind *types.MsgCreateGauge_LiquidityMetaData, forSwapFee bool) error {
pool, found := k.liquidityKeeper.GetPool(ctx, kind.LiquidityMetaData.PoolId)
if !found {
return types.ErrInvalidPoolID
}

if !forSwapFee {
err := k.ValidateIfOraclePricesExists(ctx, pool.PairId)
if err != nil {
return err
}
}

childPoolIds := kind.LiquidityMetaData.ChildPoolIds
for _, poolID := range childPoolIds {
_, found := k.liquidityKeeper.GetPool(ctx, poolID)
Expand All @@ -61,7 +97,7 @@ func (k Keeper) ValidateMsgCreateGaugeLiquidityMetaData(ctx sdk.Context, kind *t
}

// NewGauge returns the new Gauge object.
func (k Keeper) NewGauge(ctx sdk.Context, msg *types.MsgCreateGauge) (types.Gauge, error) {
func (k Keeper) NewGauge(ctx sdk.Context, msg *types.MsgCreateGauge, forSwapFee bool) (types.Gauge, error) {
newGauge := types.Gauge{
Id: k.GetGaugeID(ctx) + 1,
From: msg.From,
Expand All @@ -81,7 +117,7 @@ func (k Keeper) NewGauge(ctx sdk.Context, msg *types.MsgCreateGauge) (types.Gaug
switch extraData := msg.Kind.(type) {
case *types.MsgCreateGauge_LiquidityMetaData:

err := k.ValidateMsgCreateGaugeLiquidityMetaData(ctx, extraData)
err := k.ValidateMsgCreateGaugeLiquidityMetaData(ctx, extraData, forSwapFee)
if err != nil {
return types.Gauge{}, err
}
Expand Down Expand Up @@ -129,7 +165,7 @@ func (k Keeper) GetUpdatedGaugeIdsByTriggerDurationObj(ctx sdk.Context, triggerD
}

func (k Keeper) CreateNewGauge(ctx sdk.Context, msg *types.MsgCreateGauge, forSwapFee bool) error {
newGauge, err := k.NewGauge(ctx, msg)
newGauge, err := k.NewGauge(ctx, msg, forSwapFee)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions x/rewards/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type (
asset expected.AssetKeeper
bank expected.BankKeeper
liquidityKeeper expected.LiquidityKeeper
marketKeeper expected.MarketKeeper
}
)

Expand All @@ -40,6 +41,7 @@ func NewKeeper(
asset expected.AssetKeeper,
bank expected.BankKeeper,
liquidityKeeper expected.LiquidityKeeper,
marketKeeper expected.MarketKeeper,

) *Keeper {
// set KeyTable if it has not already been set
Expand All @@ -59,6 +61,7 @@ func NewKeeper(
asset: asset,
bank: bank,
liquidityKeeper: liquidityKeeper,
marketKeeper: marketKeeper,
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/rewards/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ var (
ErrNegativeTimeElapsed = sdkerrors.Register(ModuleName, 1102, "negative time elapsed since last interest time")
ErrAppIdExists = sdkerrors.Register(ModuleName, 1103, "Asset Id does not exist in locker for App_Mapping")
ErrAppIdDoesNotExists = sdkerrors.Register(ModuleName, 1104, "Asset Id does not exist in locker for App_Mapping")
ErrPairNotExists = sdkerrors.Register(ModuleName, 1105, "pair does not exists")
ErrPriceNotFound = sdkerrors.Register(ModuleName, 1106, "price not found")
)