2021-07-15 14:05:54 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/incentive/types"
|
2021-07-15 14:05:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClaimTests runs unit tests for the keeper Claim methods
|
|
|
|
type ClaimTests struct {
|
|
|
|
unitTester
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClaim(t *testing.T) {
|
|
|
|
suite.Run(t, new(ClaimTests))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ClaimTests) ErrorIs(err, target error) bool {
|
|
|
|
return suite.Truef(errors.Is(err, target), "err didn't match: %s, it was: %s", target, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ClaimTests) TestCannotClaimWhenMultiplierNotRecognised() {
|
|
|
|
subspace := &fakeParamSubspace{
|
|
|
|
params: types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
ClaimMultipliers: types.MultipliersPerDenoms{
|
2021-08-04 14:55:29 +00:00
|
|
|
{
|
|
|
|
Denom: "hard",
|
|
|
|
Multipliers: types.Multipliers{
|
2022-01-08 00:39:27 +00:00
|
|
|
types.NewMultiplier("small", 1, d("0.2")),
|
2021-08-04 14:55:29 +00:00
|
|
|
},
|
|
|
|
},
|
2021-07-15 14:05:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-09-23 16:38:22 +00:00
|
|
|
suite.keeper = suite.NewKeeper(subspace, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
|
|
|
claim := types.DelegatorClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
suite.storeDelegatorClaim(claim)
|
|
|
|
|
|
|
|
// multiplier not in params
|
2021-08-04 14:55:29 +00:00
|
|
|
err := suite.keeper.ClaimDelegatorReward(suite.ctx, claim.Owner, claim.Owner, "hard", "large")
|
2021-07-15 14:05:54 +00:00
|
|
|
suite.ErrorIs(err, types.ErrInvalidMultiplier)
|
|
|
|
|
|
|
|
// invalid multiplier name
|
2021-08-04 14:55:29 +00:00
|
|
|
err = suite.keeper.ClaimDelegatorReward(suite.ctx, claim.Owner, claim.Owner, "hard", "")
|
2021-07-15 14:05:54 +00:00
|
|
|
suite.ErrorIs(err, types.ErrInvalidMultiplier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ClaimTests) TestCannotClaimAfterEndTime() {
|
|
|
|
endTime := time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
subspace := &fakeParamSubspace{
|
|
|
|
params: types.Params{
|
2022-01-08 00:39:27 +00:00
|
|
|
ClaimMultipliers: types.MultipliersPerDenoms{
|
2021-08-04 14:55:29 +00:00
|
|
|
{
|
|
|
|
Denom: "hard",
|
|
|
|
Multipliers: types.Multipliers{
|
2022-01-08 00:39:27 +00:00
|
|
|
types.NewMultiplier("small", 1, d("0.2")),
|
2021-08-04 14:55:29 +00:00
|
|
|
},
|
|
|
|
},
|
2021-07-15 14:05:54 +00:00
|
|
|
},
|
|
|
|
ClaimEnd: endTime,
|
|
|
|
},
|
|
|
|
}
|
2022-09-23 16:38:22 +00:00
|
|
|
suite.keeper = suite.NewKeeper(subspace, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
2021-07-15 14:05:54 +00:00
|
|
|
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(endTime.Add(time.Nanosecond))
|
|
|
|
|
|
|
|
claim := types.DelegatorClaim{
|
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
suite.storeDelegatorClaim(claim)
|
|
|
|
|
2021-08-04 14:55:29 +00:00
|
|
|
err := suite.keeper.ClaimDelegatorReward(suite.ctx, claim.Owner, claim.Owner, "hard", "small")
|
2021-07-15 14:05:54 +00:00
|
|
|
suite.ErrorIs(err, types.ErrClaimExpired)
|
|
|
|
}
|