Skip to content

Commit

Permalink
remove all cross dependency in accountedpool keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Sep 5, 2023
1 parent fa3f3df commit 2439e94
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 80 deletions.
22 changes: 10 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,15 @@ func NewElysApp(
app.AssetprofileKeeper,
)

app.AccountedPoolKeeper = *accountedpoolmodulekeeper.NewKeeper(
appCodec,
keys[accountedpoolmoduletypes.StoreKey],
keys[accountedpoolmoduletypes.MemStoreKey],
app.GetSubspace(accountedpoolmoduletypes.ModuleName),
app.BankKeeper,
)
accountedPoolModule := accountedpoolmodule.NewAppModule(appCodec, app.AccountedPoolKeeper, app.AccountKeeper, app.BankKeeper)

app.AmmKeeper = *ammmodulekeeper.NewKeeper(
appCodec,
keys[ammmoduletypes.StoreKey],
Expand All @@ -700,7 +709,6 @@ func NewElysApp(
app.BankKeeper,
app.AmmKeeper,
app.OracleKeeper,
app.AccountedPoolKeeper,
authtypes.FeeCollectorName,
DexRevenueCollectorName,
)
Expand Down Expand Up @@ -826,16 +834,6 @@ func NewElysApp(
)
marginModule := marginmodule.NewAppModule(appCodec, app.MarginKeeper, app.AccountKeeper, app.BankKeeper)

app.AccountedPoolKeeper = *accountedpoolmodulekeeper.NewKeeper(
appCodec,
keys[accountedpoolmoduletypes.StoreKey],
keys[accountedpoolmoduletypes.MemStoreKey],
app.GetSubspace(accountedpoolmoduletypes.ModuleName),
app.MarginKeeper,
app.BankKeeper,
)
accountedPoolModule := accountedpoolmodule.NewAppModule(appCodec, app.AccountedPoolKeeper, app.AccountKeeper, app.BankKeeper)

// this line is used by starport scaffolding # stargate/app/keeperDefinition

/**** IBC Routing ****/
Expand Down Expand Up @@ -879,7 +877,7 @@ func NewElysApp(
ammmoduletypes.NewMultiAmmHooks(
// insert amm hooks receivers here
app.IncentiveKeeper.AmmHooks(),
app.AccountedPoolKeeper.AmmHooks(),
app.MarginKeeper.AmmHooks(),
),
)

Expand Down
1 change: 0 additions & 1 deletion testutil/keeper/accountedpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func AccountedPoolKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
paramsSubspace,
nil,
nil,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
1 change: 0 additions & 1 deletion testutil/keeper/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func IncentiveKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
nil,
"",
"",
)
Expand Down
17 changes: 0 additions & 17 deletions x/accountedpool/keeper/accounted_pool_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ func (k Keeper) GetMarginPoolBalances(marginPool margintypes.Pool, denom string)
return sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
}

// Update accounted pool balance
func (k Keeper) UpdateAccountedPoolByAMM(ctx sdk.Context, ammPool ammtypes.Pool) error {
poolId := ammPool.PoolId
// Get margin pool
marginPool, found := k.margin.GetPool(ctx, poolId)
if !found {
return errors.New("pool doesn't exist!")
}

return k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

// Update accounted pool balance
func (k Keeper) UpdateAccountedPoolByMargin(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) error {
return k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

func (k Keeper) UpdateAccountedPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) error {
poolId := ammPool.PoolId
// Check if already exists
Expand Down
80 changes: 80 additions & 0 deletions x/accountedpool/keeper/accounted_pool_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package keeper_test

import (
"testing"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
simapp "github.com/elys-network/elys/app"
ammtypes "github.com/elys-network/elys/x/amm/types"

"github.com/elys-network/elys/x/accountedpool/types"
margintypes "github.com/elys-network/elys/x/margin/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

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

apk := app.AccountedPoolKeeper

// Generate 1 random account with 1000stake balanced
addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000))

// Initiate pool
ammPool := ammtypes.Pool{
PoolId: 0,
Address: addr[0].String(),
PoolParams: ammtypes.PoolParams{},
TotalShares: sdk.NewCoin("lp-token", sdk.NewInt(100)),
PoolAssets: []ammtypes.PoolAsset{
{Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100))},
{Token: sdk.NewCoin(ptypes.USDC, sdk.NewInt(1000))},
},
TotalWeight: sdk.NewInt(100),
RebalanceTreasury: addr[0].String(),
}
// Initiate pool
accountedPool := types.AccountedPool{
PoolId: 0,
TotalShares: ammPool.TotalShares,
PoolAssets: []ammtypes.PoolAsset{},
TotalWeight: ammPool.TotalWeight,
}

