Skip to content

Commit

Permalink
refactor: move some functions to types (#256)
Browse files Browse the repository at this point in the history
* refactor: move some functions to types

* test: fix tests

* test: fix

* test: fix
  • Loading branch information
cosmic-vagabond authored Nov 20, 2023
1 parent df718f5 commit 547de88
Show file tree
Hide file tree
Showing 44 changed files with 531 additions and 1,275 deletions.
42 changes: 5 additions & 37 deletions x/accountedpool/keeper/accounted_pool_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (
margintypes "github.com/elys-network/elys/x/margin/types"
)

// Get Amm Pool Balance
func (k Keeper) GetAmmPoolBalance(ammPool ammtypes.Pool, denom string) sdk.Int {
for _, asset := range ammPool.PoolAssets {
if asset.Token.Denom == denom {
return asset.Token.Amount
}
}

return sdk.ZeroInt()
}

func (k Keeper) GetMarginPoolBalancesByPosition(marginPool margintypes.Pool, denom string, position margintypes.Position) (sdk.Int, sdk.Int, sdk.Int) {
poolAssets := marginPool.GetPoolAssets(position)

for _, asset := range *poolAssets {
if asset.AssetDenom == denom {
return asset.AssetBalance, asset.Liabilities, asset.Custody
}
}

return sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
}

// Get Margin Pool Balance
func (k Keeper) GetMarginPoolBalances(marginPool margintypes.Pool, denom string) (sdk.Int, sdk.Int, sdk.Int) {
assetBalanceLong, liabilitiesLong, custodyLong := k.GetMarginPoolBalancesByPosition(marginPool, denom, margintypes.Position_LONG)
assetBalanceShort, liabilitiesShort, custodyShort := k.GetMarginPoolBalancesByPosition(marginPool, denom, margintypes.Position_SHORT)

assetBalance := assetBalanceLong.Add(assetBalanceShort)
liabilities := liabilitiesLong.Add(liabilitiesShort)
custody := custodyLong.Add(custodyShort)

return assetBalance, liabilities, custody
}

func (k Keeper) UpdateAccountedPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) error {
poolId := ammPool.PoolId
// Check if already exists
Expand All @@ -61,8 +26,11 @@ func (k Keeper) UpdateAccountedPool(ctx sdk.Context, ammPool ammtypes.Pool, marg
// amm pool balance + margin pool balance + margin pool liabilties - margin pool custody
// But not deducting custody amount here as the balance was already deducted through TakeCustody function.
for i, asset := range accountedPool.PoolAssets {
aBalance := k.GetAmmPoolBalance(ammPool, asset.Token.Denom)
mBalance, mLiabiltiies, _ := k.GetMarginPoolBalances(marginPool, asset.Token.Denom)
aBalance, err := margintypes.GetAmmPoolBalance(ammPool, asset.Token.Denom)
if err != nil {
return err
}
mBalance, mLiabiltiies, _ := margintypes.GetMarginPoolBalances(marginPool, asset.Token.Denom)
accountedAmt := aBalance.Add(mBalance).Add(mLiabiltiies)
accountedPool.PoolAssets[i].Token = sdk.NewCoin(asset.Token.Denom, accountedAmt)
}
Expand Down
2 changes: 1 addition & 1 deletion x/margin/keeper/calc_mtp_interest_liabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (k Keeper) CalcMTPBorrowInterestLiabilities(ctx sdk.Context, mtp *types.MTP

rate.SetFloat64(borrowInterestRate.MustFloat64())

collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "")
collateralIndex, _ := types.GetMTPAssetIndex(mtp, collateralAsset, "")
unpaidCollaterals := sdk.ZeroInt()
// Calculate collateral borrow interests in base currency
if mtp.Collaterals[collateralIndex].Denom == baseCurrency {
Expand Down
23 changes: 0 additions & 23 deletions x/margin/keeper/check_long_assets.go

This file was deleted.

58 changes: 0 additions & 58 deletions x/margin/keeper/check_long_assets_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/margin/keeper/check_same_asset_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/elys-network/elys/x/margin/types"
)

func (k Keeper) CheckSamePosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP {
func (k Keeper) CheckSameAssetPosition(ctx sdk.Context, msg *types.MsgOpen) *types.MTP {
mtps := k.GetAllMTPs(ctx)
for _, mtp := range mtps {
if mtp.Address == msg.Creator && mtp.Position == msg.Position {
Expand Down
4 changes: 2 additions & 2 deletions x/margin/keeper/check_same_asset_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCheckSameAssets_NewPosition(t *testing.T) {
func TestCheckSameAssetPosition_NewPosition(t *testing.T) {
app := simapp.InitElysTestApp(true)
ctx := app.BaseApp.NewContext(true, tmproto.Header{})

Expand All @@ -29,7 +29,7 @@ func TestCheckSameAssets_NewPosition(t *testing.T) {
Leverage: sdk.NewDec(1),
}

mtp = k.CheckSamePosition(ctx, msg)
mtp = k.CheckSameAssetPosition(ctx, msg)

// Expect no error
assert.Nil(t, mtp)
Expand Down
26 changes: 0 additions & 26 deletions x/margin/keeper/check_short_assets.go

This file was deleted.

59 changes: 0 additions & 59 deletions x/margin/keeper/check_short_assets_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/margin/keeper/estimate_and_repay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (k Keeper) EstimateAndRepay(ctx sdk.Context, mtp types.MTP, pool types.Pool, ammPool ammtypes.Pool, collateralAsset string, custodyAsset string) (sdk.Int, error) {
collateralIndex, custodyIndex := k.GetMTPAssetIndex(&mtp, collateralAsset, custodyAsset)
collateralIndex, custodyIndex := types.GetMTPAssetIndex(&mtp, collateralAsset, custodyAsset)
cutodyAmtTokenIn := sdk.NewCoin(mtp.Custodies[custodyIndex].Denom, mtp.Custodies[custodyIndex].Amount)
repayAmount, err := k.EstimateSwap(ctx, cutodyAmtTokenIn, mtp.Collaterals[collateralIndex].Denom, ammPool)
if err != nil {
Expand Down
30 changes: 0 additions & 30 deletions x/margin/keeper/get_margin_pool_balance.go

This file was deleted.

8 changes: 0 additions & 8 deletions x/margin/keeper/get_trading_asset.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/margin/keeper/handle_borrow_interest_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (k Keeper) HandleBorrowInterestPayment(ctx sdk.Context, collateralAsset str
return finalBorrowInterestPayment
}
} else { // else update unpaid mtp interest
collateralIndex, _ := k.GetMTPAssetIndex(mtp, collateralAsset, "")
collateralIndex, _ := types.GetMTPAssetIndex(mtp, collateralAsset, "")
if collateralIndex < 0 {
return sdk.ZeroInt()
}
Expand Down
3 changes: 2 additions & 1 deletion x/margin/keeper/invariant_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/margin/types"
)

func (k Keeper) AmmPoolBalanceCheck(ctx sdk.Context, poolId uint64) error {
Expand Down Expand Up @@ -41,7 +42,7 @@ func (k Keeper) AmmPoolBalanceCheck(ctx sdk.Context, poolId uint64) error {
// AMM pool balance differs bank module balance
balances := k.bankKeeper.GetAllBalances(ctx, address)
for _, balance := range balances {
ammBalance, _ := k.GetAmmPoolBalance(ctx, ammPool, balance.Denom)
ammBalance, _ := types.GetAmmPoolBalance(ammPool, balance.Denom)
collateralAmt := mtpCollateralBalances.AmountOf(balance.Denom)

diff := ammBalance.Add(collateralAmt).Sub(balance.Amount)
Expand Down
8 changes: 4 additions & 4 deletions x/margin/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (k Keeper) Borrow(ctx sdk.Context, collateralAsset string, custodyAsset str
liabilitiesDec = sdk.NewDecFromInt(liabilityAmt)
}

collateralIndex, custodyIndex := k.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset)
collateralIndex, custodyIndex := types.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset)
if collateralIndex < 0 || custodyIndex < 0 {
return sdkerrors.Wrap(types.ErrBalanceNotAvailable, "MTP collateral or custody invalid!")
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (k Keeper) CalculatePoolHealthByPosition(ctx sdk.Context, pool *types.Pool,
poolAssets := pool.GetPoolAssets(position)
H := sdk.NewDec(1)
for _, asset := range *poolAssets {
ammBalance, err := k.GetAmmPoolBalance(ctx, ammPool, asset.AssetDenom)
ammBalance, err := types.GetAmmPoolBalance(ammPool, asset.AssetDenom)
if err != nil {
return sdk.ZeroDec()
}
Expand Down Expand Up @@ -280,7 +280,7 @@ func (k Keeper) TakeInCustody(ctx sdk.Context, mtp types.MTP, pool *types.Pool)
}

func (k Keeper) IncrementalBorrowInterestPayment(ctx sdk.Context, collateralAsset string, custodyAsset string, borrowInterestPayment sdk.Int, mtp *types.MTP, pool *types.Pool, ammPool ammtypes.Pool) (sdk.Int, error) {
collateralIndex, custodyIndex := k.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset)
collateralIndex, custodyIndex := types.GetMTPAssetIndex(mtp, collateralAsset, custodyAsset)

entry, found := k.apKeeper.GetEntry(ctx, ptypes.BaseCurrency)
if !found {
Expand Down Expand Up @@ -383,7 +383,7 @@ func (k Keeper) BorrowInterestRateComputationByPosition(ctx sdk.Context, pool ty
poolAssets := pool.GetPoolAssets(position)
targetBorrowInterestRate := sdk.OneDec()
for _, asset := range *poolAssets {
ammBalance, err := k.GetAmmPoolBalance(ctx, ammPool, asset.AssetDenom)
ammBalance, err := types.GetAmmPoolBalance(ammPool, asset.AssetDenom)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
Loading

0 comments on commit 547de88

Please sign in to comment.