0g-chain/x/swap/keeper/withdraw_test.go
Ruaridh ffef832d45
Upgrade to sdk v0.44.5 and add IBC (#1106)
- Upgrade cosmos-sdk to v0.44.5 from v0.39.2
- Add Legacy Tx Endpoint for backwards compatibility
- Add IBC v1.2.3 Support

Co-authored-by: DracoLi <draco@dracoli.com>
Co-authored-by: drklee3 <derrick@dlee.dev>
Co-authored-by: denalimarsh <denalimarsh@gmail.com>
Co-authored-by: Draco Li <draco@kava.io>
Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
Co-authored-by: Denali Marsh <denali@kava.io>
2022-01-07 17:39:27 -07:00

224 lines
8.1 KiB
Go

package keeper_test
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/swap/types"
)
func (suite *keeperTestSuite) TestWithdraw_AllShares() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
poolID := suite.setupPool(reserves, totalShares, owner.GetAddress())
err := suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), totalShares, reserves[0], reserves[1])
suite.Require().NoError(err)
suite.PoolDeleted(reserves[0].Denom, reserves[1].Denom)
suite.PoolSharesDeleted(owner.GetAddress(), reserves[0].Denom, reserves[1].Denom)
suite.AccountBalanceEqual(owner.GetAddress(), reserves)
suite.ModuleAccountBalanceEqual(sdk.Coins{})
suite.EventsContains(suite.Ctx.EventManager().Events(), sdk.NewEvent(
types.EventTypeSwapWithdraw,
sdk.NewAttribute(types.AttributeKeyPoolID, poolID),
sdk.NewAttribute(types.AttributeKeyOwner, owner.GetAddress().String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, reserves.String()),
sdk.NewAttribute(types.AttributeKeyShares, totalShares.String()),
))
}
func (suite *keeperTestSuite) TestWithdraw_PartialShares() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
poolID := suite.setupPool(reserves, totalShares, owner.GetAddress())
sharesToWithdraw := sdk.NewInt(15e6)
minCoinA := sdk.NewCoin("usdx", sdk.NewInt(25e6))
minCoinB := sdk.NewCoin("ukava", sdk.NewInt(5e6))
err := suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), sharesToWithdraw, minCoinA, minCoinB)
suite.Require().NoError(err)
sharesLeft := totalShares.Sub(sharesToWithdraw)
reservesLeft := sdk.NewCoins(reserves[0].Sub(minCoinB), reserves[1].Sub(minCoinA))
suite.PoolShareTotalEqual(poolID, sharesLeft)
suite.PoolDepositorSharesEqual(owner.GetAddress(), poolID, sharesLeft)
suite.PoolReservesEqual(poolID, reservesLeft)
suite.AccountBalanceEqual(owner.GetAddress(), sdk.NewCoins(minCoinA, minCoinB))
suite.ModuleAccountBalanceEqual(reservesLeft)
suite.EventsContains(suite.Ctx.EventManager().Events(), sdk.NewEvent(
types.EventTypeSwapWithdraw,
sdk.NewAttribute(types.AttributeKeyPoolID, poolID),
sdk.NewAttribute(types.AttributeKeyOwner, owner.GetAddress().String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, sdk.NewCoins(minCoinA, minCoinB).String()),
sdk.NewAttribute(types.AttributeKeyShares, sharesToWithdraw.String()),
))
}
func (suite *keeperTestSuite) TestWithdraw_NoSharesOwned() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
poolID := suite.setupPool(reserves, totalShares, owner.GetAddress())
accWithNoDeposit := sdk.AccAddress("some account")
err := suite.Keeper.Withdraw(suite.Ctx, accWithNoDeposit, totalShares, reserves[0], reserves[1])
suite.EqualError(err, fmt.Sprintf("no deposit for account %s and pool %s: deposit not found", accWithNoDeposit.String(), poolID))
}
func (suite *keeperTestSuite) TestWithdraw_GreaterThanSharesOwned() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
suite.setupPool(reserves, totalShares, owner.GetAddress())
sharesToWithdraw := totalShares.Add(sdk.OneInt())
err := suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), sharesToWithdraw, reserves[0], reserves[1])
suite.EqualError(err, fmt.Sprintf("withdraw of %s shares greater than %s shares owned: invalid shares", sharesToWithdraw, totalShares))
}
func (suite *keeperTestSuite) TestWithdraw_MinWithdraw() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
testCases := []struct {
shares sdk.Int
minCoinA sdk.Coin
minCoinB sdk.Coin
shouldFail bool
}{
{sdk.NewInt(1), sdk.NewCoin("ukava", sdk.NewInt(1)), sdk.NewCoin("usdx", sdk.NewInt(1)), true},
{sdk.NewInt(1), sdk.NewCoin("usdx", sdk.NewInt(5)), sdk.NewCoin("ukava", sdk.NewInt(1)), true},
{sdk.NewInt(2), sdk.NewCoin("ukava", sdk.NewInt(1)), sdk.NewCoin("usdx", sdk.NewInt(1)), true},
{sdk.NewInt(2), sdk.NewCoin("usdx", sdk.NewInt(5)), sdk.NewCoin("ukava", sdk.NewInt(1)), true},
{sdk.NewInt(3), sdk.NewCoin("ukava", sdk.NewInt(1)), sdk.NewCoin("usdx", sdk.NewInt(5)), false},
{sdk.NewInt(3), sdk.NewCoin("usdx", sdk.NewInt(5)), sdk.NewCoin("ukava", sdk.NewInt(1)), false},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("shares=%s minCoinA=%s minCoinB=%s", tc.shares, tc.minCoinA, tc.minCoinB), func() {
suite.SetupTest()
suite.setupPool(reserves, totalShares, owner.GetAddress())
err := suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), tc.shares, tc.minCoinA, tc.minCoinB)
if tc.shouldFail {
suite.EqualError(err, "shares must be increased: insufficient liquidity")
} else {
suite.NoError(err, "expected no liquidity error")
}
})
}
}
func (suite *keeperTestSuite) TestWithdraw_BelowMinimum() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
testCases := []struct {
shares sdk.Int
minCoinA sdk.Coin
minCoinB sdk.Coin
shouldFail bool
}{
{sdk.NewInt(15e6), sdk.NewCoin("ukava", sdk.NewInt(5000001)), sdk.NewCoin("usdx", sdk.NewInt(25e6)), true},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("shares=%s minCoinA=%s minCoinB=%s", tc.shares, tc.minCoinA, tc.minCoinB), func() {
suite.SetupTest()
suite.setupPool(reserves, totalShares, owner.GetAddress())
err := suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), tc.shares, tc.minCoinA, tc.minCoinB)
if tc.shouldFail {
suite.EqualError(err, "minimum withdraw not met: slippage exceeded")
} else {
suite.NoError(err, "expected no slippage error")
}
})
}
}
func (suite *keeperTestSuite) TestWithdraw_PanicOnMissingPool() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
poolID := suite.setupPool(reserves, totalShares, owner.GetAddress())
suite.Keeper.DeletePool(suite.Ctx, poolID)
suite.PanicsWithValue("pool ukava:usdx not found", func() {
_ = suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), totalShares, reserves[0], reserves[1])
}, "expected missing pool record to panic")
}
func (suite *keeperTestSuite) TestWithdraw_PanicOnInvalidPool() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
poolID := suite.setupPool(reserves, totalShares, owner.GetAddress())
poolRecord, found := suite.Keeper.GetPool(suite.Ctx, poolID)
suite.Require().True(found, "expected pool record to exist")
poolRecord.TotalShares = sdk.ZeroInt()
suite.Keeper.SetPool_Raw(suite.Ctx, poolRecord)
suite.PanicsWithValue("invalid pool ukava:usdx: total shares must be greater than zero: invalid pool", func() {
_ = suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), totalShares, reserves[0], reserves[1])
}, "expected invalid pool record to panic")
}
func (suite *keeperTestSuite) TestWithdraw_PanicOnModuleInsufficientFunds() {
owner := suite.CreateAccount(sdk.Coins{})
reserves := sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(10e6)),
sdk.NewCoin("usdx", sdk.NewInt(50e6)),
)
totalShares := sdk.NewInt(30e6)
suite.setupPool(reserves, totalShares, owner.GetAddress())
suite.RemoveCoinsFromModule(sdk.NewCoins(
sdk.NewCoin("ukava", sdk.NewInt(1e6)),
sdk.NewCoin("usdx", sdk.NewInt(5e6)),
))
suite.Panics(func() {
_ = suite.Keeper.Withdraw(suite.Ctx, owner.GetAddress(), totalShares, reserves[0], reserves[1])
}, "expected panic when module account does not have enough funds")
}