Skip to content

Commit

Permalink
test: house withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed May 13, 2024
1 parent 26a4f44 commit 9e52f60
Showing 1 changed file with 279 additions and 0 deletions.
279 changes: 279 additions & 0 deletions tests/e2e/house/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/spf13/cast"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -107,6 +108,18 @@ func (s *E2ETestSuite) SetupSuite() {
},
}

genesis.WithdrawalList = []types.Withdrawal{
{
ID: 1,
Creator: sample.AccAddress(),
Address: dummyDepositor,
MarketUID: marketUID,
ParticipationIndex: 0,
Amount: sdkmath.NewInt(10000),
Mode: types.WithdrawalMode_WITHDRAWAL_MODE_FULL,
},
}

genesis.Params = types.Params{
MinDeposit: sdkmath.NewInt(100),
HouseParticipationFee: sdk.NewDecWithPrec(0, 2),
Expand Down Expand Up @@ -546,3 +559,269 @@ func (s *E2ETestSuite) TestDepositWithAuthzTxCmd() {

s.T().Log("==== new deposit with authorization test passed successfully")
}

func (s *E2ETestSuite) TestWithdrawTxCmd() {
s.T().Log("==== new house deposit command test started")
val := s.network.Validators[0]

clientCtx := val.ClientCtx
{
ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"uid": marketUID,
"start_ts": marketStartDate,
"end_ts": marketEndDate,
"odds": []markettypes.Odds{
{
UID: "9991c60f-2025-48ce-ae79-1dc110f16990",
Meta: "Home",
},
{
UID: "9991c60f-2025-48ce-ae79-1dc110f16991",
Meta: "Draw",
},
{
UID: "9991c60f-2025-48ce-ae79-1dc110f16992",
Meta: "Away",
},
},
"status": markettypes.MarketStatus_MARKET_STATUS_ACTIVE,
"meta": "sample 3way market",
})
require.Nil(s.T(), err)

args := []string{
ticket,
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(fees))).String()),
}
bz, err := clitestutil.ExecTestCLICmd(clientCtx, marketcli.CmdAdd(), args)
s.Require().NoError(err)
respType := sdk.TxResponse{}
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType), bz.String())
s.Require().Equal(uint32(0), respType.Code)
}
s.Require().NoError(s.network.WaitForNextBlock())

expectedValBalance := valBalance.Sub(sdkmath.NewInt(fees))
valBalance = e2esdk.GetSGEBalance(clientCtx, val.Address.String())
s.Require().Equal(expectedValBalance, valBalance)
{
ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"kyc_data": sgetypes.KycDataPayload{
Ignore: false,
Approved: true,
ID: val.Address.String(),
},
"depositor_address": val.Address.String(),
})
require.Nil(s.T(), err)

bz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdDeposit(), []string{
marketUID,
depositAmount.String(),
ticket,
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagGas, flags.GasFlagAuto),
fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.15"),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(fees))).String()),
})
s.Require().NoError(err)

var respType sdk.TxResponse
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType), bz.String())
s.Require().Equal(uint32(0), respType.Code)

txResp, err := clitestutil.GetTxResponse(s.network, clientCtx, respType.TxHash)
s.Require().NoError(err)
s.Require().Equal(uint32(0), txResp.Code)
s.T().Logf("==== success deposit create test case finished")
}

depositFeeAmount := sdk.NewDecFromInt(depositAmount).Mul(genesis.Params.HouseParticipationFee).TruncateInt()

expectedValBalance = valBalance.Sub(depositAmount).Sub(depositFeeAmount).Sub(sdkmath.NewInt(fees))
valBalance = e2esdk.GetSGEBalance(clientCtx, val.Address.String())
s.Require().Equal(expectedValBalance, valBalance)
s.T().Logf("==== bank val balance checked after deposit to the market: %s", valBalance)

depositsBz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.GetCmdQueryDeposits(), []string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
})
require.NoError(s.T(), err)
deposits := types.QueryDepositsResponse{}
err = clientCtx.Codec.UnmarshalJSON(depositsBz.Bytes(), &deposits)
require.NoError(s.T(), err)

