Skip to content

Commit

Permalink
refactor: Test suites refactor #1297
Browse files Browse the repository at this point in the history
  • Loading branch information
catShaark authored Apr 20, 2022
1 parent 0cfa729 commit 8914eee
Show file tree
Hide file tree
Showing 39 changed files with 1,076 additions and 1,385 deletions.
64 changes: 56 additions & 8 deletions app/apptesting/gamm.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package apptesting

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer"
gammtypes "github.com/osmosis-labs/osmosis/v7/x/gamm/types"
)

var gammPoolMakerAcc = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address().Bytes())
var DefaultAcctFunds sdk.Coins = sdk.NewCoins(
sdk.NewCoin("uosmo", sdk.NewInt(10000000000)),
sdk.NewCoin("foo", sdk.NewInt(10000000)),
sdk.NewCoin("bar", sdk.NewInt(10000000)),
sdk.NewCoin("baz", sdk.NewInt(10000000)),
)

// Returns a Univ2 pool with the initial liquidity being the provided balances
func (suite *KeeperTestHelper) PrepareUni2PoolWithAssets(asset1, asset2 sdk.Coin) uint64 {
return suite.preparePool(
return suite.PrepareBalancerPoolWithPoolAsset(
[]balancer.PoolAsset{
{
Weight: sdk.NewInt(1),
Expand All @@ -26,18 +30,62 @@ func (suite *KeeperTestHelper) PrepareUni2PoolWithAssets(asset1, asset2 sdk.Coin
)
}

func (suite *KeeperTestHelper) preparePool(assets []balancer.PoolAsset) uint64 {
func (suite *KeeperTestHelper) PrepareBalancerPool() uint64 {
poolId := suite.PrepareBalancerPoolWithPoolParams(balancer.PoolParams{
SwapFee: sdk.NewDec(0),
ExitFee: sdk.NewDec(0),
})

spotPrice, err := suite.App.GAMMKeeper.CalculateSpotPrice(suite.Ctx, poolId, "foo", "bar")
suite.NoError(err)
suite.Equal(sdk.NewDec(2).String(), spotPrice.String())
spotPrice, err = suite.App.GAMMKeeper.CalculateSpotPrice(suite.Ctx, poolId, "bar", "baz")
suite.NoError(err)
suite.Equal(sdk.NewDecWithPrec(15, 1).String(), spotPrice.String())
spotPrice, err = suite.App.GAMMKeeper.CalculateSpotPrice(suite.Ctx, poolId, "baz", "foo")
suite.NoError(err)
s := sdk.NewDec(1).Quo(sdk.NewDec(3))
sp := s.Mul(gammtypes.SigFigs).RoundInt().ToDec().Quo(gammtypes.SigFigs)
suite.Equal(sp.String(), spotPrice.String())

return poolId
}

func (suite *KeeperTestHelper) PrepareBalancerPoolWithPoolParams(poolParams balancer.PoolParams) uint64 {
// Mint some assets to the account.
suite.FundAcc(suite.TestAccs[0], DefaultAcctFunds)

poolAssets := []balancer.PoolAsset{
{
Weight: sdk.NewInt(100),
Token: sdk.NewCoin("foo", sdk.NewInt(5000000)),
},
{
Weight: sdk.NewInt(200),
Token: sdk.NewCoin("bar", sdk.NewInt(5000000)),
},
{
Weight: sdk.NewInt(300),
Token: sdk.NewCoin("baz", sdk.NewInt(5000000)),
},
}
msg := balancer.NewMsgCreateBalancerPool(suite.TestAccs[0], poolParams, poolAssets, "")
poolId, err := suite.App.GAMMKeeper.CreatePool(suite.Ctx, msg)
suite.NoError(err)
return poolId
}

func (suite *KeeperTestHelper) PrepareBalancerPoolWithPoolAsset(assets []balancer.PoolAsset) uint64 {
suite.Require().Len(assets, 2)

// Add coins for pool creation fee + coins needed to mint balances
fundCoins := sdk.Coins{sdk.NewCoin("uosmo", sdk.NewInt(10000000000))}
for _, a := range assets {
fundCoins = fundCoins.Add(a.Token)
}
err := simapp.FundAccount(suite.App.BankKeeper, suite.Ctx, gammPoolMakerAcc, fundCoins)
suite.Require().NoError(err)
suite.FundAcc(suite.TestAccs[0], fundCoins)

msg := balancer.NewMsgCreateBalancerPool(gammPoolMakerAcc, balancer.PoolParams{
msg := balancer.NewMsgCreateBalancerPool(suite.TestAccs[0], balancer.PoolParams{
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
}, assets, "")
Expand Down
50 changes: 37 additions & 13 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -26,8 +27,25 @@ import (
type KeeperTestHelper struct {
suite.Suite

App *app.OsmosisApp
Ctx sdk.Context
App *app.OsmosisApp
Ctx sdk.Context
QueryHelper *baseapp.QueryServiceTestHelper
TestAccs []sdk.AccAddress
}

func (keeperTestHelper *KeeperTestHelper) Setup() {
keeperTestHelper.App = app.Setup(false)
keeperTestHelper.Ctx = keeperTestHelper.App.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: time.Now().UTC()})
keeperTestHelper.QueryHelper = &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: keeperTestHelper.App.GRPCQueryRouter(),
Ctx: keeperTestHelper.Ctx,
}
keeperTestHelper.TestAccs = CreateRandomAccounts(3)
}

func (keeperTestHelper *KeeperTestHelper) FundAcc(acc sdk.AccAddress, amounts sdk.Coins) {
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc, amounts)
keeperTestHelper.Require().NoError(err)
}

func (keeperTestHelper *KeeperTestHelper) SetupValidator(bondStatus stakingtypes.BondStatus) sdk.ValAddress {
Expand All @@ -36,8 +54,7 @@ func (keeperTestHelper *KeeperTestHelper) SetupValidator(bondStatus stakingtypes
bondDenom := keeperTestHelper.App.StakingKeeper.GetParams(keeperTestHelper.Ctx).BondDenom
selfBond := sdk.NewCoins(sdk.Coin{Amount: sdk.NewInt(100), Denom: bondDenom})

err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, sdk.AccAddress(valAddr), selfBond)
keeperTestHelper.Require().NoError(err)
keeperTestHelper.FundAcc(sdk.AccAddress(valAddr), selfBond)

sh := teststaking.NewHelper(keeperTestHelper.Suite.T(), keeperTestHelper.Ctx, *keeperTestHelper.App.StakingKeeper)
msg := sh.CreateValidatorMsg(valAddr, valPub, selfBond[0].Amount)
Expand Down Expand Up @@ -138,8 +155,7 @@ func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(
acc1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes())

poolCreationFee := keeperTestHelper.App.GAMMKeeper.GetParams(keeperTestHelper.Ctx)
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, poolCreationFee.PoolCreationFee)
keeperTestHelper.Require().NoError(err)
keeperTestHelper.FundAcc(acc1, poolCreationFee.PoolCreationFee)

pools := []gammtypes.PoolI{}

Expand All @@ -148,11 +164,10 @@ func (keeperTestHelper *KeeperTestHelper) SetupGammPoolsWithBondDenomMultiplier(

uosmoAmount := gammtypes.InitPoolSharesSupply.ToDec().Mul(multiplier).RoundInt()

err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, sdk.NewCoins(
keeperTestHelper.FundAcc(acc1, sdk.NewCoins(
sdk.NewCoin(bondDenom, uosmoAmount.Mul(sdk.NewInt(10))),
sdk.NewInt64Coin(token, 100000),
))
keeperTestHelper.NoError(err)

var (
defaultFutureGovernor = ""
Expand Down Expand Up @@ -192,10 +207,9 @@ func (keeperTestHelper *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fro

// fund dummy account with tokens to swap
coins := sdk.Coins{sdk.NewInt64Coin(fromAsset.Denom, 100000000000000)}
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, acc1, coins)
keeperTestHelper.Require().NoError(err)
keeperTestHelper.FundAcc(acc1, coins)

_, err = keeperTestHelper.App.GAMMKeeper.SwapExactAmountOut(
_, err := keeperTestHelper.App.GAMMKeeper.SwapExactAmountOut(
keeperTestHelper.Ctx, acc1,
poolId, fromAsset.Denom, fromAsset.Amount,
sdk.NewCoin(toAsset.Denom, toAsset.Amount.Quo(sdk.NewInt(4))))
Expand All @@ -208,9 +222,19 @@ func (keeperTestHelper *KeeperTestHelper) SwapAndSetSpotPrice(poolId uint64, fro

func (keeperTestHelper *KeeperTestHelper) LockTokens(addr sdk.AccAddress, coins sdk.Coins, duration time.Duration) (lockID uint64) {
msgServer := lockupkeeper.NewMsgServerImpl(keeperTestHelper.App.LockupKeeper)
err := simapp.FundAccount(keeperTestHelper.App.BankKeeper, keeperTestHelper.Ctx, addr, coins)
keeperTestHelper.Require().NoError(err)
keeperTestHelper.FundAcc(addr, coins)
msgResponse, err := msgServer.LockTokens(sdk.WrapSDKContext(keeperTestHelper.Ctx), lockuptypes.NewMsgLockTokens(addr, duration, coins))
keeperTestHelper.Require().NoError(err)
return msgResponse.ID
}

// CreateRandomAccounts is a function return a list of randomly generated AccAddresses
func CreateRandomAccounts(numAccts int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, numAccts)
for i := 0; i < numAccts; i++ {
pk := ed25519.GenPrivKey().PubKey()
testAddrs[i] = sdk.AccAddress(pk.Address())
}

return testAddrs
}
Loading

0 comments on commit 8914eee

Please sign in to comment.