Skip to content

Commit

Permalink
Mz/feat/bug fixing version migration (#61)
Browse files Browse the repository at this point in the history
* feat: sort liquidity according to the denom

* feat: fix apr calcuation

* feat: fix lint issue

* feat: fix APR test-case issue
  • Loading branch information
soring323 authored Feb 22, 2024
1 parent 642b492 commit 5bfe259
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 31 deletions.
5 changes: 3 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type AppKeepers struct {
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
GovKeeper *govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
Expand Down Expand Up @@ -349,7 +349,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
govConfig,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
appKeepers.GovKeeper = *govKeeper
appKeepers.GovKeeper = govKeeper

govRouter := govv1beta1.NewRouter()
govRouter.
Expand Down Expand Up @@ -429,6 +429,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
// initParamsKeeper init params keeper and its subspaces.
func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key, tkey storetypes.StoreKey) paramskeeper.Keeper {
paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey)

paramsKeeper.Subspace(authtypes.ModuleName)
paramsKeeper.Subspace(banktypes.ModuleName)
paramsKeeper.Subspace(stakingtypes.ModuleName)
Expand Down
2 changes: 1 addition & 1 deletion app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func appModules(
capability.NewAppModule(appCodec, *app.CapabilityKeeper, false),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName)),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
Expand Down
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ genesis:
min_deposit:
- amount: "10000000"
denom: uside
params:
min_deposit:
- amount: "10000000"
denom: uside
voting_period: "60s"
mint:
params:
mint_denom: uside
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cosmossdk.io/api v0.3.1
github.com/cometbft/cometbft v0.37.4
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-sdk v0.47.8
github.com/cosmos/cosmos-sdk v0.47.9
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.3.0
github.com/golang/protobuf v1.5.3
Expand All @@ -27,14 +27,13 @@ require (
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/log v1.3.0 // indirect
cosmossdk.io/log v1.3.1 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/CosmWasm/wasmvm v1.2.4 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
Expand Down Expand Up @@ -70,7 +69,7 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.23.0 // indirect
Expand Down Expand Up @@ -144,7 +143,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand Down Expand Up @@ -186,8 +185,9 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.2.0
github.com/CosmWasm/wasmd v0.40.1
github.com/CosmWasm/wasmvm v1.2.4
github.com/Stride-Labs/stride/v16 v16.0.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-proto v1.0.0-beta.4
github.com/cosmos/ics23/go v0.10.0
github.com/prometheus/client_golang v1.15.0
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ cosmossdk.io/depinject v1.0.0-alpha.4 h1:PLNp8ZYAMPTUKyG9IK2hsbciDWqna2z1Wsl98ok
cosmossdk.io/depinject v1.0.0-alpha.4/go.mod h1:HeDk7IkR5ckZ3lMGs/o91AVUc7E596vMaOmslGFM3yU=
cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0=
cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U=
cosmossdk.io/log v1.3.0 h1:L0Z0XstClo2kOU4h3V1iDoE5Ji64sg5HLOogzGg67Oo=
cosmossdk.io/log v1.3.0/go.mod h1:HIDyvWLqZe2ovlWabsDN4aPMpY/nUEquAhgfTf2ZzB8=
cosmossdk.io/log v1.3.1 h1:UZx8nWIkfbbNEWusZqzAx3ZGvu54TZacWib3EzUYmGI=
cosmossdk.io/log v1.3.1/go.mod h1:2/dIomt8mKdk6vl3OWJcPk2be3pGOS8OQaLUM/3/tCM=
cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig=
cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/tools/rosetta v0.2.1 h1:ddOMatOH+pbxWbrGJKRAawdBkPYLfKXutK9IETnjYxw=
Expand Down Expand Up @@ -333,10 +333,10 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosmos/btcutil v1.0.5 h1:t+ZFcX77LpKtDBhjucvnOH8C2l2ioGsBNEQ3jef8xFk=
github.com/cosmos/btcutil v1.0.5/go.mod h1:IyB7iuqZMJlthe2tkIFL33xPyzbFYP0XVdS8P5lUPis=
github.com/cosmos/cosmos-proto v1.0.0-beta.2 h1:X3OKvWgK9Gsejo0F1qs5l8Qn6xJV/AzgIWR2wZ8Nua8=
github.com/cosmos/cosmos-proto v1.0.0-beta.2/go.mod h1:+XRCLJ14pr5HFEHIUcn51IKXD1Fy3rkEQqt4WqmN4V0=
github.com/cosmos/cosmos-sdk v0.47.8 h1:kzYF2xhnfi8dy15t2VVS24tc2KcuU4JBgjh9yCFx4y4=
github.com/cosmos/cosmos-sdk v0.47.8/go.mod h1:VTAtthIsmfplanhFfUTfT6ED4F+kkJxT7nmvmKXRthI=
github.com/cosmos/cosmos-proto v1.0.0-beta.4 h1:aEL7tU/rLOmxZQ9z4i7mzxcLbSCY48OdY7lIWTLG7oU=
github.com/cosmos/cosmos-proto v1.0.0-beta.4/go.mod h1:oeB+FyVzG3XrQJbJng0EnV8Vljfk9XvTIpGILNU/9Co=
github.com/cosmos/cosmos-sdk v0.47.9 h1:D51VLkF59D53PMLsbNtp6JyWR+6MbetFyomrH88+y08=
github.com/cosmos/cosmos-sdk v0.47.9/go.mod h1:cmAawe8FV/52oPKbgeHLt4UpNkrNu8R5KD+kw0kxJFc=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down Expand Up @@ -389,8 +389,8 @@ github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:Htrtb
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM=
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down Expand Up @@ -885,8 +885,8 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE=
github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
5 changes: 4 additions & 1 deletion testutil/keeper/gmm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"testing"
"time"

tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -45,7 +46,9 @@ func GmmKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
ctx := sdk.NewContext(stateStore, tmproto.Header{
Time: time.Now().UTC(),
}, false, log.NewNopLogger())

// Initialize params
k.SetParams(ctx, types.DefaultParams())
Expand Down
2 changes: 1 addition & 1 deletion x/gmm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var gmmModuleAddress string
func (suite *KeeperTestSuite) SetupTest() {
// app := simapp.InitSideTestApp(initChain)
app := simapp.Setup(suite.T())
ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now().UTC()})

