mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
Harvest: refactor deposit type to claim type (#716)
* status: refactor deposit type to claim type * refactor all deposit types to claim types * refactor msg withdraw * update module errors * refactor querier and rest * update spec * update tests * update handler, alias * refactor to IterateDepositsByDenom * v0_12 legacy types * fix build
This commit is contained in:
parent
510b7e7c04
commit
df2c4271e4
@ -15,9 +15,9 @@ const (
|
||||
AttributeKeyClaimAmount = types.AttributeKeyClaimAmount
|
||||
AttributeKeyClaimHolder = types.AttributeKeyClaimHolder
|
||||
AttributeKeyClaimMultiplier = types.AttributeKeyClaimMultiplier
|
||||
AttributeKeyClaimType = types.AttributeKeyClaimType
|
||||
AttributeKeyDeposit = types.AttributeKeyDeposit
|
||||
AttributeKeyDepositDenom = types.AttributeKeyDepositDenom
|
||||
AttributeKeyDepositType = types.AttributeKeyDepositType
|
||||
AttributeKeyDepositor = types.AttributeKeyDepositor
|
||||
AttributeKeyRewardsDistribution = types.AttributeKeyRewardsDistribution
|
||||
AttributeValueCategory = types.AttributeValueCategory
|
||||
@ -82,8 +82,7 @@ var (
|
||||
DefaultGovSchedules = types.DefaultGovSchedules
|
||||
DefaultLPSchedules = types.DefaultLPSchedules
|
||||
DefaultPreviousBlockTime = types.DefaultPreviousBlockTime
|
||||
DepositTypesClaimQuery = types.DepositTypesClaimQuery
|
||||
DepositTypesDepositQuery = types.DepositTypesDepositQuery
|
||||
ClaimTypesClaimQuery = types.ClaimTypesClaimQuery
|
||||
DepositsKeyPrefix = types.DepositsKeyPrefix
|
||||
ErrAccountNotFound = types.ErrAccountNotFound
|
||||
ErrClaimExpired = types.ErrClaimExpired
|
||||
@ -91,10 +90,10 @@ var (
|
||||
ErrDepositNotFound = types.ErrDepositNotFound
|
||||
ErrGovScheduleNotFound = types.ErrGovScheduleNotFound
|
||||
ErrInsufficientModAccountBalance = types.ErrInsufficientModAccountBalance
|
||||
ErrInvaliWithdrawAmount = types.ErrInvaliWithdrawAmount
|
||||
ErrInvaliWithdrawAmount = types.ErrInvalidWithdrawAmount
|
||||
ErrInvalidAccountType = types.ErrInvalidAccountType
|
||||
ErrInvalidDepositDenom = types.ErrInvalidDepositDenom
|
||||
ErrInvalidDepositType = types.ErrInvalidDepositType
|
||||
ErrInvalidClaimType = types.ErrInvalidClaimType
|
||||
ErrInvalidMultiplier = types.ErrInvalidMultiplier
|
||||
ErrLPScheduleNotFound = types.ErrLPScheduleNotFound
|
||||
ErrZeroClaim = types.ErrZeroClaim
|
||||
@ -116,7 +115,7 @@ type (
|
||||
DelegatorDistributionSchedule = types.DelegatorDistributionSchedule
|
||||
DelegatorDistributionSchedules = types.DelegatorDistributionSchedules
|
||||
Deposit = types.Deposit
|
||||
DepositType = types.DepositType
|
||||
ClaimType = types.ClaimType
|
||||
DistributionSchedule = types.DistributionSchedule
|
||||
DistributionSchedules = types.DistributionSchedules
|
||||
GenesisDistributionTime = types.GenesisDistributionTime
|
||||
|
@ -22,7 +22,7 @@ const (
|
||||
flagName = "name"
|
||||
flagDepositDenom = "deposit-denom"
|
||||
flagOwner = "owner"
|
||||
flagDepositType = "deposit-type"
|
||||
flagClaimType = "claim-type"
|
||||
)
|
||||
|
||||
// GetQueryCmd returns the cli query commands for the harvest module
|
||||
@ -122,8 +122,8 @@ func queryDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
|
||||
Example:
|
||||
$ kvcli q harvest deposits
|
||||
$ kvcli q harvest deposits --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --deposit-type lp --deposit-denom bnb
|
||||
$ kvcli q harvest deposits --deposit-type stake --deposit-denom ukava
|
||||
$ kvcli q harvest deposits --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --deposit-denom bnb
|
||||
$ kvcli q harvest deposits --deposit-denom ukava
|
||||
$ kvcli q harvest deposits --deposit-denom btcb`,
|
||||
),
|
||||
Args: cobra.NoArgs,
|
||||
@ -131,11 +131,9 @@ func queryDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
var owner sdk.AccAddress
|
||||
var depositType types.DepositType
|
||||
|
||||
ownerBech := viper.GetString(flagOwner)
|
||||
depositDenom := viper.GetString(flagDepositDenom)
|
||||
depositTypeStr := viper.GetString(flagDepositType)
|
||||
|
||||
if len(ownerBech) != 0 {
|
||||
depositOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
||||
@ -145,17 +143,10 @@ func queryDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
owner = depositOwner
|
||||
}
|
||||
|
||||
if len(depositTypeStr) != 0 {
|
||||
if err := types.DepositType(depositTypeStr).IsValid(); err != nil {
|
||||
return err
|
||||
}
|
||||
depositType = types.DepositType(depositTypeStr)
|
||||
}
|
||||
|
||||
page := viper.GetInt(flags.FlagPage)
|
||||
limit := viper.GetInt(flags.FlagLimit)
|
||||
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, owner, depositType)
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, owner)
|
||||
bz, err := cdc.MarshalJSON(params)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -179,7 +170,6 @@ func queryDepositsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit (max 100)")
|
||||
cmd.Flags().String(flagOwner, "", "(optional) filter for deposits by owner address")
|
||||
cmd.Flags().String(flagDepositDenom, "", "(optional) filter for deposits by denom")
|
||||
cmd.Flags().String(flagDepositType, "", "(optional) filter for deposits by type (lp or staking)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@ -191,8 +181,8 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
|
||||
Example:
|
||||
$ kvcli q harvest claims
|
||||
$ kvcli q harvest claims --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --deposit-type lp --deposit-denom bnb
|
||||
$ kvcli q harvest claims --deposit-type stake --deposit-denom ukava
|
||||
$ kvcli q harvest claims --owner kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny --claim-type lp --deposit-denom bnb
|
||||
$ kvcli q harvest claims --claim-type stake --deposit-denom ukava
|
||||
$ kvcli q harvest claims --deposit-denom btcb`,
|
||||
),
|
||||
Args: cobra.NoArgs,
|
||||
@ -200,11 +190,11 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||
|
||||
var owner sdk.AccAddress
|
||||
var depositType types.DepositType
|
||||
var claimType types.ClaimType
|
||||
|
||||
ownerBech := viper.GetString(flagOwner)
|
||||
depositDenom := viper.GetString(flagDepositDenom)
|
||||
depositTypeStr := viper.GetString(flagDepositType)
|
||||
claimTypeStr := viper.GetString(flagClaimType)
|
||||
|
||||
if len(ownerBech) != 0 {
|
||||
claimOwner, err := sdk.AccAddressFromBech32(ownerBech)
|
||||
@ -214,17 +204,17 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
owner = claimOwner
|
||||
}
|
||||
|
||||
if len(depositTypeStr) != 0 {
|
||||
if err := types.DepositType(depositTypeStr).IsValid(); err != nil {
|
||||
if len(claimTypeStr) != 0 {
|
||||
if err := types.ClaimType(claimTypeStr).IsValid(); err != nil {
|
||||
return err
|
||||
}
|
||||
depositType = types.DepositType(depositTypeStr)
|
||||
claimType = types.ClaimType(claimTypeStr)
|
||||
}
|
||||
|
||||
page := viper.GetInt(flags.FlagPage)
|
||||
limit := viper.GetInt(flags.FlagLimit)
|
||||
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, owner, depositType)
|
||||
params := types.NewQueryClaimParams(page, limit, depositDenom, owner, claimType)
|
||||
bz, err := cdc.MarshalJSON(params)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -248,7 +238,7 @@ func queryClaimsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit (max 100)")
|
||||
cmd.Flags().String(flagOwner, "", "(optional) filter for claims by owner address")
|
||||
cmd.Flags().String(flagDepositDenom, "", "(optional) filter for claims by denom")
|
||||
cmd.Flags().String(flagDepositType, "", "(optional) filter for claims by type (lp or staking)")
|
||||
cmd.Flags().String(flagClaimType, "", "(optional) filter for claims by type (lp or staking)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -41,11 +41,10 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
||||
|
||||
func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "deposit [amount] [deposit-type]",
|
||||
Use: "deposit [amount]",
|
||||
Short: "deposit coins to harvest",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Example: fmt.Sprintf(
|
||||
`%s tx %s deposit 10000000bnb lp --from <key>`, version.ClientName, types.ModuleName,
|
||||
`%s tx %s deposit 10000000bnb --from <key>`, version.ClientName, types.ModuleName,
|
||||
),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
@ -55,7 +54,7 @@ func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg := types.NewMsgDeposit(cliCtx.GetFromAddress(), amount, args[1])
|
||||
msg := types.NewMsgDeposit(cliCtx.GetFromAddress(), amount)
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -66,11 +65,11 @@ func getCmdDeposit(cdc *codec.Codec) *cobra.Command {
|
||||
|
||||
func getCmdWithdraw(cdc *codec.Codec) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "withdraw [amount] [deposit-type]",
|
||||
Use: "withdraw [amount]",
|
||||
Short: "withdraw coins from harvest",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: fmt.Sprintf(
|
||||
`%s tx %s withdraw 10000000bnb lp --from <key>`, version.ClientName, types.ModuleName,
|
||||
`%s tx %s withdraw 10000000bnb --from <key>`, version.ClientName, types.ModuleName,
|
||||
),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
inBuf := bufio.NewReader(cmd.InOrStdin())
|
||||
@ -80,7 +79,7 @@ func getCmdWithdraw(cdc *codec.Codec) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg := types.NewMsgWithdraw(cliCtx.GetFromAddress(), amount, args[1])
|
||||
msg := types.NewMsgWithdraw(cliCtx.GetFromAddress(), amount)
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -56,7 +56,6 @@ func queryDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
|
||||
var depositDenom string
|
||||
var depositOwner sdk.AccAddress
|
||||
var depositType types.DepositType
|
||||
|
||||
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
||||
depositDenom = strings.TrimSpace(x)
|
||||
@ -71,17 +70,7 @@ func queryDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
if x := r.URL.Query().Get(RestType); len(x) != 0 {
|
||||
depositTypeStr := strings.ToLower(strings.TrimSpace(x))
|
||||
err := types.DepositType(depositTypeStr).IsValid()
|
||||
if err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
depositType = types.DepositType(depositTypeStr)
|
||||
}
|
||||
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, depositOwner, depositType)
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, depositOwner)
|
||||
|
||||
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||
if err != nil {
|
||||
@ -115,7 +104,7 @@ func queryClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
|
||||
var depositDenom string
|
||||
var claimOwner sdk.AccAddress
|
||||
var depositType types.DepositType
|
||||
var claimType types.ClaimType
|
||||
|
||||
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
||||
depositDenom = strings.TrimSpace(x)
|
||||
@ -131,16 +120,16 @@ func queryClaimsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
if x := r.URL.Query().Get(RestType); len(x) != 0 {
|
||||
depositTypeStr := strings.ToLower(strings.TrimSpace(x))
|
||||
err := types.DepositType(depositTypeStr).IsValid()
|
||||
claimTypeStr := strings.ToLower(strings.TrimSpace(x))
|
||||
err := types.ClaimType(claimTypeStr).IsValid()
|
||||
if err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
depositType = types.DepositType(depositTypeStr)
|
||||
claimType = types.ClaimType(claimTypeStr)
|
||||
}
|
||||
|
||||
params := types.NewQueryDepositParams(page, limit, depositDenom, claimOwner, depositType)
|
||||
params := types.NewQueryClaimParams(page, limit, depositDenom, claimOwner, claimType)
|
||||
|
||||
bz, err := cliCtx.Codec.MarshalJSON(params)
|
||||
if err != nil {
|
||||
|
@ -33,7 +33,7 @@ func postDepositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
msg := types.NewMsgDeposit(req.From, req.Amount, strings.ToLower(req.DepositType))
|
||||
msg := types.NewMsgDeposit(req.From, req.Amount)
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
@ -54,7 +54,7 @@ func postWithdrawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
msg := types.NewMsgWithdraw(req.From, req.Amount, strings.ToLower(req.DepositType))
|
||||
msg := types.NewMsgWithdraw(req.From, req.Amount)
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
|
@ -30,7 +30,7 @@ func NewHandler(k Keeper) sdk.Handler {
|
||||
}
|
||||
|
||||
func handleMsgClaimReward(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaimReward) (*sdk.Result, error) {
|
||||
err := k.ClaimReward(ctx, msg.Sender, msg.Receiver, msg.DepositDenom, types.DepositType(strings.ToLower(msg.DepositType)), types.MultiplierName(strings.ToLower(msg.MultiplierName)))
|
||||
err := k.ClaimReward(ctx, msg.Sender, msg.Receiver, msg.DepositDenom, types.ClaimType(strings.ToLower(msg.ClaimType)), types.MultiplierName(strings.ToLower(msg.MultiplierName)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -48,7 +48,7 @@ func handleMsgClaimReward(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaimRe
|
||||
}
|
||||
|
||||
func handleMsgDeposit(ctx sdk.Context, k keeper.Keeper, msg types.MsgDeposit) (*sdk.Result, error) {
|
||||
err := k.Deposit(ctx, msg.Depositor, msg.Amount, types.DepositType(strings.ToLower(msg.DepositType)))
|
||||
err := k.Deposit(ctx, msg.Depositor, msg.Amount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -66,7 +66,7 @@ func handleMsgDeposit(ctx sdk.Context, k keeper.Keeper, msg types.MsgDeposit) (*
|
||||
}
|
||||
|
||||
func handleMsgWithdraw(ctx sdk.Context, k keeper.Keeper, msg types.MsgWithdraw) (*sdk.Result, error) {
|
||||
err := k.Withdraw(ctx, msg.Depositor, msg.Amount, types.DepositType(strings.ToLower(msg.DepositType)))
|
||||
err := k.Withdraw(ctx, msg.Depositor, msg.Amount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -351,7 +351,7 @@ func (suite *KeeperTestSuite) TestBorrow() {
|
||||
// Deposit coins to harvest
|
||||
depositedCoins := sdk.NewCoins()
|
||||
for _, depositCoin := range tc.args.depositCoins {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin, types.LP)
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.borrower, depositCoin)
|
||||
suite.Require().NoError(err)
|
||||
depositedCoins.Add(depositCoin)
|
||||
}
|
||||
|
@ -20,11 +20,11 @@ const (
|
||||
)
|
||||
|
||||
// ClaimReward sends the reward amount to the reward owner and deletes the claim from the store
|
||||
func (k Keeper) ClaimReward(ctx sdk.Context, claimHolder sdk.AccAddress, receiver sdk.AccAddress, depositDenom string, depositType types.DepositType, multiplier types.MultiplierName) error {
|
||||
func (k Keeper) ClaimReward(ctx sdk.Context, claimHolder sdk.AccAddress, receiver sdk.AccAddress, depositDenom string, claimType types.ClaimType, multiplier types.MultiplierName) error {
|
||||
|
||||
claim, found := k.GetClaim(ctx, claimHolder, depositDenom, depositType)
|
||||
claim, found := k.GetClaim(ctx, claimHolder, depositDenom, claimType)
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrClaimNotFound, "no %s %s claim found for %s", depositDenom, depositType, claimHolder)
|
||||
return sdkerrors.Wrapf(types.ErrClaimNotFound, "no %s %s claim found for %s", depositDenom, claimType, claimHolder)
|
||||
}
|
||||
|
||||
err := k.validateSenderReceiver(ctx, claimHolder, receiver)
|
||||
@ -32,13 +32,13 @@ func (k Keeper) ClaimReward(ctx sdk.Context, claimHolder sdk.AccAddress, receive
|
||||
return err
|
||||
}
|
||||
|
||||
switch depositType {
|
||||
switch claimType {
|
||||
case types.LP:
|
||||
err = k.claimLPReward(ctx, claim, receiver, multiplier)
|
||||
case types.Stake:
|
||||
err = k.claimDelegatorReward(ctx, claim, receiver, multiplier)
|
||||
default:
|
||||
return sdkerrors.Wrap(types.ErrInvalidDepositType, string(depositType))
|
||||
return sdkerrors.Wrap(types.ErrInvalidClaimType, string(claimType))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@ -49,7 +49,7 @@ func (k Keeper) ClaimReward(ctx sdk.Context, claimHolder sdk.AccAddress, receive
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, claim.Amount.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyClaimHolder, claimHolder.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, depositDenom),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositType, string(depositType)),
|
||||
sdk.NewAttribute(types.AttributeKeyClaimType, string(claimType)),
|
||||
sdk.NewAttribute(types.AttributeKeyClaimMultiplier, string(multiplier)),
|
||||
),
|
||||
)
|
||||
|
@ -20,7 +20,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner sdk.AccAddress
|
||||
receiver sdk.AccAddress
|
||||
denom string
|
||||
depositType types.DepositType
|
||||
claimType types.ClaimType
|
||||
multiplier types.MultiplierName
|
||||
blockTime time.Time
|
||||
createClaim bool
|
||||
@ -47,7 +47,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -69,7 +69,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.Stake,
|
||||
claimType: types.Stake,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -91,7 +91,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -113,7 +113,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -135,7 +135,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test2"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -157,7 +157,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -179,7 +179,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: false,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -201,7 +201,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2022, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -223,7 +223,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
claimOwner: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
receiver: sdk.AccAddress(crypto.AddressHash([]byte("test2"))),
|
||||
denom: "bnb",
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
createClaim: true,
|
||||
claimAmount: sdk.NewCoin("hard", sdk.NewInt(100)),
|
||||
@ -303,17 +303,17 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
suite.keeper = keeper
|
||||
|
||||
if tc.args.createClaim {
|
||||
claim := types.NewClaim(tc.args.claimOwner, tc.args.denom, tc.args.claimAmount, tc.args.depositType)
|
||||
claim := types.NewClaim(tc.args.claimOwner, tc.args.denom, tc.args.claimAmount, tc.args.claimType)
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetClaim(suite.ctx, claim) })
|
||||
}
|
||||
|
||||
err := suite.keeper.ClaimReward(suite.ctx, tc.args.claimOwner, tc.args.receiver, tc.args.denom, tc.args.depositType, tc.args.multiplier)
|
||||
err := suite.keeper.ClaimReward(suite.ctx, tc.args.claimOwner, tc.args.receiver, tc.args.denom, tc.args.claimType, tc.args.multiplier)
|
||||
if tc.errArgs.expectPass {
|
||||
suite.Require().NoError(err)
|
||||
acc := suite.getAccount(tc.args.receiver)
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
mAcc := suite.getModuleAccount(types.LPAccount)
|
||||
if tc.args.depositType == types.Stake {
|
||||
if tc.args.claimType == types.Stake {
|
||||
mAcc = suite.getModuleAccount(types.DelegatorAccount)
|
||||
}
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
@ -324,7 +324,7 @@ func (suite *KeeperTestSuite) TestClaim() {
|
||||
} else {
|
||||
suite.Require().False(ok)
|
||||
}
|
||||
_, f := suite.keeper.GetClaim(ctx, tc.args.claimOwner, tc.args.denom, tc.args.depositType)
|
||||
_, f := suite.keeper.GetClaim(ctx, tc.args.claimOwner, tc.args.denom, tc.args.claimType)
|
||||
suite.Require().False(f)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
|
@ -9,26 +9,21 @@ import (
|
||||
)
|
||||
|
||||
// Deposit deposit
|
||||
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin, depositType types.DepositType) error {
|
||||
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
||||
|
||||
err := k.ValidateDeposit(ctx, amount, depositType)
|
||||
err := k.ValidateDeposit(ctx, amount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch depositType {
|
||||
case types.LP:
|
||||
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, sdk.NewCoins(amount))
|
||||
default:
|
||||
return sdkerrors.Wrap(types.ErrInvalidDepositType, string(depositType))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom, depositType)
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
||||
if !found {
|
||||
deposit = types.NewDeposit(depositor, amount, depositType)
|
||||
deposit = types.NewDeposit(depositor, amount)
|
||||
} else {
|
||||
deposit.Amount = deposit.Amount.Add(amount)
|
||||
}
|
||||
@ -41,7 +36,6 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, deposit.Amount.Denom),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositType, string(depositType)),
|
||||
),
|
||||
)
|
||||
|
||||
@ -49,22 +43,7 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Co
|
||||
}
|
||||
|
||||
// ValidateDeposit validates a deposit
|
||||
func (k Keeper) ValidateDeposit(ctx sdk.Context, amount sdk.Coin, depositType types.DepositType) error {
|
||||
var err error
|
||||
switch depositType {
|
||||
case types.LP:
|
||||
err = k.ValidateLPDeposit(ctx, amount, depositType)
|
||||
default:
|
||||
return sdkerrors.Wrap(types.ErrInvalidDepositType, string(depositType))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateLPDeposit validates that a liquidity provider deposit
|
||||
func (k Keeper) ValidateLPDeposit(ctx sdk.Context, amount sdk.Coin, depositType types.DepositType) error {
|
||||
func (k Keeper) ValidateDeposit(ctx sdk.Context, amount sdk.Coin) error {
|
||||
params := k.GetParams(ctx)
|
||||
for _, lps := range params.LiquidityProviderSchedules {
|
||||
if lps.DepositDenom == amount.Denom {
|
||||
@ -75,22 +54,16 @@ func (k Keeper) ValidateLPDeposit(ctx sdk.Context, amount sdk.Coin, depositType
|
||||
}
|
||||
|
||||
// Withdraw returns some or all of a deposit back to original depositor
|
||||
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin, depositType types.DepositType) error {
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom, depositType)
|
||||
func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.Coin) error {
|
||||
deposit, found := k.GetDeposit(ctx, depositor, amount.Denom)
|
||||
if !found {
|
||||
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no %s %s deposit found for %s", amount.Denom, depositType, depositor)
|
||||
return sdkerrors.Wrapf(types.ErrDepositNotFound, "no %s deposit found for %s", amount.Denom, depositor)
|
||||
}
|
||||
if !deposit.Amount.IsGTE(amount) {
|
||||
return sdkerrors.Wrapf(types.ErrInvaliWithdrawAmount, "%s>%s", amount, deposit.Amount)
|
||||
return sdkerrors.Wrapf(types.ErrInvalidWithdrawAmount, "%s>%s", amount, deposit.Amount)
|
||||
}
|
||||
|
||||
var err error
|
||||
switch depositType {
|
||||
case types.LP:
|
||||
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, sdk.NewCoins(amount))
|
||||
default:
|
||||
return sdkerrors.Wrap(types.ErrInvalidDepositType, string(depositType))
|
||||
}
|
||||
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, depositor, sdk.NewCoins(amount))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -101,7 +74,6 @@ func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.C
|
||||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositType, string(depositType)),
|
||||
),
|
||||
)
|
||||
|
||||
@ -111,7 +83,6 @@ func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.C
|
||||
types.EventTypeDeleteHarvestDeposit,
|
||||
sdk.NewAttribute(types.AttributeKeyDepositor, depositor.String()),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositDenom, amount.Denom),
|
||||
sdk.NewAttribute(types.AttributeKeyDepositType, string(depositType)),
|
||||
),
|
||||
)
|
||||
k.DeleteDeposit(ctx, deposit)
|
||||
@ -125,12 +96,8 @@ func (k Keeper) Withdraw(ctx sdk.Context, depositor sdk.AccAddress, amount sdk.C
|
||||
}
|
||||
|
||||
// GetTotalDeposited returns the total amount deposited for the input deposit type and deposit denom
|
||||
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositType types.DepositType, depositDenom string) (total sdk.Int) {
|
||||
|
||||
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositDenom string) (total sdk.Int) {
|
||||
var macc supplyExported.ModuleAccountI
|
||||
switch depositType {
|
||||
case types.LP:
|
||||
macc = k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
||||
}
|
||||
return macc.GetCoins().AmountOf(depositDenom)
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
depositType types.DepositType
|
||||
numberDeposits int
|
||||
expectedAccountBalance sdk.Coins
|
||||
expectedModAccountBalance sdk.Coins
|
||||
@ -37,7 +36,6 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
depositType: types.LP,
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
@ -52,7 +50,6 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
depositType: types.LP,
|
||||
numberDeposits: 2,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(800)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(200))),
|
||||
@ -62,27 +59,11 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
contains: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid deposit type",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
depositType: types.Stake,
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
contains: "invalid deposit type",
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid deposit denom",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("btcb", sdk.NewInt(100)),
|
||||
depositType: types.LP,
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
@ -97,7 +78,6 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000)),
|
||||
depositType: types.LP,
|
||||
numberDeposits: 1,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
@ -141,7 +121,7 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
// run the test
|
||||
var err error
|
||||
for i := 0; i < tc.args.numberDeposits; i++ {
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.amount, tc.args.depositType)
|
||||
err = suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.amount)
|
||||
}
|
||||
|
||||
// verify results
|
||||
@ -151,7 +131,7 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.amount.Denom, tc.args.depositType)
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.amount.Denom)
|
||||
suite.Require().True(f)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
@ -166,8 +146,6 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
depositor sdk.AccAddress
|
||||
depositAmount sdk.Coin
|
||||
withdrawAmount sdk.Coin
|
||||
depositType types.DepositType
|
||||
withdrawType types.DepositType
|
||||
createDeposit bool
|
||||
expectedAccountBalance sdk.Coins
|
||||
expectedModAccountBalance sdk.Coins
|
||||
@ -190,8 +168,6 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(100)),
|
||||
depositType: types.LP,
|
||||
withdrawType: types.LP,
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(900)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
||||
@ -209,8 +185,6 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
depositType: types.LP,
|
||||
withdrawType: types.LP,
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(1000)), sdk.NewCoin("btcb", sdk.NewInt(1000))),
|
||||
expectedModAccountBalance: sdk.Coins(nil),
|
||||
@ -228,27 +202,6 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("btcb", sdk.NewInt(200)),
|
||||
depositType: types.LP,
|
||||
withdrawType: types.LP,
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
depositExists: false,
|
||||
finalDepositAmount: sdk.Coin{},
|
||||
},
|
||||
errArgs{
|
||||
expectPass: false,
|
||||
contains: "deposit not found",
|
||||
},
|
||||
},
|
||||
{
|
||||
"deposit not found invalid deposit type",
|
||||
args{
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
depositType: types.LP,
|
||||
withdrawType: types.Stake,
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
@ -266,8 +219,6 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
depositor: sdk.AccAddress(crypto.AddressHash([]byte("test"))),
|
||||
depositAmount: sdk.NewCoin("bnb", sdk.NewInt(200)),
|
||||
withdrawAmount: sdk.NewCoin("bnb", sdk.NewInt(300)),
|
||||
depositType: types.LP,
|
||||
withdrawType: types.LP,
|
||||
createDeposit: true,
|
||||
expectedAccountBalance: sdk.Coins{},
|
||||
expectedModAccountBalance: sdk.Coins{},
|
||||
@ -311,11 +262,11 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
suite.keeper = keeper
|
||||
|
||||
if tc.args.createDeposit {
|
||||
err := suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.depositAmount, tc.args.depositType)
|
||||
err := suite.keeper.Deposit(suite.ctx, tc.args.depositor, tc.args.depositAmount)
|
||||
suite.Require().NoError(err)
|
||||
}
|
||||
|
||||
err := suite.keeper.Withdraw(suite.ctx, tc.args.depositor, tc.args.withdrawAmount, tc.args.withdrawType)
|
||||
err := suite.keeper.Withdraw(suite.ctx, tc.args.depositor, tc.args.withdrawAmount)
|
||||
|
||||
if tc.errArgs.expectPass {
|
||||
suite.Require().NoError(err)
|
||||
@ -323,7 +274,7 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
||||
suite.Require().Equal(tc.args.expectedAccountBalance, acc.GetCoins())
|
||||
mAcc := suite.getModuleAccount(types.ModuleAccountName)
|
||||
suite.Require().Equal(tc.args.expectedModAccountBalance, mAcc.GetCoins())
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.depositAmount.Denom, tc.args.depositType)
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, tc.args.depositor, tc.args.depositAmount.Denom)
|
||||
if tc.args.depositExists {
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(tc.args.finalDepositAmount, testDeposit.Amount)
|
||||
|
@ -75,10 +75,10 @@ func (k Keeper) SetPreviousDelegationDistribution(ctx sdk.Context, distTime time
|
||||
store.Set([]byte(denom), k.cdc.MustMarshalBinaryBare(distTime))
|
||||
}
|
||||
|
||||
// GetDeposit returns a deposit from the store for a particular depositor address, deposit denom, and deposit type
|
||||
func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress, denom string, depositType types.DepositType) (types.Deposit, bool) {
|
||||
// GetDeposit returns a deposit from the store for a particular depositor address, deposit denom
|
||||
func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress, denom string) (types.Deposit, bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
bz := store.Get(types.DepositKey(depositType, denom, depositor))
|
||||
bz := store.Get(types.DepositKey(denom, depositor))
|
||||
if bz == nil {
|
||||
return types.Deposit{}, false
|
||||
}
|
||||
@ -91,13 +91,13 @@ func (k Keeper) GetDeposit(ctx sdk.Context, depositor sdk.AccAddress, denom stri
|
||||
func (k Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
bz := k.cdc.MustMarshalBinaryBare(deposit)
|
||||
store.Set(types.DepositKey(deposit.Type, deposit.Amount.Denom, deposit.Depositor), bz)
|
||||
store.Set(types.DepositKey(deposit.Amount.Denom, deposit.Depositor), bz)
|
||||
}
|
||||
|
||||
// DeleteDeposit deletes a deposit from the store
|
||||
func (k Keeper) DeleteDeposit(ctx sdk.Context, deposit types.Deposit) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
store.Delete(types.DepositKey(deposit.Type, deposit.Amount.Denom, deposit.Depositor))
|
||||
store.Delete(types.DepositKey(deposit.Amount.Denom, deposit.Depositor))
|
||||
}
|
||||
|
||||
// IterateDeposits iterates over all deposit objects in the store and performs a callback function
|
||||
@ -114,11 +114,10 @@ func (k Keeper) IterateDeposits(ctx sdk.Context, cb func(deposit types.Deposit)
|
||||
}
|
||||
}
|
||||
|
||||
// IterateDepositsByTypeAndDenom iterates over all deposit objects in the store with the matching deposit type and deposit denom and performs a callback function
|
||||
func (k Keeper) IterateDepositsByTypeAndDenom(ctx sdk.Context, depositType types.DepositType, depositDenom string, cb func(deposit types.Deposit) (stop bool)) {
|
||||
|
||||
// IterateDepositsByDenom iterates over all deposit objects in the store with the matching deposit denom and performs a callback function
|
||||
func (k Keeper) IterateDepositsByDenom(ctx sdk.Context, depositDenom string, cb func(deposit types.Deposit) (stop bool)) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.DepositsKeyPrefix)
|
||||
iterator := sdk.KVStorePrefixIterator(store, types.DepositTypeIteratorKey(depositType, depositDenom))
|
||||
iterator := sdk.KVStorePrefixIterator(store, types.DepositTypeIteratorKey(depositDenom))
|
||||
defer iterator.Close()
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var deposit types.Deposit
|
||||
@ -129,10 +128,10 @@ func (k Keeper) IterateDepositsByTypeAndDenom(ctx sdk.Context, depositType types
|
||||
}
|
||||
}
|
||||
|
||||
// GetClaim returns a claim from the store for a particular claim owner, deposit denom, and deposit type
|
||||
func (k Keeper) GetClaim(ctx sdk.Context, owner sdk.AccAddress, depositDenom string, depositType types.DepositType) (types.Claim, bool) {
|
||||
// GetClaim returns a claim from the store for a particular claim owner, deposit denom, and claim type
|
||||
func (k Keeper) GetClaim(ctx sdk.Context, owner sdk.AccAddress, depositDenom string, claimType types.ClaimType) (types.Claim, bool) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimsKeyPrefix)
|
||||
bz := store.Get(types.ClaimKey(depositType, depositDenom, owner))
|
||||
bz := store.Get(types.ClaimKey(claimType, depositDenom, owner))
|
||||
if bz == nil {
|
||||
return types.Claim{}, false
|
||||
}
|
||||
@ -168,10 +167,10 @@ func (k Keeper) IterateClaims(ctx sdk.Context, cb func(claim types.Claim) (stop
|
||||
}
|
||||
}
|
||||
|
||||
// IterateClaimsByTypeAndDenom iterates over all claim objects in the store with the matching deposit type and deposit denom and performs a callback function
|
||||
func (k Keeper) IterateClaimsByTypeAndDenom(ctx sdk.Context, depositType types.DepositType, depositDenom string, cb func(claim types.Claim) (stop bool)) {
|
||||
// IterateClaimsByTypeAndDenom iterates over all claim objects in the store with the matching claim type and deposit denom and performs a callback function
|
||||
func (k Keeper) IterateClaimsByTypeAndDenom(ctx sdk.Context, claimType types.ClaimType, depositDenom string, cb func(claim types.Claim) (stop bool)) {
|
||||
store := prefix.NewStore(ctx.KVStore(k.key), types.ClaimsKeyPrefix)
|
||||
iterator := sdk.KVStorePrefixIterator(store, types.DepositTypeIteratorKey(depositType, depositDenom))
|
||||
iterator := sdk.KVStorePrefixIterator(store, types.ClaimTypeIteratorKey(claimType, depositDenom))
|
||||
defer iterator.Close()
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var claim types.Claim
|
||||
|
@ -72,27 +72,27 @@ func (suite *KeeperTestSuite) TestGetSetPreviousDelegatorDistribution() {
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() {
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(100)), "lp")
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb", "lp")
|
||||
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
suite.Require().False(f)
|
||||
|
||||
suite.keeper.SetDeposit(suite.ctx, dep)
|
||||
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb", "lp")
|
||||
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(dep, testDeposit)
|
||||
|
||||
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
|
||||
|
||||
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb", "lp")
|
||||
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"), "bnb")
|
||||
suite.Require().False(f)
|
||||
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestIterateDeposits() {
|
||||
for i := 0; i < 5; i++ {
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)), "lp")
|
||||
dep := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
|
||||
}
|
||||
var deposits []types.Deposit
|
||||
@ -103,39 +103,39 @@ func (suite *KeeperTestSuite) TestIterateDeposits() {
|
||||
suite.Require().Equal(5, len(deposits))
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestIterateDepositsByTypeAndDenom() {
|
||||
func (suite *KeeperTestSuite) TestIterateDepositsByDenom() {
|
||||
for i := 0; i < 5; i++ {
|
||||
depA := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)), "lp")
|
||||
depA := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depA) })
|
||||
depB := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)), "gov")
|
||||
depB := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("bnb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depB) })
|
||||
depC := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("btcb", sdk.NewInt(100)), "lp")
|
||||
depC := types.NewDeposit(sdk.AccAddress("test"+string(i)), sdk.NewCoin("btcb", sdk.NewInt(100)))
|
||||
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, depC) })
|
||||
}
|
||||
var bnbLPDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByTypeAndDenom(suite.ctx, "lp", "bnb", func(d types.Deposit) bool {
|
||||
bnbLPDeposits = append(bnbLPDeposits, d)
|
||||
|
||||
// Check BNB deposits
|
||||
var bnbDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByDenom(suite.ctx, "bnb", func(d types.Deposit) bool {
|
||||
bnbDeposits = append(bnbDeposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(5, len(bnbLPDeposits))
|
||||
var bnbGovDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByTypeAndDenom(suite.ctx, "gov", "bnb", func(d types.Deposit) bool {
|
||||
bnbGovDeposits = append(bnbGovDeposits, d)
|
||||
suite.Require().Equal(5, len(bnbDeposits))
|
||||
|
||||
// Check BTCB deposits
|
||||
var btcbDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByDenom(suite.ctx, "btcb", func(d types.Deposit) bool {
|
||||
btcbDeposits = append(btcbDeposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(5, len(bnbGovDeposits))
|
||||
var btcbLPDeposits []types.Deposit
|
||||
suite.keeper.IterateDepositsByTypeAndDenom(suite.ctx, "lp", "btcb", func(d types.Deposit) bool {
|
||||
btcbLPDeposits = append(btcbLPDeposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(5, len(btcbLPDeposits))
|
||||
suite.Require().Equal(5, len(btcbDeposits))
|
||||
|
||||
// Fetch all deposits
|
||||
var deposits []types.Deposit
|
||||
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
|
||||
deposits = append(deposits, d)
|
||||
return false
|
||||
})
|
||||
suite.Require().Equal(15, len(deposits))
|
||||
suite.Require().Equal(len(bnbDeposits)+len(btcbDeposits), len(deposits))
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGetSetDeleteClaim() {
|
||||
|
@ -84,60 +84,27 @@ func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte,
|
||||
}
|
||||
depositDenom := len(params.DepositDenom) > 0
|
||||
owner := len(params.Owner) > 0
|
||||
depositType := len(params.DepositType) > 0
|
||||
|
||||
var deposits []types.Deposit
|
||||
switch {
|
||||
case depositDenom && owner && depositType:
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, params.DepositDenom, params.DepositType)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
case depositDenom && owner:
|
||||
for _, dt := range types.DepositTypesDepositQuery {
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, params.DepositDenom, dt)
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, params.DepositDenom)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
}
|
||||
case depositDenom && depositType:
|
||||
k.IterateDepositsByTypeAndDenom(ctx, params.DepositType, params.DepositDenom, func(deposit types.Deposit) (stop bool) {
|
||||
deposits = append(deposits, deposit)
|
||||
return false
|
||||
})
|
||||
case owner && depositType:
|
||||
schedules := k.GetParams(ctx).LiquidityProviderSchedules
|
||||
for _, lps := range schedules {
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, lps.DepositDenom, params.DepositType)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
}
|
||||
case depositDenom:
|
||||
for _, dt := range types.DepositTypesDepositQuery {
|
||||
k.IterateDepositsByTypeAndDenom(ctx, dt, params.DepositDenom, func(deposit types.Deposit) (stop bool) {
|
||||
k.IterateDepositsByDenom(ctx, params.DepositDenom, func(deposit types.Deposit) (stop bool) {
|
||||
deposits = append(deposits, deposit)
|
||||
return false
|
||||
})
|
||||
}
|
||||
case owner:
|
||||
schedules := k.GetParams(ctx).LiquidityProviderSchedules
|
||||
for _, lps := range schedules {
|
||||
for _, dt := range types.DepositTypesDepositQuery {
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, lps.DepositDenom, dt)
|
||||
deposit, found := k.GetDeposit(ctx, params.Owner, lps.DepositDenom)
|
||||
if found {
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
}
|
||||
}
|
||||
case depositType:
|
||||
schedules := k.GetParams(ctx).LiquidityProviderSchedules
|
||||
for _, lps := range schedules {
|
||||
k.IterateDepositsByTypeAndDenom(ctx, params.DepositType, lps.DepositDenom, func(deposit types.Deposit) (stop bool) {
|
||||
deposits = append(deposits, deposit)
|
||||
return false
|
||||
})
|
||||
}
|
||||
default:
|
||||
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
||||
deposits = append(deposits, deposit)
|
||||
@ -169,47 +136,47 @@ func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e
|
||||
}
|
||||
depositDenom := len(params.DepositDenom) > 0
|
||||
owner := len(params.Owner) > 0
|
||||
depositType := len(params.DepositType) > 0
|
||||
claimType := len(params.ClaimType) > 0
|
||||
|
||||
var claims []types.Claim
|
||||
switch {
|
||||
case depositDenom && owner && depositType:
|
||||
claim, found := k.GetClaim(ctx, params.Owner, params.DepositDenom, params.DepositType)
|
||||
case depositDenom && owner && claimType:
|
||||
claim, found := k.GetClaim(ctx, params.Owner, params.DepositDenom, params.ClaimType)
|
||||
if found {
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
case depositDenom && owner:
|
||||
for _, dt := range types.DepositTypesClaimQuery {
|
||||
for _, dt := range types.ClaimTypesClaimQuery {
|
||||
claim, found := k.GetClaim(ctx, params.Owner, params.DepositDenom, dt)
|
||||
if found {
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
}
|
||||
case depositDenom && depositType:
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.DepositType, params.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
case depositDenom && claimType:
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, params.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
claims = append(claims, claim)
|
||||
return false
|
||||
})
|
||||
case owner && depositType:
|
||||
case owner && claimType:
|
||||
harvestParams := k.GetParams(ctx)
|
||||
switch {
|
||||
case params.DepositType == types.LP:
|
||||
case params.ClaimType == types.LP:
|
||||
for _, lps := range harvestParams.LiquidityProviderSchedules {
|
||||
claim, found := k.GetClaim(ctx, params.Owner, lps.DepositDenom, params.DepositType)
|
||||
claim, found := k.GetClaim(ctx, params.Owner, lps.DepositDenom, params.ClaimType)
|
||||
if found {
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
}
|
||||
case params.DepositType == types.Stake:
|
||||
case params.ClaimType == types.Stake:
|
||||
for _, dss := range harvestParams.DelegatorDistributionSchedules {
|
||||
claim, found := k.GetClaim(ctx, params.Owner, dss.DistributionSchedule.DepositDenom, params.DepositType)
|
||||
claim, found := k.GetClaim(ctx, params.Owner, dss.DistributionSchedule.DepositDenom, params.ClaimType)
|
||||
if found {
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
}
|
||||
}
|
||||
case depositDenom:
|
||||
for _, dt := range types.DepositTypesClaimQuery {
|
||||
for _, dt := range types.ClaimTypesClaimQuery {
|
||||
k.IterateClaimsByTypeAndDenom(ctx, dt, params.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
claims = append(claims, claim)
|
||||
return false
|
||||
@ -229,16 +196,16 @@ func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
}
|
||||
case depositType:
|
||||
case claimType:
|
||||
harvestParams := k.GetParams(ctx)
|
||||
for _, lps := range harvestParams.LiquidityProviderSchedules {
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.DepositType, lps.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, lps.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
claims = append(claims, claim)
|
||||
return false
|
||||
})
|
||||
}
|
||||
for _, dds := range harvestParams.DelegatorDistributionSchedules {
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.DepositType, dds.DistributionSchedule.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, dds.DistributionSchedule.DepositDenom, func(claim types.Claim) (stop bool) {
|
||||
claims = append(claims, claim)
|
||||
return false
|
||||
})
|
||||
|
@ -34,7 +34,7 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
|
||||
if lps.Start.After(ctx.BlockTime()) {
|
||||
continue
|
||||
}
|
||||
totalDeposited := k.GetTotalDeposited(ctx, types.LP, lps.DepositDenom)
|
||||
totalDeposited := k.GetTotalDeposited(ctx, lps.DepositDenom)
|
||||
if totalDeposited.IsZero() {
|
||||
continue
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
|
||||
continue
|
||||
}
|
||||
rewardsDistributed := sdk.ZeroInt()
|
||||
k.IterateDepositsByTypeAndDenom(ctx, types.LP, lps.DepositDenom, func(dep types.Deposit) (stop bool) {
|
||||
k.IterateDepositsByDenom(ctx, lps.DepositDenom, func(dep types.Deposit) (stop bool) {
|
||||
rewardsShare := sdk.NewDecFromInt(dep.Amount.Amount).Quo(sdk.NewDecFromInt(totalDeposited))
|
||||
if rewardsShare.IsZero() {
|
||||
return false
|
||||
@ -52,7 +52,7 @@ func (k Keeper) ApplyDepositRewards(ctx sdk.Context) {
|
||||
if rewardsEarned.IsZero() {
|
||||
return false
|
||||
}
|
||||
k.AddToClaim(ctx, dep.Depositor, dep.Amount.Denom, dep.Type, sdk.NewCoin(lps.RewardsPerSecond.Denom, rewardsEarned))
|
||||
k.AddToClaim(ctx, dep.Depositor, dep.Amount.Denom, types.LP, sdk.NewCoin(lps.RewardsPerSecond.Denom, rewardsEarned))
|
||||
rewardsDistributed = rewardsDistributed.Add(rewardsEarned)
|
||||
return false
|
||||
})
|
||||
@ -164,10 +164,10 @@ func (k Keeper) ApplyDelegationRewards(ctx sdk.Context, denom string) {
|
||||
}
|
||||
|
||||
// AddToClaim adds the input amount to an existing claim or creates a new one
|
||||
func (k Keeper) AddToClaim(ctx sdk.Context, owner sdk.AccAddress, depositDenom string, depositType types.DepositType, amountToAdd sdk.Coin) {
|
||||
claim, found := k.GetClaim(ctx, owner, depositDenom, depositType)
|
||||
func (k Keeper) AddToClaim(ctx sdk.Context, owner sdk.AccAddress, depositDenom string, claimType types.ClaimType, amountToAdd sdk.Coin) {
|
||||
claim, found := k.GetClaim(ctx, owner, depositDenom, claimType)
|
||||
if !found {
|
||||
claim = types.NewClaim(owner, depositDenom, amountToAdd, depositType)
|
||||
claim = types.NewClaim(owner, depositDenom, amountToAdd, claimType)
|
||||
} else {
|
||||
claim.Amount = claim.Amount.Add(amountToAdd)
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
depositAmount sdk.Coin
|
||||
totalDeposits sdk.Coin
|
||||
rewardRate sdk.Coin
|
||||
depositType types.DepositType
|
||||
claimType types.ClaimType
|
||||
previousBlockTime time.Time
|
||||
blockTime time.Time
|
||||
expectedClaimBalance sdk.Coin
|
||||
@ -47,7 +47,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
rewardRate: c("hard", 500),
|
||||
depositAmount: c("bnb", 100),
|
||||
totalDeposits: c("bnb", 1000),
|
||||
depositType: types.LP,
|
||||
claimType: types.LP,
|
||||
previousBlockTime: time.Date(2020, 11, 1, 13, 59, 50, 0, time.UTC),
|
||||
blockTime: time.Date(2020, 11, 1, 14, 0, 0, 0, time.UTC),
|
||||
expectedClaimBalance: c("hard", 500),
|
||||
@ -83,7 +83,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
supplyKeeper := tApp.GetSupplyKeeper()
|
||||
supplyKeeper.MintCoins(ctx, types.ModuleAccountName, cs(tc.args.totalDeposits))
|
||||
keeper := tApp.GetHarvestKeeper()
|
||||
deposit := types.NewDeposit(tc.args.depositor, tc.args.depositAmount, tc.args.depositType)
|
||||
deposit := types.NewDeposit(tc.args.depositor, tc.args.depositAmount)
|
||||
keeper.SetDeposit(ctx, deposit)
|
||||
suite.app = tApp
|
||||
suite.ctx = ctx
|
||||
@ -93,7 +93,7 @@ func (suite *KeeperTestSuite) TestApplyDepositRewards() {
|
||||
suite.Require().Panics(func() { suite.keeper.ApplyDepositRewards(suite.ctx) })
|
||||
} else {
|
||||
suite.Require().NotPanics(func() { suite.keeper.ApplyDepositRewards(suite.ctx) })
|
||||
claim, f := suite.keeper.GetClaim(suite.ctx, tc.args.depositor, tc.args.denom, tc.args.depositType)
|
||||
claim, f := suite.keeper.GetClaim(suite.ctx, tc.args.depositor, tc.args.denom, tc.args.claimType)
|
||||
suite.Require().True(f)
|
||||
suite.Require().Equal(tc.args.expectedClaimBalance, claim.Amount)
|
||||
}
|
||||
|
391
x/harvest/legacy/v0_12/types.go
Normal file
391
x/harvest/legacy/v0_12/types.go
Normal file
@ -0,0 +1,391 @@
|
||||
package v0_12
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
||||
"github.com/cosmos/cosmos-sdk/x/params"
|
||||
|
||||
cdptypes "github.com/kava-labs/kava/x/cdp/types"
|
||||
)
|
||||
|
||||
// Parameter keys and default values
|
||||
var (
|
||||
KeyActive = []byte("Active")
|
||||
KeyLPSchedules = []byte("LPSchedules")
|
||||
KeyDelegatorSchedule = []byte("DelegatorSchedule")
|
||||
DefaultActive = true
|
||||
DefaultGovSchedules = DistributionSchedules{}
|
||||
DefaultLPSchedules = DistributionSchedules{}
|
||||
DefaultDelegatorSchedules = DelegatorDistributionSchedules{}
|
||||
GovDenom = cdptypes.DefaultGovDenom
|
||||
)
|
||||
|
||||
// Params governance parameters for harvest module
|
||||
type Params struct {
|
||||
Active bool `json:"active" yaml:"active"`
|
||||
LiquidityProviderSchedules DistributionSchedules `json:"liquidity_provider_schedules" yaml:"liquidity_provider_schedules"`
|
||||
DelegatorDistributionSchedules DelegatorDistributionSchedules `json:"delegator_distribution_schedules" yaml:"delegator_distribution_schedules"`
|
||||
}
|
||||
|
||||
// DistributionSchedule distribution schedule for liquidity providers
|
||||
type DistributionSchedule struct {
|
||||
Active bool `json:"active" yaml:"active"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
Start time.Time `json:"start" yaml:"start"`
|
||||
End time.Time `json:"end" yaml:"end"`
|
||||
RewardsPerSecond sdk.Coin `json:"rewards_per_second" yaml:"rewards_per_second"`
|
||||
ClaimEnd time.Time `json:"claim_end" yaml:"claim_end"`
|
||||
ClaimMultipliers Multipliers `json:"claim_multipliers" yaml:"claim_multipliers"`
|
||||
}
|
||||
|
||||
// NewDistributionSchedule returns a new DistributionSchedule
|
||||
func NewDistributionSchedule(active bool, denom string, start, end time.Time, reward sdk.Coin, claimEnd time.Time, multipliers Multipliers) DistributionSchedule {
|
||||
return DistributionSchedule{
|
||||
Active: active,
|
||||
DepositDenom: denom,
|
||||
Start: start,
|
||||
End: end,
|
||||
RewardsPerSecond: reward,
|
||||
ClaimEnd: claimEnd,
|
||||
ClaimMultipliers: multipliers,
|
||||
}
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (ds DistributionSchedule) String() string {
|
||||
return fmt.Sprintf(`Liquidity Provider Distribution Schedule:
|
||||
Deposit Denom: %s,
|
||||
Start: %s,
|
||||
End: %s,
|
||||
Rewards Per Second: %s,
|
||||
Claim End: %s,
|
||||
Active: %t
|
||||
`, ds.DepositDenom, ds.Start, ds.End, ds.RewardsPerSecond, ds.ClaimEnd, ds.Active)
|
||||
}
|
||||
|
||||
// Validate performs a basic check of a distribution schedule.
|
||||
func (ds DistributionSchedule) Validate() error {
|
||||
if !ds.RewardsPerSecond.IsValid() {
|
||||
return fmt.Errorf("invalid reward coins %s for %s", ds.RewardsPerSecond, ds.DepositDenom)
|
||||
}
|
||||
if !ds.RewardsPerSecond.IsPositive() {
|
||||
return fmt.Errorf("reward amount must be positive, is %s for %s", ds.RewardsPerSecond, ds.DepositDenom)
|
||||
}
|
||||
if ds.RewardsPerSecond.Denom != "hard" {
|
||||
return fmt.Errorf("reward denom should be hard, is %s", ds.RewardsPerSecond.Denom)
|
||||
}
|
||||
if ds.Start.IsZero() {
|
||||
return errors.New("reward period start time cannot be 0")
|
||||
}
|
||||
if ds.End.IsZero() {
|
||||
return errors.New("reward period end time cannot be 0")
|
||||
}
|
||||
if ds.Start.After(ds.End) {
|
||||
return fmt.Errorf("end period time %s cannot be before start time %s", ds.End, ds.Start)
|
||||
}
|
||||
if ds.ClaimEnd.Before(ds.End) {
|
||||
return fmt.Errorf("claim end time %s cannot be before end time %s", ds.ClaimEnd, ds.End)
|
||||
}
|
||||
for _, multiplier := range ds.ClaimMultipliers {
|
||||
if err := multiplier.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DistributionSchedules slice of DistributionSchedule
|
||||
type DistributionSchedules []DistributionSchedule
|
||||
|
||||
// Validate checks if all the LiquidityProviderSchedules are valid and there are no duplicated
|
||||
// entries.
|
||||
func (dss DistributionSchedules) Validate() error {
|
||||
seenPeriods := make(map[string]bool)
|
||||
for _, ds := range dss {
|
||||
if seenPeriods[ds.DepositDenom] {
|
||||
return fmt.Errorf("duplicated distribution provider schedule with deposit denom %s", ds.DepositDenom)
|
||||
}
|
||||
|
||||
if err := ds.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
seenPeriods[ds.DepositDenom] = true
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (dss DistributionSchedules) String() string {
|
||||
out := "Distribution Schedules\n"
|
||||
for _, ds := range dss {
|
||||
out += fmt.Sprintf("%s\n", ds)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// DelegatorDistributionSchedule distribution schedule for delegators
|
||||
type DelegatorDistributionSchedule struct {
|
||||
DistributionSchedule DistributionSchedule `json:"distribution_schedule" yaml:"distribution_schedule"`
|
||||
|
||||
DistributionFrequency time.Duration `json:"distribution_frequency" yaml:"distribution_frequency"`
|
||||
}
|
||||
|
||||
// NewDelegatorDistributionSchedule returns a new DelegatorDistributionSchedule
|
||||
func NewDelegatorDistributionSchedule(ds DistributionSchedule, frequency time.Duration) DelegatorDistributionSchedule {
|
||||
return DelegatorDistributionSchedule{
|
||||
DistributionSchedule: ds,
|
||||
DistributionFrequency: frequency,
|
||||
}
|
||||
}
|
||||
|
||||
// Validate performs a basic check of a reward fields.
|
||||
func (dds DelegatorDistributionSchedule) Validate() error {
|
||||
if err := dds.DistributionSchedule.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if dds.DistributionFrequency <= 0 {
|
||||
return fmt.Errorf("distribution frequency should be positive, got %d", dds.DistributionFrequency)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelegatorDistributionSchedules slice of DelegatorDistributionSchedule
|
||||
type DelegatorDistributionSchedules []DelegatorDistributionSchedule
|
||||
|
||||
// Validate checks if all the LiquidityProviderSchedules are valid and there are no duplicated
|
||||
// entries.
|
||||
func (dds DelegatorDistributionSchedules) Validate() error {
|
||||
seenPeriods := make(map[string]bool)
|
||||
for _, ds := range dds {
|
||||
if seenPeriods[ds.DistributionSchedule.DepositDenom] {
|
||||
return fmt.Errorf("duplicated liquidity provider schedule with deposit denom %s", ds.DistributionSchedule.DepositDenom)
|
||||
}
|
||||
|
||||
if err := ds.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
seenPeriods[ds.DistributionSchedule.DepositDenom] = true
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Multiplier amount the claim rewards get increased by, along with how long the claim rewards are locked
|
||||
type Multiplier struct {
|
||||
Name MultiplierName `json:"name" yaml:"name"`
|
||||
MonthsLockup int64 `json:"months_lockup" yaml:"months_lockup"`
|
||||
Factor sdk.Dec `json:"factor" yaml:"factor"`
|
||||
}
|
||||
|
||||
// NewMultiplier returns a new Multiplier
|
||||
func NewMultiplier(name MultiplierName, lockup int64, factor sdk.Dec) Multiplier {
|
||||
return Multiplier{
|
||||
Name: name,
|
||||
MonthsLockup: lockup,
|
||||
Factor: factor,
|
||||
}
|
||||
}
|
||||
|
||||
// Validate multiplier param
|
||||
func (m Multiplier) Validate() error {
|
||||
if err := m.Name.IsValid(); err != nil {
|
||||
return err
|
||||
}
|
||||
if m.MonthsLockup < 0 {
|
||||
return fmt.Errorf("expected non-negative lockup, got %d", m.MonthsLockup)
|
||||
}
|
||||
if m.Factor.IsNegative() {
|
||||
return fmt.Errorf("expected non-negative factor, got %s", m.Factor.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMultiplier returns the named multiplier from the input distribution schedule
|
||||
func (ds DistributionSchedule) GetMultiplier(name MultiplierName) (Multiplier, bool) {
|
||||
for _, multiplier := range ds.ClaimMultipliers {
|
||||
if multiplier.Name == name {
|
||||
return multiplier, true
|
||||
}
|
||||
}
|
||||
return Multiplier{}, false
|
||||
}
|
||||
|
||||
// Multipliers slice of Multiplier
|
||||
type Multipliers []Multiplier
|
||||
|
||||
// NewParams returns a new params object
|
||||
func NewParams(active bool, lps DistributionSchedules, dds DelegatorDistributionSchedules) Params {
|
||||
return Params{
|
||||
Active: active,
|
||||
LiquidityProviderSchedules: lps,
|
||||
DelegatorDistributionSchedules: dds,
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultParams returns default params for harvest module
|
||||
func DefaultParams() Params {
|
||||
return NewParams(DefaultActive, DefaultLPSchedules, DefaultDelegatorSchedules)
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (p Params) String() string {
|
||||
return fmt.Sprintf(`Params:
|
||||
Active: %t
|
||||
Liquidity Provider Distribution Schedules %s
|
||||
Delegator Distribution Schedule %s`, p.Active, p.LiquidityProviderSchedules, p.DelegatorDistributionSchedules)
|
||||
}
|
||||
|
||||
// ParamKeyTable Key declaration for parameters
|
||||
func ParamKeyTable() params.KeyTable {
|
||||
return params.NewKeyTable().RegisterParamSet(&Params{})
|
||||
}
|
||||
|
||||
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
|
||||
func (p *Params) ParamSetPairs() params.ParamSetPairs {
|
||||
return params.ParamSetPairs{
|
||||
params.NewParamSetPair(KeyActive, &p.Active, validateActiveParam),
|
||||
params.NewParamSetPair(KeyLPSchedules, &p.LiquidityProviderSchedules, validateLPParams),
|
||||
params.NewParamSetPair(KeyDelegatorSchedule, &p.DelegatorDistributionSchedules, validateDelegatorParams),
|
||||
}
|
||||
}
|
||||
|
||||
// Validate checks that the parameters have valid values.
|
||||
func (p Params) Validate() error {
|
||||
if err := validateActiveParam(p.Active); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := validateDelegatorParams(p.DelegatorDistributionSchedules); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return validateLPParams(p.LiquidityProviderSchedules)
|
||||
}
|
||||
|
||||
func validateActiveParam(i interface{}) error {
|
||||
_, ok := i.(bool)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid parameter type: %T", i)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateLPParams(i interface{}) error {
|
||||
dss, ok := i.(DistributionSchedules)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid parameter type: %T", i)
|
||||
}
|
||||
|
||||
for _, ds := range dss {
|
||||
err := ds.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDelegatorParams(i interface{}) error {
|
||||
dds, ok := i.(DelegatorDistributionSchedules)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid parameter type: %T", i)
|
||||
}
|
||||
|
||||
return dds.Validate()
|
||||
}
|
||||
|
||||
// MultiplierName name for valid multiplier
|
||||
type MultiplierName string
|
||||
|
||||
// DepositType type for valid deposit type strings
|
||||
type DepositType string
|
||||
|
||||
// Valid reward multipliers and reward types
|
||||
const (
|
||||
Small MultiplierName = "small"
|
||||
Medium MultiplierName = "medium"
|
||||
Large MultiplierName = "large"
|
||||
|
||||
LP DepositType = "lp"
|
||||
Stake DepositType = "stake"
|
||||
)
|
||||
|
||||
// Queryable deposit types
|
||||
var (
|
||||
DepositTypesDepositQuery = []DepositType{LP}
|
||||
DepositTypesClaimQuery = []DepositType{LP, Stake}
|
||||
)
|
||||
|
||||
// IsValid checks if the input is one of the expected strings
|
||||
func (mn MultiplierName) IsValid() error {
|
||||
switch mn {
|
||||
case Small, Medium, Large:
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid multiplier name: %s", mn)
|
||||
}
|
||||
|
||||
// IsValid checks if the input is one of the expected strings
|
||||
func (dt DepositType) IsValid() error {
|
||||
switch dt {
|
||||
case LP, Stake:
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid deposit type: %s", dt)
|
||||
}
|
||||
|
||||
// Deposit defines an amount of coins deposited into a harvest module account
|
||||
type Deposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Type DepositType `json:"type" yaml:"type"`
|
||||
}
|
||||
|
||||
// NewDeposit returns a new deposit
|
||||
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coin, dtype DepositType) Deposit {
|
||||
return Deposit{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
Type: dtype,
|
||||
}
|
||||
}
|
||||
|
||||
// Claim defines an amount of coins that the owner can claim
|
||||
type Claim struct {
|
||||
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Type DepositType `json:"type" yaml:"type"`
|
||||
}
|
||||
|
||||
// NewClaim returns a new claim
|
||||
func NewClaim(owner sdk.AccAddress, denom string, amount sdk.Coin, dtype DepositType) Claim {
|
||||
return Claim{
|
||||
Owner: owner,
|
||||
DepositDenom: denom,
|
||||
Amount: amount,
|
||||
Type: dtype,
|
||||
}
|
||||
}
|
||||
|
||||
// NewPeriod returns a new vesting period
|
||||
func NewPeriod(amount sdk.Coins, length int64) vesting.Period {
|
||||
return vesting.Period{Amount: amount, Length: length}
|
||||
}
|
||||
|
||||
// GetTotalVestingPeriodLength returns the summed length of all vesting periods
|
||||
func GetTotalVestingPeriodLength(periods vesting.Periods) int64 {
|
||||
length := int64(0)
|
||||
for _, period := range periods {
|
||||
length += period.Length
|
||||
}
|
||||
return length
|
||||
}
|
@ -27,7 +27,7 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
cdc := makeTestCodec()
|
||||
|
||||
prevBlockTime := time.Now().UTC()
|
||||
deposit := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(1)), "lp")
|
||||
deposit := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoin("bnb", sdk.NewInt(1)))
|
||||
claim := types.NewClaim(sdk.AccAddress("test"), "bnb", sdk.NewCoin("hard", sdk.NewInt(100)), "stake")
|
||||
|
||||
kvPairs := kv.Pairs{
|
||||
|
@ -11,14 +11,12 @@ There are three messages in the harvest module. Deposit allows users to deposit
|
||||
type MsgDeposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
// MsgWithdraw withdraw from the harvest module.
|
||||
type MsgWithdraw struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
// MsgClaimReward message type used to claim HARD tokens
|
||||
|
@ -17,7 +17,6 @@ The harvest module emits the following events:
|
||||
| harvest_deposit | amount | `{amount}` |
|
||||
| harvest_deposit | depositor | `{depositor address}` |
|
||||
| harvest_deposit | deposit_denom | `{deposit denom}` |
|
||||
| harvest_deposit | deposit_type | `{deposit type}` |
|
||||
|
||||
### MsgWithdraw
|
||||
|
||||
@ -31,7 +30,6 @@ The harvest module emits the following events:
|
||||
| harvest_deposit | deposit_type | `{deposit type}` |
|
||||
| delete_harvest_deposit | depositor | `{depositor address}` |
|
||||
| delete_harvest_deposit | deposit_denom | `{deposit denom}` |
|
||||
| delete_harvest_deposit | deposit_type | `{deposit type}` |
|
||||
|
||||
### MsgClaimReward
|
||||
|
||||
@ -42,7 +40,7 @@ The harvest module emits the following events:
|
||||
| claim_harvest_reward | amount | `{amount}` |
|
||||
| claim_harvest_reward | claim_holder | `{claim holder address}` |
|
||||
| claim_harvest_reward | deposit_denom | `{deposit denom}` |
|
||||
| claim_harvest_reward | deposit_type | `{deposit type}` |
|
||||
| claim_harvest_reward | claim_type | `{claim type}` |
|
||||
| claim_harvest_reward | claim_multiplier | `{claim multiplier}` |
|
||||
|
||||
## BeginBlock
|
||||
|
@ -9,15 +9,15 @@ type Claim struct {
|
||||
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Type DepositType `json:"type" yaml:"type"`
|
||||
Type ClaimType `json:"claim_type" yaml:"claim_type"`
|
||||
}
|
||||
|
||||
// NewClaim returns a new claim
|
||||
func NewClaim(owner sdk.AccAddress, denom string, amount sdk.Coin, dtype DepositType) Claim {
|
||||
func NewClaim(owner sdk.AccAddress, denom string, amount sdk.Coin, claimType ClaimType) Claim {
|
||||
return Claim{
|
||||
Owner: owner,
|
||||
DepositDenom: denom,
|
||||
Amount: amount,
|
||||
Type: dtype,
|
||||
Type: claimType,
|
||||
}
|
||||
}
|
||||
|
@ -8,14 +8,12 @@ import (
|
||||
type Deposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
Type DepositType `json:"type" yaml:"type"`
|
||||
}
|
||||
|
||||
// NewDeposit returns a new deposit
|
||||
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coin, dtype DepositType) Deposit {
|
||||
func NewDeposit(depositor sdk.AccAddress, amount sdk.Coin) Deposit {
|
||||
return Deposit{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
Type: dtype,
|
||||
}
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ var (
|
||||
ErrInvalidDepositDenom = sdkerrors.Register(ModuleName, 2, "invalid deposit denom")
|
||||
// ErrDepositNotFound error for deposit not found
|
||||
ErrDepositNotFound = sdkerrors.Register(ModuleName, 3, "deposit not found")
|
||||
// ErrInvaliWithdrawAmount error for invalid withdrawal amount
|
||||
ErrInvaliWithdrawAmount = sdkerrors.Register(ModuleName, 4, "withdrawal amount exceeds deposit amount")
|
||||
// ErrInvalidDepositType error for invalid deposit type
|
||||
ErrInvalidDepositType = sdkerrors.Register(ModuleName, 5, "invalid deposit type")
|
||||
// ErrInvalidWithdrawAmount error for invalid withdrawal amount
|
||||
ErrInvalidWithdrawAmount = sdkerrors.Register(ModuleName, 4, "withdrawal amount exceeds deposit amount")
|
||||
// ErrInvalidClaimType error for invalid claim type
|
||||
ErrInvalidClaimType = sdkerrors.Register(ModuleName, 5, "invalid claim type")
|
||||
// ErrClaimNotFound error for claim not found
|
||||
ErrClaimNotFound = sdkerrors.Register(ModuleName, 6, "claim not found")
|
||||
// ErrZeroClaim error for claim amount rounded to zero
|
||||
|
@ -13,9 +13,9 @@ const (
|
||||
AttributeKeyBlockHeight = "block_height"
|
||||
AttributeKeyRewardsDistribution = "rewards_distributed"
|
||||
AttributeKeyDeposit = "deposit"
|
||||
AttributeKeyDepositType = "deposit_type"
|
||||
AttributeKeyDepositDenom = "deposit_denom"
|
||||
AttributeKeyDepositor = "depositor"
|
||||
AttributeKeyClaimType = "claim_type"
|
||||
AttributeKeyClaimHolder = "claim_holder"
|
||||
AttributeKeyClaimAmount = "claim_amount"
|
||||
AttributeKeyClaimMultiplier = "claim_multiplier"
|
||||
|
@ -41,20 +41,25 @@ var (
|
||||
)
|
||||
|
||||
// DepositKey key of a specific deposit in the store
|
||||
func DepositKey(depositType DepositType, denom string, depositor sdk.AccAddress) []byte {
|
||||
return createKey([]byte(depositType), sep, []byte(denom), sep, depositor)
|
||||
func DepositKey(denom string, depositor sdk.AccAddress) []byte {
|
||||
return createKey([]byte(denom), sep, depositor)
|
||||
}
|
||||
|
||||
// DepositTypeIteratorKey returns an interator prefix for interating over deposits by deposit type and denom
|
||||
func DepositTypeIteratorKey(depositType DepositType, denom string) []byte {
|
||||
return createKey([]byte(depositType), sep, []byte(denom))
|
||||
// DepositTypeIteratorKey returns an interator prefix for interating over deposits by deposit denom
|
||||
func DepositTypeIteratorKey(denom string) []byte {
|
||||
return createKey([]byte(denom))
|
||||
}
|
||||
|
||||
// ClaimKey key of a specific deposit in the store
|
||||
func ClaimKey(depositType DepositType, denom string, owner sdk.AccAddress) []byte {
|
||||
func ClaimKey(depositType ClaimType, denom string, owner sdk.AccAddress) []byte {
|
||||
return createKey([]byte(depositType), sep, []byte(denom), sep, owner)
|
||||
}
|
||||
|
||||
// ClaimTypeIteratorKey returns an interator prefix for interating over claims by deposit type and denom
|
||||
func ClaimTypeIteratorKey(depositType ClaimType, denom string) []byte {
|
||||
return createKey([]byte(depositType), sep, []byte(denom))
|
||||
}
|
||||
|
||||
func createKey(bytes ...[]byte) (r []byte) {
|
||||
for _, b := range bytes {
|
||||
r = append(r, b...)
|
||||
|
@ -11,8 +11,8 @@ import (
|
||||
// MultiplierName name for valid multiplier
|
||||
type MultiplierName string
|
||||
|
||||
// DepositType type for valid deposit type strings
|
||||
type DepositType string
|
||||
// ClaimType type for valid claim type strings
|
||||
type ClaimType string
|
||||
|
||||
// Valid reward multipliers and reward types
|
||||
const (
|
||||
@ -20,14 +20,13 @@ const (
|
||||
Medium MultiplierName = "medium"
|
||||
Large MultiplierName = "large"
|
||||
|
||||
LP DepositType = "lp"
|
||||
Stake DepositType = "stake"
|
||||
LP ClaimType = "lp"
|
||||
Stake ClaimType = "stake"
|
||||
)
|
||||
|
||||
// Queryable deposit types
|
||||
// Queryable claim types
|
||||
var (
|
||||
DepositTypesDepositQuery = []DepositType{LP}
|
||||
DepositTypesClaimQuery = []DepositType{LP, Stake}
|
||||
ClaimTypesClaimQuery = []ClaimType{LP, Stake}
|
||||
)
|
||||
|
||||
// IsValid checks if the input is one of the expected strings
|
||||
@ -40,12 +39,12 @@ func (mn MultiplierName) IsValid() error {
|
||||
}
|
||||
|
||||
// IsValid checks if the input is one of the expected strings
|
||||
func (dt DepositType) IsValid() error {
|
||||
func (dt ClaimType) IsValid() error {
|
||||
switch dt {
|
||||
case LP, Stake:
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid deposit type: %s", dt)
|
||||
return fmt.Errorf("invalid claim type: %s", dt)
|
||||
}
|
||||
|
||||
// ensure Msg interface compliance at compile time
|
||||
@ -60,15 +59,13 @@ var (
|
||||
type MsgDeposit struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
// NewMsgDeposit returns a new MsgDeposit
|
||||
func NewMsgDeposit(depositor sdk.AccAddress, amount sdk.Coin, depositType string) MsgDeposit {
|
||||
func NewMsgDeposit(depositor sdk.AccAddress, amount sdk.Coin) MsgDeposit {
|
||||
return MsgDeposit{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
DepositType: depositType,
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,7 +83,7 @@ func (msg MsgDeposit) ValidateBasic() error {
|
||||
if !msg.Amount.IsValid() || msg.Amount.IsZero() {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "deposit amount %s", msg.Amount)
|
||||
}
|
||||
return DepositType(strings.ToLower(msg.DepositType)).IsValid()
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSignBytes gets the canonical byte representation of the Msg.
|
||||
@ -105,23 +102,20 @@ func (msg MsgDeposit) String() string {
|
||||
return fmt.Sprintf(`Deposit Message:
|
||||
Depositor: %s
|
||||
Amount: %s
|
||||
Deposit Type: %s
|
||||
`, msg.Depositor, msg.Amount, msg.DepositType)
|
||||
`, msg.Depositor, msg.Amount)
|
||||
}
|
||||
|
||||
// MsgWithdraw withdraw from the harvest module.
|
||||
type MsgWithdraw struct {
|
||||
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
|
||||
Amount sdk.Coin `json:"amount" yaml:"amount"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
// NewMsgWithdraw returns a new MsgWithdraw
|
||||
func NewMsgWithdraw(depositor sdk.AccAddress, amount sdk.Coin, depositType string) MsgWithdraw {
|
||||
func NewMsgWithdraw(depositor sdk.AccAddress, amount sdk.Coin) MsgWithdraw {
|
||||
return MsgWithdraw{
|
||||
Depositor: depositor,
|
||||
Amount: amount,
|
||||
DepositType: depositType,
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,7 +133,7 @@ func (msg MsgWithdraw) ValidateBasic() error {
|
||||
if !msg.Amount.IsValid() || msg.Amount.IsZero() {
|
||||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "deposit amount %s", msg.Amount)
|
||||
}
|
||||
return DepositType(strings.ToLower(msg.DepositType)).IsValid()
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSignBytes gets the canonical byte representation of the Msg.
|
||||
@ -158,8 +152,7 @@ func (msg MsgWithdraw) String() string {
|
||||
return fmt.Sprintf(`Withdraw Message:
|
||||
Depositor: %s
|
||||
Amount: %s
|
||||
Deposit Type: %s
|
||||
`, msg.Depositor, msg.Amount, msg.DepositType)
|
||||
`, msg.Depositor, msg.Amount)
|
||||
}
|
||||
|
||||
// MsgClaimReward message type used to claim rewards
|
||||
@ -168,17 +161,17 @@ type MsgClaimReward struct {
|
||||
Receiver sdk.AccAddress `json:"receiver" yaml:"receiver"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
MultiplierName string `json:"multiplier_name" yaml:"multiplier_name"`
|
||||
DepositType string `json:"deposit_type" yaml:"deposit_type"`
|
||||
ClaimType string `json:"claim_type" yaml:"claim_type"`
|
||||
}
|
||||
|
||||
// NewMsgClaimReward returns a new MsgClaimReward.
|
||||
func NewMsgClaimReward(sender, receiver sdk.AccAddress, depositDenom, depositType, multiplier string) MsgClaimReward {
|
||||
func NewMsgClaimReward(sender, receiver sdk.AccAddress, depositDenom, claimType, multiplier string) MsgClaimReward {
|
||||
return MsgClaimReward{
|
||||
Sender: sender,
|
||||
Receiver: receiver,
|
||||
DepositDenom: depositDenom,
|
||||
MultiplierName: multiplier,
|
||||
DepositType: depositType,
|
||||
ClaimType: claimType,
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,7 +192,7 @@ func (msg MsgClaimReward) ValidateBasic() error {
|
||||
if err := sdk.ValidateDenom(msg.DepositDenom); err != nil {
|
||||
return fmt.Errorf("collateral type cannot be blank")
|
||||
}
|
||||
if err := DepositType(strings.ToLower(msg.DepositType)).IsValid(); err != nil {
|
||||
if err := ClaimType(strings.ToLower(msg.ClaimType)).IsValid(); err != nil {
|
||||
return err
|
||||
}
|
||||
return MultiplierName(strings.ToLower(msg.MultiplierName)).IsValid()
|
||||
|
@ -19,7 +19,6 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
depositType string
|
||||
}
|
||||
addrs := []sdk.AccAddress{
|
||||
sdk.AccAddress("test1"),
|
||||
@ -36,7 +35,6 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "lp",
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
@ -46,25 +44,14 @@ func (suite *MsgTestSuite) TestMsgDeposit() {
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "LP",
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
},
|
||||
{
|
||||
name: "invalid",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "cat",
|
||||
},
|
||||
expectPass: false,
|
||||
expectedErr: "invalid deposit type",
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
suite.Run(tc.name, func() {
|
||||
msg := types.NewMsgDeposit(tc.args.depositor, tc.args.amount, tc.args.depositType)
|
||||
msg := types.NewMsgDeposit(tc.args.depositor, tc.args.amount)
|
||||
err := msg.ValidateBasic()
|
||||
if tc.expectPass {
|
||||
suite.NoError(err)
|
||||
@ -80,7 +67,6 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
type args struct {
|
||||
depositor sdk.AccAddress
|
||||
amount sdk.Coin
|
||||
depositType string
|
||||
}
|
||||
addrs := []sdk.AccAddress{
|
||||
sdk.AccAddress("test1"),
|
||||
@ -97,7 +83,6 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "lp",
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
@ -107,25 +92,14 @@ func (suite *MsgTestSuite) TestMsgWithdraw() {
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "LP",
|
||||
},
|
||||
expectPass: true,
|
||||
expectedErr: "",
|
||||
},
|
||||
{
|
||||
name: "invalid",
|
||||
args: args{
|
||||
depositor: addrs[0],
|
||||
amount: sdk.NewCoin("bnb", sdk.NewInt(10000000)),
|
||||
depositType: "cat",
|
||||
},
|
||||
expectPass: false,
|
||||
expectedErr: "invalid deposit type",
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
suite.Run(tc.name, func() {
|
||||
msg := types.NewMsgWithdraw(tc.args.depositor, tc.args.amount, tc.args.depositType)
|
||||
msg := types.NewMsgWithdraw(tc.args.depositor, tc.args.amount)
|
||||
err := msg.ValidateBasic()
|
||||
if tc.expectPass {
|
||||
suite.NoError(err)
|
||||
@ -142,7 +116,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
sender sdk.AccAddress
|
||||
receiver sdk.AccAddress
|
||||
denom string
|
||||
depositType string
|
||||
claimType string
|
||||
multiplier string
|
||||
}
|
||||
addrs := []sdk.AccAddress{
|
||||
@ -161,7 +135,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
sender: addrs[0],
|
||||
receiver: addrs[0],
|
||||
denom: "bnb",
|
||||
depositType: "lp",
|
||||
claimType: "lp",
|
||||
multiplier: "large",
|
||||
},
|
||||
expectPass: true,
|
||||
@ -173,7 +147,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
sender: addrs[0],
|
||||
receiver: addrs[0],
|
||||
denom: "bnb",
|
||||
depositType: "stake",
|
||||
claimType: "stake",
|
||||
multiplier: "small",
|
||||
},
|
||||
expectPass: true,
|
||||
@ -185,7 +159,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
sender: addrs[0],
|
||||
receiver: addrs[1],
|
||||
denom: "bnb",
|
||||
depositType: "lp",
|
||||
claimType: "lp",
|
||||
multiplier: "Medium",
|
||||
},
|
||||
expectPass: true,
|
||||
@ -197,7 +171,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
sender: addrs[0],
|
||||
receiver: addrs[0],
|
||||
denom: "bnb",
|
||||
depositType: "lp",
|
||||
claimType: "lp",
|
||||
multiplier: "huge",
|
||||
},
|
||||
expectPass: false,
|
||||
@ -206,7 +180,7 @@ func (suite *MsgTestSuite) TestMsgClaim() {
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
suite.Run(tc.name, func() {
|
||||
msg := types.NewMsgClaimReward(tc.args.sender, tc.args.receiver, tc.args.denom, tc.args.depositType, tc.args.multiplier)
|
||||
msg := types.NewMsgClaimReward(tc.args.sender, tc.args.receiver, tc.args.denom, tc.args.claimType, tc.args.multiplier)
|
||||
err := msg.ValidateBasic()
|
||||
if tc.expectPass {
|
||||
suite.NoError(err)
|
||||
|
@ -20,17 +20,15 @@ type QueryDepositParams struct {
|
||||
Limit int `json:"limit" yaml:"limit"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
|
||||
DepositType DepositType `json:"deposit_type" yaml:"deposit_type"`
|
||||
}
|
||||
|
||||
// NewQueryDepositParams creates a new QueryDepositParams
|
||||
func NewQueryDepositParams(page, limit int, depositDenom string, owner sdk.AccAddress, depositType DepositType) QueryDepositParams {
|
||||
func NewQueryDepositParams(page, limit int, depositDenom string, owner sdk.AccAddress) QueryDepositParams {
|
||||
return QueryDepositParams{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
DepositDenom: depositDenom,
|
||||
Owner: owner,
|
||||
DepositType: depositType,
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,17 +38,17 @@ type QueryClaimParams struct {
|
||||
Limit int `json:"limit" yaml:"limit"`
|
||||
DepositDenom string `json:"deposit_denom" yaml:"deposit_denom"`
|
||||
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
|
||||
DepositType DepositType `json:"deposit_type" yaml:"deposit_type"`
|
||||
ClaimType ClaimType `json:"claim_type" yaml:"claim_type"`
|
||||
}
|
||||
|
||||
// NewQueryClaimParams creates a new QueryClaimParams
|
||||
func NewQueryClaimParams(page, limit int, depositDenom string, owner sdk.AccAddress, depositType DepositType) QueryClaimParams {
|
||||
func NewQueryClaimParams(page, limit int, depositDenom string, owner sdk.AccAddress, claimType ClaimType) QueryClaimParams {
|
||||
return QueryClaimParams{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
DepositDenom: depositDenom,
|
||||
Owner: owner,
|
||||
DepositType: depositType,
|
||||
ClaimType: claimType,
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user