2022-07-20 23:14:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/earn/keeper"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/testutil"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/types"
|
2022-07-20 23:14:43 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var moduleAccAddress = sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleAccountName)))
|
|
|
|
|
|
|
|
type msgServerTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
|
|
|
|
msgServer types.MsgServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *msgServerTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest()
|
|
|
|
suite.Keeper.SetParams(suite.Ctx, types.DefaultParams())
|
|
|
|
|
|
|
|
suite.msgServer = keeper.NewMsgServerImpl(suite.Keeper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMsgServerTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(msgServerTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *msgServerTestSuite) TestDeposit() {
|
|
|
|
vaultDenom := "usdx"
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
msg := types.NewMsgDeposit(acc.GetAddress().String(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
_, err := suite.msgServer.Deposit(sdk.WrapSDKContext(suite.Ctx), msg)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
suite.AccountBalanceEqual(
|
|
|
|
acc.GetAddress(),
|
|
|
|
sdk.NewCoins(startBalance.Sub(depositAmount)),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bank: Send deposit Account -> Module account
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
banktypes.EventTypeTransfer,
|
|
|
|
sdk.NewAttribute(banktypes.AttributeKeyRecipient, moduleAccAddress.String()),
|
|
|
|
sdk.NewAttribute(banktypes.AttributeKeySender, acc.GetAddress().String()),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, depositAmount.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper Deposit()
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeVaultDeposit,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyVaultDenom, depositAmount.Denom),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyDepositor, acc.GetAddress().String()),
|
2022-09-12 16:23:26 +00:00
|
|
|
// Shares 1:1 to amount
|
2023-04-04 00:08:45 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyShares, sdk.NewDecFromInt(depositAmount.Amount).String()),
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, depositAmount.Amount.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Msg server module
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, acc.GetAddress().String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *msgServerTestSuite) TestWithdraw() {
|
|
|
|
vaultDenom := "usdx"
|
2022-09-12 17:29:35 +00:00
|
|
|
suite.CreateVault(vaultDenom, types.StrategyTypes{types.STRATEGY_TYPE_HARD}, false, nil)
|
2022-07-20 23:14:43 +00:00
|
|
|
|
|
|
|
startBalance := sdk.NewInt64Coin(vaultDenom, 1000)
|
|
|
|
depositAmount := sdk.NewInt64Coin(vaultDenom, 100)
|
|
|
|
|
|
|
|
acc := suite.CreateAccount(sdk.NewCoins(startBalance), 0)
|
|
|
|
|
2022-09-12 16:43:59 +00:00
|
|
|
msgDeposit := types.NewMsgDeposit(acc.GetAddress().String(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
_, err := suite.msgServer.Deposit(sdk.WrapSDKContext(suite.Ctx), msgDeposit)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Withdraw all
|
2022-09-12 16:43:59 +00:00
|
|
|
msgWithdraw := types.NewMsgWithdraw(acc.GetAddress().String(), depositAmount, types.STRATEGY_TYPE_HARD)
|
2022-07-20 23:14:43 +00:00
|
|
|
_, err = suite.msgServer.Withdraw(sdk.WrapSDKContext(suite.Ctx), msgWithdraw)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Bank: Send deposit Account -> Module account
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
banktypes.EventTypeTransfer,
|
|
|
|
// Direction opposite from Deposit()
|
|
|
|
sdk.NewAttribute(banktypes.AttributeKeyRecipient, acc.GetAddress().String()),
|
|
|
|
sdk.NewAttribute(banktypes.AttributeKeySender, moduleAccAddress.String()),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, depositAmount.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper Withdraw()
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
types.EventTypeVaultWithdraw,
|
|
|
|
sdk.NewAttribute(types.AttributeKeyVaultDenom, depositAmount.Denom),
|
|
|
|
sdk.NewAttribute(types.AttributeKeyOwner, acc.GetAddress().String()),
|
2023-04-04 00:08:45 +00:00
|
|
|
sdk.NewAttribute(types.AttributeKeyShares, sdk.NewDecFromInt(depositAmount.Amount).String()),
|
2022-07-20 23:14:43 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyAmount, depositAmount.Amount.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Msg server module
|
|
|
|
suite.EventsContains(
|
|
|
|
suite.GetEvents(),
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, acc.GetAddress().String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|