for _, asset := range ammPool.PoolAssets {
accountedPool.PoolAssets = append(accountedPool.PoolAssets, asset)
}
// Set accounted pool
apk.SetAccountedPool(ctx, accountedPool)

marginPool := margintypes.Pool{
AmmPoolId: 0,
Health: sdk.NewDec(1),
Enabled: true,
Closed: false,
InterestRate: sdk.NewDec(1),
PoolAssets: []margintypes.PoolAsset{
{
Liabilities: sdk.NewInt(400),
Custody: sdk.NewInt(0),
AssetBalance: sdk.NewInt(100),
UnsettledLiabilities: sdk.NewInt(0),
BlockInterest: sdk.NewInt(0),
AssetDenom: ptypes.USDC,
},
{
Liabilities: sdk.NewInt(50),
Custody: sdk.NewInt(0),
AssetBalance: sdk.NewInt(0),
UnsettledLiabilities: sdk.NewInt(0),
BlockInterest: sdk.NewInt(0),
AssetDenom: ptypes.ATOM,
},
},
}
// Update accounted pool
apk.UpdateAccountedPool(ctx, ammPool, marginPool)

}
42 changes: 39 additions & 3 deletions x/accountedpool/keeper/hooks_margin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,35 @@ import (
)

func (k Keeper) AfterMarginPositionOpended(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPoolByMargin(ctx, ammPool, marginPool)
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

func (k Keeper) AfterMarginPositionModified(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPoolByMargin(ctx, ammPool, marginPool)
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

func (k Keeper) AfterMarginPositionClosed(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPoolByMargin(ctx, ammPool, marginPool)
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

// AfterPoolCreated is called after CreatePool
func (k Keeper) AfterAmmPoolCreated(ctx sdk.Context, ammPool ammtypes.Pool) {
k.InitiateAccountedPool(ctx, ammPool)
}

// AfterJoinPool is called after JoinPool, JoinSwapExternAmountIn, and JoinSwapShareAmountOut
func (k Keeper) AfterAmmJoinPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

// AfterExitPool is called after ExitPool, ExitSwapShareAmountIn, and ExitSwapExternAmountOut
func (k Keeper) AfterAmmExitPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

// AfterSwap is called after SwapExactAmountIn and SwapExactAmountOut
func (k Keeper) AfterAmmSwap(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
k.UpdateAccountedPool(ctx, ammPool, marginPool)
}

// Hooks wrapper struct for tvl keeper
Expand All @@ -41,3 +61,19 @@ func (h MarginHooks) AfterMarginPositionModified(ctx sdk.Context, ammPool ammtyp
func (h MarginHooks) AfterMarginPositionClosed(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterMarginPositionClosed(ctx, ammPool, marginPool)
}

func (h MarginHooks) AfterAmmPoolCreated(ctx sdk.Context, ammPool ammtypes.Pool) {
h.k.AfterAmmPoolCreated(ctx, ammPool)
}

func (h MarginHooks) AfterAmmJoinPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterAmmJoinPool(ctx, ammPool, marginPool)
}

func (h MarginHooks) AfterAmmExitPool(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterAmmExitPool(ctx, ammPool, marginPool)
}

func (h MarginHooks) AfterAmmSwap(ctx sdk.Context, ammPool ammtypes.Pool, marginPool margintypes.Pool) {
h.k.AfterAmmSwap(ctx, ammPool, marginPool)
}
3 changes: 0 additions & 3 deletions x/accountedpool/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type (
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
margin types.MarginKeeper
bankKeeper types.BankKeeper
}
)
Expand All @@ -28,7 +27,6 @@ func NewKeeper(
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
margin types.MarginKeeper,
bk types.BankKeeper,
) *Keeper {
// set KeyTable if it has not already been set
Expand All @@ -41,7 +39,6 @@ func NewKeeper(
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
margin: margin,
bankKeeper: bk,
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/initialize_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// - Records total liquidity increase
// - Calls the AfterPoolCreated hook
func (k Keeper) InitializePool(ctx sdk.Context, pool *types.Pool, sender sdk.AccAddress) (err error) {
tvl, err := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
tvl, err := pool.TVL(ctx, k.oracleKeeper)
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions x/amm/keeper/swap_in_amt_given_out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper

import (
"fmt"

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

// SwapInAmtGivenOut is a mutative method for CalcOutAmtGivenIn, which includes the actual swap.
func (k Keeper) SwapInAmtGivenOut(
ctx sdk.Context, poolId uint64, oracleKeeper types.OracleKeeper, snapshot *types.Pool,
tokensOut sdk.Coins, tokenInDenom string, swapFee sdk.Dec) (
tokenIn sdk.Coin, weightBalanceBonus sdk.Dec, err error,
) {
ammPool, found := k.GetPool(ctx, poolId)
if !found {
return sdk.Coin{}, sdk.ZeroDec(), fmt.Errorf("invalid pool: %d", poolId)
}

return ammPool.SwapInAmtGivenOut(ctx, oracleKeeper, snapshot, tokensOut, tokenInDenom, swapFee, k.accountedPoolKeeper)
}
25 changes: 25 additions & 0 deletions x/amm/keeper/swap_out_amt_given_in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
fmt "fmt"

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

// SwapOutAmtGivenIn is a mutative method for CalcOutAmtGivenIn, which includes the actual swap.
func (k Keeper) SwapOutAmtGivenIn(
ctx sdk.Context, poolId uint64,
oracleKeeper types.OracleKeeper,
snapshot *types.Pool,
tokensIn sdk.Coins,
tokenOutDenom string,
swapFee sdk.Dec,
) (tokenOut sdk.Coin, weightBalanceBonus sdk.Dec, err error) {
ammPool, found := k.GetPool(ctx, poolId)
if !found {
return sdk.Coin{}, sdk.ZeroDec(), fmt.Errorf("invalid pool: %d", poolId)
}

return ammPool.SwapOutAmtGivenIn(ctx, oracleKeeper, snapshot, tokensIn, tokenOutDenom, swapFee, k.accountedPoolKeeper)
}
2 changes: 1 addition & 1 deletion x/amm/types/calc_exit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func CalcExitValueWithoutSlippage(ctx sdk.Context, oracleKeeper OracleKeeper, accPoolKeeper AccountedPoolKeeper, pool Pool, exitingShares sdk.Int, tokenOutDenom string) (sdk.Dec, error) {
tvl, err := pool.TVL(ctx, oracleKeeper, accPoolKeeper)
tvl, err := pool.TVL(ctx, oracleKeeper)
if err != nil {
return sdk.ZeroDec(), err
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/pool_join_pool_no_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (p *Pool) JoinPoolNoSwap(ctx sdk.Context, oracleKeeper OracleKeeper, accoun
}

initialWeightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, p.PoolAssets)
tvl, err := p.TVL(ctx, oracleKeeper, accountedPoolKeeper)
tvl, err := p.TVL(ctx, oracleKeeper)
if err != nil {
return sdk.ZeroInt(), err
}
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/tvl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper, accountedPoolKeeepr AccountedPoolKeeper) (sdk.Dec, error) {
func (p *Pool) TVL(ctx sdk.Context, oracleKeeper OracleKeeper) (sdk.Dec, error) {
// OracleAssetsTVL * TotalWeight / OracleAssetsWeight
// E.g. JUNO / USDT / USDC (30:30:30)
// TVL = USDC_USDT_liquidity * 90 / 60
Expand Down
2 changes: 1 addition & 1 deletion x/amm/types/tvl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (suite *TestSuite) TestTVL() {
PoolAssets: tc.poolAssets,
TotalWeight: sdk.ZeroInt(),
}
tvl, err := pool.TVL(suite.ctx, suite.app.OracleKeeper, suite.app.AccountedPoolKeeper)
tvl, err := pool.TVL(suite.ctx, suite.app.OracleKeeper)
if tc.expError {
suite.Require().Error(err)
} else {
Expand Down
Loading

0 comments on commit 2439e94

Please sign in to comment.