Skip to content

Commit

Permalink
test: reward subaccount topup test
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpioborn committed Sep 27, 2023
1 parent 5a108c7 commit 4c7b744
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 3 deletions.
2 changes: 1 addition & 1 deletion x/reward/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k Keeper) DistributeRewards(ctx sdk.Context, funderAddr string, distributi
Amount: d.Allocation.Amount,
},
}); err != nil {
return sdkerrors.Wrapf(types.ErrSubAccRewardTopUp, "owner address %s", d.AccAddr)
return sdkerrors.Wrapf(types.ErrSubAccRewardTopUp, "owner address %s, %s", d.AccAddr, err)
}
default:
return types.ErrUnknownAccType
Expand Down
2 changes: 1 addition & 1 deletion x/reward/keeper/msg_server_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (k msgServer) ApplyReward(goCtx context.Context, msg *types.MsgApplyReward)
}

if err := k.DistributeRewards(ctx, campaign.FunderAddress, distribution); err != nil {
return nil, types.ErrInDistributionOfRewards
return nil, sdkerrors.Wrapf(types.ErrInDistributionOfRewards, "%s", err)
}

k.UpdateCampaignPool(ctx, campaign, distribution)
Expand Down
164 changes: 163 additions & 1 deletion x/reward/keeper/msg_server_reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sge-network/sge/testutil/sample"
"github.com/sge-network/sge/testutil/simapp"
bettypes "github.com/sge-network/sge/x/bet/types"
"github.com/sge-network/sge/x/reward/keeper"
"github.com/sge-network/sge/x/reward/types"
subaccounttypes "github.com/sge-network/sge/x/subaccount/types"
)

func getDefaultClaim(creator string) jwt.MapClaims {
Expand All @@ -29,7 +31,8 @@ func getDefaultClaim(creator string) jwt.MapClaims {
}

func createCampaign(t *testing.T, k *keeper.Keeper, srv types.MsgServer, ctx sdk.Context,
funder string, claims jwt.MapClaims) string {
funder string, claims jwt.MapClaims,
) string {
ticket, err := simapp.CreateJwtTicket(claims)
require.Nil(t, err)

Expand Down Expand Up @@ -392,3 +395,162 @@ func TestMsgApplyNoLossBetsReward(t *testing.T) {
})
}
}

func TestMsgApplySignupRewardSubAcc(t *testing.T) {
tApp, k, ctx := setupKeeperAndApp(t)
srv := keeper.NewMsgServerImpl(*k)
ctx = ctx.WithBlockTime(time.Now())
wctx := sdk.WrapSDKContext(ctx)

funder := simapp.TestParamUsers["user1"].Address.String()
receiverAddr := simapp.TestParamUsers["user2"].Address.String()

campClaims := getDefaultClaim(funder)
campClaims["type"] = types.RewardType_REWARD_TYPE_SIGNUP
campClaims["reward_defs"] = []types.Definition{
{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
Amount: sdkmath.NewInt(100),
DstAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_SUB,
UnlockTS: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
}

campUID := createCampaign(t, k, srv, ctx, funder, campClaims)

_, err := tApp.SubaccountKeeper.CreateSubAccount(ctx, receiverAddr, receiverAddr, []subaccounttypes.LockedBalance{
{
Amount: sdk.ZeroInt(),
UnlockTS: uint64(ctx.BlockTime().Add(60 * time.Minute).Unix()),
},
})
require.NoError(t, err)

for _, tc := range []struct {
desc string
claims jwt.MapClaims
err error
}{
{
desc: "invalid ticket",
claims: jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"receiver": "invalid",
},
err: types.ErrInTicketVerification,
},
{
desc: "invalid receiver type",
claims: jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"receiver": types.Receiver{
RecType: types.ReceiverType_RECEIVER_TYPE_REFEREE,
Addr: receiverAddr,
},
},
err: types.ErrAccReceiverTypeNotFound,
},
{
desc: "subaccount not exists",
claims: jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"receiver": types.Receiver{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
Addr: sample.AccAddress(),
},
},
err: types.ErrSubAccRewardTopUp,
},
{
desc: "valid",
claims: jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"receiver": types.Receiver{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
Addr: receiverAddr,
},
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
ticket, err := simapp.CreateJwtTicket(tc.claims)
require.Nil(t, err)
reward := &types.MsgApplyReward{
Creator: funder,
CampaignUid: campUID,
Ticket: ticket,
}
_, err = srv.ApplyReward(wctx, reward)
if tc.err != nil {
require.ErrorContains(t, err, tc.err.Error())
} else {
require.NoError(t, err)
}
})
}
}

func TestMsgApplySubAccFunds(t *testing.T) {
tApp, k, ctx := setupKeeperAndApp(t)
srv := keeper.NewMsgServerImpl(*k)
ctx = ctx.WithBlockTime(time.Now())
wctx := sdk.WrapSDKContext(ctx)

funder := simapp.TestParamUsers["user1"].Address.String()
receiverAddr := simapp.TestParamUsers["user2"].Address.String()

rewardAmount := int64(100)

campClaims := getDefaultClaim(funder)
campClaims["type"] = types.RewardType_REWARD_TYPE_SIGNUP
campClaims["reward_defs"] = []types.Definition{
{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
Amount: sdkmath.NewInt(rewardAmount),
DstAccType: types.ReceiverAccType_RECEIVER_ACC_TYPE_SUB,
UnlockTS: uint64(ctx.BlockTime().Add(10 * time.Minute).Unix()),
},
}

campUID := createCampaign(t, k, srv, ctx, funder, campClaims)

_, err := tApp.SubaccountKeeper.CreateSubAccount(ctx, receiverAddr, receiverAddr, []subaccounttypes.LockedBalance{
{
Amount: sdk.ZeroInt(),
UnlockTS: uint64(ctx.BlockTime().Add(60 * time.Minute).Unix()),
},
})
require.NoError(t, err)

claims := jwt.MapClaims{
"exp": time.Now().Add(time.Minute * 5).Unix(),
"iat": time.Now().Unix(),
"receiver": types.Receiver{
RecType: types.ReceiverType_RECEIVER_TYPE_SINGLE,
Addr: receiverAddr,
},
}

ticket, err := simapp.CreateJwtTicket(claims)
require.Nil(t, err)

reward := &types.MsgApplyReward{
Creator: funder,
CampaignUid: campUID,
Ticket: ticket,
}
_, err = srv.ApplyReward(wctx, reward)
require.NoError(t, err)

subAccAddr, found := tApp.SubaccountKeeper.GetSubAccountByOwner(ctx, sdk.MustAccAddressFromBech32(receiverAddr))
require.True(t, found)

balance, found := tApp.SubaccountKeeper.GetBalance(ctx, subAccAddr)
require.True(t, found)

require.Equal(t, rewardAmount, balance.DepositedAmount.Int64())
}

0 comments on commit 4c7b744

Please sign in to comment.