app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
app.BankKeeper.SetParams(ctx, banktypes.DefaultParams())
Expand Down
8 changes: 4 additions & 4 deletions x/gmm/keeper/pool_apr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ func TestAPRCalculation(t *testing.T) {
keeper.AppendPool(ctx, pool)
// Loop to simulate 1000 observations at different timestamps
for i := 0; i < 365; i++ {
// Increment the block time for each observation
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(24 * time.Hour))
// Perform the observation
err := keeper.ObserveFeeFromPool(ctx, pool.PoolId, tokenIn)
// Increment the block time for each observation
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(24 * time.Hour))
require.NoError(t, err)
}

// Calculate the APR for the pool
apr := keeper.GetAPR(ctx, pool.PoolId)
expectedAPR := sdk.NewCoin("usdt", sdkmath.NewInt(185344))
expectedAPR := sdk.NewCoin("usdt", sdkmath.NewInt(6610000))
// Assert APR calculation
require.Equal(t, expectedAPR, apr[0], "Calculated APR does not match expected APR")
require.Equal(t, expectedAPR.Amount.LTE(apr[0].Amount), true, "Calculated APR does not match expected APR")
}
2 changes: 1 addition & 1 deletion x/gmm/types/message_add_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewMsgAddLiquidity(
return &MsgAddLiquidity{
Sender: sender,
PoolId: poolID,
Liquidity: liquidity,
Liquidity: liquidity.Sort(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/gmm/types/message_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (msg *MsgCreatePool) InitialLiquidity() sdk.Coins {
for _, asset := range msg.Liquidity {
liquidity = liquidity.Add(asset.Token)
}
return liquidity
return liquidity.Sort()
}

// Return denom list of liquidity
Expand Down
12 changes: 8 additions & 4 deletions x/gmm/types/pool_apr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ func NewPoolAPR(ctx sdk.Context) *PoolAPR {

// Please multiply market price of every assets after getting to display as USD.
func (pa *PoolAPR) CalcAPR(ctx sdk.Context, tvl map[string]PoolAsset) sdk.Coins {
oneYearAsSec := 60 * 60 * 24 * 365
oneYearAsSec := int64(60 * 60 * 24 * 365)
var apr sdk.Coins
for _, coin := range pa.Fees {
interval := (pa.CreatedAt - ctx.BlockTime().Unix())
avg := coin.Amount.Mul(sdkmath.NewInt(int64(oneYearAsSec))).Mul(sdk.NewInt(1e10)).Quo(sdkmath.NewInt(interval))
if _, found := tvl[coin.Denom]; found {
interval := ctx.BlockTime().Unix() - pa.CreatedAt
if _, found := tvl[coin.Denom]; found && !tvl[coin.Denom].Token.Amount.IsZero() {
if interval <= 0 {
apr = apr.Add(sdk.NewCoin(coin.Denom, sdk.NewInt(0)))
continue
}
avg := coin.Amount.Mul(sdkmath.NewInt(oneYearAsSec)).Mul(sdk.NewInt(1e10)).Quo(sdkmath.NewInt(interval))
avg = avg.Quo(tvl[coin.Denom].Token.Amount)
apr = apr.Add(sdk.NewCoin(coin.Denom, avg))
}
Expand Down

0 comments on commit 5bfe259

Please sign in to comment.