Skip to content

Commit

Permalink
Updating accounted pool structure (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
avkr003 authored Nov 28, 2024
1 parent 659ef1a commit 4f262f7
Show file tree
Hide file tree
Showing 45 changed files with 1,416 additions and 384 deletions.
1,286 changes: 1,022 additions & 264 deletions api/elys/accountedpool/accounted_pool.pulsar.go

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion proto/elys/accountedpool/accounted_pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos_proto/cosmos.proto";

message AccountedPool {
message LegacyAccountedPool {
uint64 pool_id = 1;
cosmos.base.v1beta1.Coin total_shares = 2 [ (gogoproto.nullable) = false ];
repeated elys.amm.PoolAsset pool_assets = 3 [ (gogoproto.nullable) = false ];
Expand All @@ -19,3 +19,11 @@ message AccountedPool {
repeated cosmos.base.v1beta1.Coin non_amm_pool_tokens = 5
[ (gogoproto.nullable) = false ];
}

message AccountedPool {
uint64 pool_id = 1;
repeated cosmos.base.v1beta1.Coin total_tokens = 2
[ (gogoproto.nullable) = false ];
repeated cosmos.base.v1beta1.Coin non_amm_pool_tokens = 3
[ (gogoproto.nullable) = false ];
}
14 changes: 0 additions & 14 deletions x/accountedpool/client/cli/query_accounted_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ func TestShowAccountedPool(t *testing.T) {
var resp types.QueryGetAccountedPoolResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.AccountedPool)
// total weight is not set in genesis state
tc.obj.TotalWeight = resp.AccountedPool.TotalWeight
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.AccountedPool),
Expand Down Expand Up @@ -167,10 +165,6 @@ func TestListAccountedPool(t *testing.T) {
require.NoError(t, err)
require.LessOrEqual(t, len(resp.AccountedPool), stepSize)

for j, accountedPool := range resp.AccountedPool {
objs[i+j].TotalWeight = accountedPool.TotalWeight
}

require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.AccountedPool))
}
})
Expand All @@ -182,10 +176,6 @@ func TestListAccountedPool(t *testing.T) {
require.NoError(t, err)
require.LessOrEqual(t, len(resp.AccountedPool), stepSize)

for j, accountedPool := range resp.AccountedPool {
objs[i+j].TotalWeight = accountedPool.TotalWeight
}

require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.AccountedPool))
next = resp.Pagination.NextKey
}
Expand All @@ -196,10 +186,6 @@ func TestListAccountedPool(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(objs), int(resp.Pagination.Total))

for i, accountedPool := range resp.AccountedPool {
objs[i].TotalWeight = accountedPool.TotalWeight
}

require.ElementsMatch(t, nullify.Fill(objs), nullify.Fill(resp.AccountedPool))
})
}
16 changes: 16 additions & 0 deletions x/accountedpool/keeper/accounted_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ func (k Keeper) GetAllAccountedPool(ctx sdk.Context) (list []types.AccountedPool
return
}

// GetAllAccountedPool returns all accountedPool
func (k Keeper) GetAllLegacyAccountedPool(ctx sdk.Context) (list []types.LegacyAccountedPool) {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.KeyPrefix(types.AccountedPoolKeyPrefix))
iterator := storetypes.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.LegacyAccountedPool
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}

// PoolExists checks if a pool with the given poolId exists in the list of pools
func (k Keeper) PoolExists(ctx sdk.Context, poolId uint64) bool {
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.KeyPrefix(types.AccountedPoolKeyPrefix))
Expand Down
5 changes: 0 additions & 5 deletions x/accountedpool/keeper/accounted_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@ package keeper_test
import (
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/elys-network/elys/testutil/keeper"
"github.com/elys-network/elys/testutil/nullify"
"github.com/elys-network/elys/x/accountedpool/keeper"
"github.com/elys-network/elys/x/accountedpool/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/stretchr/testify/require"
)

