Skip to content

Commit

Permalink
add, update liquid stake and redeem stake tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzmann committed Aug 25, 2022
1 parent e9691f7 commit 974cb39
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
40 changes: 28 additions & 12 deletions x/stakeibc/keeper/msg_server_liquid_stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
atom = "uatom"
stAtom = "stuatom"
ibcAtom = "ibc/uatom"
chainId = "GAIA"
)

type Account struct {
Expand All @@ -31,7 +32,7 @@ type LiquidStakeState struct {

type LiquidStakeTestCase struct {
user Account
module Account
zoneAccount Account
initialState LiquidStakeState
validMsg stakeibc.MsgLiquidStake
}
Expand All @@ -46,19 +47,22 @@ func (s *KeeperTestSuite) SetupLiquidStake() LiquidStakeTestCase {
}
s.FundAccount(user.acc, user.atomBalance)

module := Account{
acc: s.App.AccountKeeper.GetModuleAddress(stakeibc.ModuleName),
zoneAddress := types.NewZoneAddress(chainId)

zoneAccount := Account{
acc: zoneAddress,
atomBalance: sdk.NewInt64Coin(ibcAtom, 10_000_000),
stAtomBalance: sdk.NewInt64Coin(stAtom, 10_000_000),
}
s.FundModuleAccount(stakeibc.ModuleName, module.atomBalance)
s.FundModuleAccount(stakeibc.ModuleName, module.stAtomBalance)
s.FundAccount(zoneAccount.acc, zoneAccount.atomBalance)
s.FundAccount(zoneAccount.acc, zoneAccount.stAtomBalance)

hostZone := stakeibc.HostZone{
ChainId: "GAIA",
ChainId: chainId,
HostDenom: atom,
IBCDenom: ibcAtom,
RedemptionRate: sdk.NewDec(1.0),
Address: zoneAddress.String(),
}

epochTracker := stakeibc.EpochTracker{
Expand All @@ -79,7 +83,7 @@ func (s *KeeperTestSuite) SetupLiquidStake() LiquidStakeTestCase {

return LiquidStakeTestCase{
user: user,
module: module,
zoneAccount: zoneAccount,
initialState: LiquidStakeState{
depositRecordAmount: initialDepositAmount,
hostZone: hostZone,
Expand All @@ -95,7 +99,7 @@ func (s *KeeperTestSuite) SetupLiquidStake() LiquidStakeTestCase {
func (s *KeeperTestSuite) TestLiquidStakeSuccessful() {
tc := s.SetupLiquidStake()
user := tc.user
module := tc.module
zoneAccount := tc.zoneAccount
msg := tc.validMsg
stakeAmount := sdk.NewInt(int64(msg.Amount))
initialStAtomSupply := s.App.BankKeeper.GetSupply(s.Ctx, stAtom)
Expand All @@ -107,9 +111,9 @@ func (s *KeeperTestSuite) TestLiquidStakeSuccessful() {
// User IBC/UATOM balance should have DECREASED by the size of the stake
expectedUserAtomBalance := user.atomBalance.SubAmount(stakeAmount)
actualUserAtomBalance := s.App.BankKeeper.GetBalance(s.Ctx, user.acc, ibcAtom)
// Module IBC/UATOM balance should have INCREASED by the size of the stake
expectedModuleAtomBalance := module.atomBalance.AddAmount(stakeAmount)
actualModuleAtomBalance := s.App.BankKeeper.GetBalance(s.Ctx, module.acc, ibcAtom)
// zoneAccount IBC/UATOM balance should have INCREASED by the size of the stake
expectedzoneAccountAtomBalance := zoneAccount.atomBalance.AddAmount(stakeAmount)
actualzoneAccountAtomBalance := s.App.BankKeeper.GetBalance(s.Ctx, zoneAccount.acc, ibcAtom)
// User STUATOM balance should have INCREASED by the size of the stake
expectedUserStAtomBalance := user.stAtomBalance.AddAmount(stakeAmount)
actualUserStAtomBalance := s.App.BankKeeper.GetBalance(s.Ctx, user.acc, stAtom)
Expand All @@ -119,7 +123,7 @@ func (s *KeeperTestSuite) TestLiquidStakeSuccessful() {

s.CompareCoins(expectedUserStAtomBalance, actualUserStAtomBalance, "user stuatom balance")
s.CompareCoins(expectedUserAtomBalance, actualUserAtomBalance, "user ibc/uatom balance")
s.CompareCoins(expectedModuleAtomBalance, actualModuleAtomBalance, "module ibc/uatom balance")
s.CompareCoins(expectedzoneAccountAtomBalance, actualzoneAccountAtomBalance, "zoneAccount ibc/uatom balance")
s.CompareCoins(expectedBankSupply, actualBankSupply, "bank stuatom supply")

// Confirm deposit record adjustment
Expand Down Expand Up @@ -238,3 +242,15 @@ func (s *KeeperTestSuite) TestLiquidStakeNoDepositRecord() {

s.Require().EqualError(err, fmt.Sprintf("no deposit record for epoch (%d): not found", 1))
}

func (s *KeeperTestSuite) TestLiquidStake_InvalidHostAddress() {
tc := s.SetupLiquidStake()

// Update hostzone with invalid address
badHostZone := tc.initialState.hostZone
badHostZone.Address = "cosmosXXX"
s.App.StakeibcKeeper.SetHostZone(s.Ctx, badHostZone)

_, err := s.msgServer.LiquidStake(sdk.WrapSDKContext(s.Ctx), &tc.validMsg)
s.Require().EqualError(err, "could not bech32 decode address cosmosXXX of zone with id: GAIA")
}
36 changes: 26 additions & 10 deletions x/stakeibc/keeper/msg_server_redeem_stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

epochtypes "github.com/Stride-Labs/stride/x/epochs/types"
recordtypes "github.com/Stride-Labs/stride/x/records/types"
"github.com/Stride-Labs/stride/x/stakeibc/types"
stakeibc "github.com/Stride-Labs/stride/x/stakeibc/types"
)

Expand All @@ -19,8 +20,8 @@ type RedeemStakeState struct {
}
type RedeemStakeTestCase struct {
user Account
module Account
hostZone stakeibc.HostZone
zoneAccount Account
initialState RedeemStakeState
validMsg stakeibc.MsgRedeemStake
}
Expand All @@ -35,21 +36,24 @@ func (suite *KeeperTestSuite) SetupRedeemStake() RedeemStakeTestCase {
suite.FundAccount(user.acc, user.atomBalance)
suite.FundAccount(user.acc, user.stAtomBalance)

module := Account{
acc: suite.App.AccountKeeper.GetModuleAddress(stakeibc.ModuleName),
zoneAddress := types.NewZoneAddress(chainId)

zoneAccount := Account{
acc: zoneAddress,
atomBalance: sdk.NewInt64Coin("ibc/uatom", 10_000_000),
stAtomBalance: sdk.NewInt64Coin("stuatom", 10_000_000),
}
suite.FundModuleAccount(stakeibc.ModuleName, module.atomBalance)
suite.FundModuleAccount(stakeibc.ModuleName, module.stAtomBalance)
suite.FundAccount(zoneAccount.acc, zoneAccount.atomBalance)
suite.FundAccount(zoneAccount.acc, zoneAccount.stAtomBalance)

// TODO define the host zone with stakedBal and validators with staked amounts
hostZone := stakeibc.HostZone{
ChainId: "GAIA",
ChainId: chainId,
HostDenom: "uatom",
Bech32Prefix: "cosmos",
RedemptionRate: sdk.NewDec(1.0),
StakedBal: 1234567890,
Address: zoneAddress.String(),
}

epochTrackerDay := stakeibc.EpochTracker{
Expand All @@ -65,7 +69,7 @@ func (suite *KeeperTestSuite) SetupRedeemStake() RedeemStakeTestCase {
hostZoneUnbonding := &recordtypes.HostZoneUnbonding{
NativeTokenAmount: uint64(0),
Denom: "uatom",
HostZoneId: "GAIA",
HostZoneId: chainId,
Status: recordtypes.HostZoneUnbonding_BONDED,
}
epochUnbondingRecord.HostZoneUnbondings = append(epochUnbondingRecord.HostZoneUnbondings, hostZoneUnbonding)
Expand All @@ -76,8 +80,8 @@ func (suite *KeeperTestSuite) SetupRedeemStake() RedeemStakeTestCase {

return RedeemStakeTestCase{
user: user,
module: module,
hostZone: hostZone,
zoneAccount: zoneAccount,
initialState: RedeemStakeState{
epochNumber: epochTrackerDay.EpochNumber,
initialNativeEpochUnbondingAmount: uint64(0),
Expand All @@ -86,7 +90,7 @@ func (suite *KeeperTestSuite) SetupRedeemStake() RedeemStakeTestCase {
validMsg: stakeibc.MsgRedeemStake{
Creator: user.acc.String(),
Amount: redeemAmount,
HostZone: "GAIA",
HostZone: chainId,
// TODO set this dynamically through test helpers for host zone
Receiver: "cosmos1g6qdx6kdhpf000afvvpte7hp0vnpzapuyxp8uf",
},
Expand Down Expand Up @@ -120,7 +124,7 @@ func (suite *KeeperTestSuite) TestRedeemStakeSuccessful() {
suite.Require().True(found)
epochUnbondingRecord, found := suite.App.RecordsKeeper.GetEpochUnbondingRecord(suite.Ctx, epochTracker.EpochNumber)
suite.Require().True(found)
hostZoneUnbonding, found := suite.App.RecordsKeeper.GetHostZoneUnbondingByChainId(suite.Ctx, epochUnbondingRecord.EpochNumber, "GAIA")
hostZoneUnbonding, found := suite.App.RecordsKeeper.GetHostZoneUnbondingByChainId(suite.Ctx, epochUnbondingRecord.EpochNumber, chainId)
suite.Require().True(found)

hostZone, _ := suite.App.StakeibcKeeper.GetHostZone(suite.Ctx, msg.HostZone)
Expand Down Expand Up @@ -275,3 +279,15 @@ func (suite *KeeperTestSuite) TestRedeemStakeHostZoneNoUnbondings() {

suite.Require().EqualError(err, "host zone not found in unbondings: GAIA: host zone not registered")
}

func (s *KeeperTestSuite) TestRedeemStake_InvalidHostAddress() {
tc := s.SetupRedeemStake()

// Update hostzone with invalid address
badHostZone, _ := s.App.StakeibcKeeper.GetHostZone(s.Ctx, tc.validMsg.HostZone)
badHostZone.Address = "cosmosXXX"
s.App.StakeibcKeeper.SetHostZone(s.Ctx, badHostZone)

_, err := s.msgServer.RedeemStake(sdk.WrapSDKContext(s.Ctx), &tc.validMsg)
s.Require().EqualError(err, "could not bech32 decode address cosmosXXX of zone with id: GAIA")
}

0 comments on commit 974cb39

Please sign in to comment.