require.Equal(s.T(), 5, len(deposits.Deposits))

depositBz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.GetCmdQueryDepositsByAccount(), []string{
acc1.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
})
require.NoError(s.T(), err)
deposit := types.QueryDepositsByAccountResponse{}
err = clientCtx.Codec.UnmarshalJSON(depositBz.Bytes(), &deposit)
require.NoError(s.T(), err)

require.Equal(s.T(), []types.Deposit{
{
Creator: acc1.String(),
DepositorAddress: acc1.String(),
MarketUID: marketUID,
ParticipationIndex: 2,
Amount: depositAmount,
WithdrawalCount: 0,
TotalWithdrawalAmount: sdkmath.ZeroInt(),
},
}, deposit.Deposits)
require.Equal(s.T(), acc1.String(), deposit.Deposits[0].DepositorAddress)
s.T().Log("==== state modifications checked after deposit to the market")

firstWithdrawAmount := depositAmount.Sub(sdkmath.NewInt(10))
testCases := []struct {
name string
marketUID string
participationIndex uint64
ticketClaims jwt.MapClaims
mode types.WithdrawalMode
amount sdkmath.Int
args []string
expectErr bool
expectedCode uint32
expectedErrMsg string
respType proto.Message
}{
{
"invalid transaction with large amount for val",
marketUID,
4,
jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"kyc_data": sgetypes.KycDataPayload{
Ignore: false,
Approved: true,
ID: val.Address.String(),
},
},
types.WithdrawalMode_WITHDRAWAL_MODE_PARTIAL,
sdkmath.NewInt(10000000000),
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagGas, flags.GasFlagAuto),
fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.15"),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(fees))).String()),
},
true,
0,
"withdrawal is more than unused amount",
&sdk.TxResponse{},
},
{
"valid transaction for val",
marketUID,
4,
jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"kyc_data": sgetypes.KycDataPayload{
Ignore: false,
Approved: true,
ID: val.Address.String(),
},
},
types.WithdrawalMode_WITHDRAWAL_MODE_PARTIAL,
firstWithdrawAmount,
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagGas, flags.GasFlagAuto),
fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.15"),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(fees))).String()),
},
false,
0,
"",
&sdk.TxResponse{},
},
{
"second valid transaction for val with the rest of the amount",
marketUID,
4,
jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"kyc_data": sgetypes.KycDataPayload{
Ignore: false,
Approved: true,
ID: val.Address.String(),
},
},
types.WithdrawalMode_WITHDRAWAL_MODE_FULL,
sdkmath.Int{},
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagGas, flags.GasFlagAuto),
fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.15"),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(fees))).String()),
},
false,
0,
"",
&sdk.TxResponse{},
},
}

for _, tc := range testCases {
tc := tc

s.Require().NoError(s.network.WaitForNextBlock())
s.Run(tc.name, func() {
ticket, err := simapp.CreateJwtTicket(tc.ticketClaims)
require.Nil(s.T(), err)

tc.args = append([]string{
tc.marketUID,
cast.ToString(tc.participationIndex),
ticket,
cast.ToString(int32(tc.mode)),
tc.amount.String(),
}, tc.args...)
bz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdWithdraw(), tc.args)

if tc.expectErr {
s.Require().Error(err)
s.T().Logf("error captured: %s", tc.name)
} else {
s.Require().NoError(err)

s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String())
respType := tc.respType.(*sdk.TxResponse)

Check failure on line 812 in tests/e2e/house/suite.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

type assertion must be checked (forcetypeassert)
s.Require().Equal(tc.expectedCode, respType.Code)

txResp, err := clitestutil.GetTxResponse(s.network, clientCtx, respType.TxHash)
s.Require().NoError(err)
s.Require().Equal(tc.expectedCode, txResp.Code)
if tc.expectedErrMsg != "" {
s.Require().Contains(txResp.RawLog, tc.expectedErrMsg)
}
s.T().Logf("==== success deposit create test case finished: %s", tc.name)
}
})
}

s.T().Log("==== new deposit test passed successfully")
}

0 comments on commit 9e52f60

Please sign in to comment.