func createNAccountedPool(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.AccountedPool {
items := make([]types.AccountedPool, n)
for i := range items {
items[i].PoolId = (uint64)(i)
items[i].TotalShares = sdk.NewCoin("lpshare", sdkmath.ZeroInt())
items[i].PoolAssets = []ammtypes.PoolAsset{}
items[i].TotalWeight = sdkmath.ZeroInt()

keeper.SetAccountedPool(ctx, items[i])
}
Expand Down
8 changes: 4 additions & 4 deletions x/accountedpool/keeper/accounted_pool_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func TestAccountedPoolUpdate(t *testing.T) {
// Initiate pool
accountedPool := types.AccountedPool{
PoolId: 0,
TotalShares: ammPool.TotalShares,
PoolAssets: []ammtypes.PoolAsset{},
TotalWeight: ammPool.TotalWeight,
TotalTokens: []sdk.Coin{},
NonAmmPoolTokens: []sdk.Coin{},
}

accountedPool.PoolAssets = append(accountedPool.PoolAssets, ammPool.PoolAssets...)
for _, asset := range ammPool.PoolAssets {
accountedPool.TotalTokens = append(accountedPool.TotalTokens, asset.Token)
}

// Set accounted pool
apk.SetAccountedPool(ctx, accountedPool)
Expand Down
9 changes: 3 additions & 6 deletions x/accountedpool/keeper/hooks_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ func (k Keeper) UpdateAccountedPoolOnAmmChange(ctx sdk.Context, ammPool ammtypes
}
}

for j, accountedPoolAsset := range accountedPool.PoolAssets {
for j, accountedPoolAsset := range accountedPool.TotalTokens {

if ammPoolAsset.Token.Denom == accountedPoolAsset.Token.Denom {
if ammPoolAsset.Token.Denom == accountedPoolAsset.Denom {
updatedAccountedPoolAsset := ammPoolAsset
updatedAccountedPoolAsset.Token.Amount = updatedAccountedPoolAsset.Token.Amount.Add(nonAmmTokenBalance)
// TODO what to do with the weight
//updatedAccountedPoolAsset.Weight =

accountedPool.PoolAssets[j] = updatedAccountedPoolAsset
accountedPool.TotalTokens[j] = updatedAccountedPoolAsset.Token
break
}

Expand Down
6 changes: 2 additions & 4 deletions x/accountedpool/keeper/hooks_leveragelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ func (k Keeper) OnLeverageLpPoolEnable(ctx sdk.Context, ammPool ammtypes.Pool) e
// Initiate pool
accountedPool := types.AccountedPool{
PoolId: poolId,
TotalShares: ammPool.TotalShares,
PoolAssets: []ammtypes.PoolAsset{},
TotalWeight: ammPool.TotalWeight,
TotalTokens: sdk.NewCoins(),
NonAmmPoolTokens: sdk.NewCoins(),
}

nonAmmPoolTokens := make([]sdk.Coin, len(ammPool.PoolAssets))

for i, poolAsset := range ammPool.PoolAssets {
accountedPool.PoolAssets = append(accountedPool.PoolAssets, poolAsset)
accountedPool.TotalTokens = append(accountedPool.TotalTokens, poolAsset.Token)
nonAmmPoolTokens[i] = sdk.NewCoin(poolAsset.Token.Denom, math.ZeroInt())
}
accountedPool.NonAmmPoolTokens = nonAmmPoolTokens
Expand Down
10 changes: 5 additions & 5 deletions x/accountedpool/keeper/hooks_perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ func (k Keeper) PerpetualUpdates(ctx sdk.Context, ammPool ammtypes.Pool, perpetu

// Accounted pool balance = amm pool + (long liability - long profit taking liability) - (long custody - long profit taking custody) + (short liability - short profit taking liability ) - ( short custody - short profit taking custody)
// Accounted pool balance = amm pool + totalLiabilities - totalCustody + total profit taking custody - total profit taking liability
for i, asset := range accountedPool.PoolAssets {
ammBalance, err := ammPool.GetAmmPoolBalance(asset.Token.Denom)
for i, asset := range accountedPool.TotalTokens {
ammBalance, err := ammPool.GetAmmPoolBalance(asset.Denom)
if err != nil {
return err
}
totalLiabilities, totalCustody, totalTakeProfitCustody, totalTakeProfitLiabilities := perpetualPool.GetPerpetualPoolBalances(asset.Token.Denom)
totalLiabilities, totalCustody, totalTakeProfitCustody, totalTakeProfitLiabilities := perpetualPool.GetPerpetualPoolBalances(asset.Denom)
accountedPoolAmt := ammBalance.Add(totalLiabilities).Sub(totalCustody)
// if this is enabled then we need to consider impact of weight balance bonus of swap while calculating takeProfitLiabilities
if EnableTakeProfitCustodyLiabilities {
accountedPoolAmt = accountedPoolAmt.Add(totalTakeProfitCustody).Sub(totalTakeProfitLiabilities)
}
accountedPool.PoolAssets[i].Token = sdk.NewCoin(asset.Token.Denom, accountedPoolAmt)
accountedPool.TotalTokens[i] = sdk.NewCoin(asset.Denom, accountedPoolAmt)

for j, nonAmmToken := range accountedPool.NonAmmPoolTokens {
if nonAmmToken.Denom == asset.Token.Denom {
if nonAmmToken.Denom == asset.Denom {
accountedPool.NonAmmPoolTokens[j].Amount = accountedPoolAmt.Sub(ammBalance)
break
}
Expand Down
6 changes: 3 additions & 3 deletions x/accountedpool/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (k Keeper) GetAccountedBalance(ctx sdk.Context, poolId uint64, denom string
return sdkmath.ZeroInt()
}

for _, asset := range pool.PoolAssets {
if asset.Token.Denom == denom {
return asset.Token.Amount
for _, token := range pool.TotalTokens {
if token.Denom == denom {
return token.Amount
}
}

Expand Down
26 changes: 26 additions & 0 deletions x/accountedpool/migrations/v3_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/accountedpool/types"
)

func (m Migrator) V3Migration(ctx sdk.Context) error {
allLegacyAccountedPool := m.keeper.GetAllLegacyAccountedPool(ctx)

for _, legacyAccountedPool := range allLegacyAccountedPool {
accountedPool := types.AccountedPool{
PoolId: legacyAccountedPool.PoolId,
TotalTokens: []sdk.Coin{},
NonAmmPoolTokens: legacyAccountedPool.NonAmmPoolTokens,
}

for _, asset := range legacyAccountedPool.PoolAssets {
accountedPool.TotalTokens = append(accountedPool.TotalTokens, asset.Token)
}

m.keeper.SetAccountedPool(ctx, accountedPool)

}
return nil
}
4 changes: 2 additions & 2 deletions x/accountedpool/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 1, m.V2Migration)
err := cfg.RegisterMigration(types.ModuleName, 2, m.V3Migration)
if err != nil {
panic(err)
}
Expand All @@ -155,7 +155,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ context.Context) error {
Expand Down
Loading

0 comments on commit 4f262f7

Please sign in to comment.