Skip to content

Commit

Permalink
Merge pull request #378 from scorpioborn/feature/campaign-withdraw-am…
Browse files Browse the repository at this point in the history
…ount

Feature / Campaign custom withdraw amount
  • Loading branch information
3eyedraga authored Apr 2, 2024
2 parents 657f479 + 89a3007 commit b7d106e
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 111 deletions.
8 changes: 7 additions & 1 deletion proto/sge/reward/authz.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ message UpdateCampaignAuthorization {

// WithdrawCampaignAuthorization allows the grantee to withdraw remaining
// pool balance of the campaign from the granter's account.
message WithdrawCampaignAuthorization {}
message WithdrawCampaignAuthorization {
// withdraw_limit is the maximum limit of the withdrawal by authorization.
string withdraw_limit = 1 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}
5 changes: 5 additions & 0 deletions proto/sge/reward/campaign.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ message Pool {
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"spent\""
];
string withdrawn = 3 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"spent\""
];
}
5 changes: 5 additions & 0 deletions proto/sge/reward/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ message MsgWithdrawFunds {
string uid = 2;
// ticket is the payload data.
string ticket = 3;
// amount is the requested withdrawal amount
string amount = 4 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}

// MsgWithdrawFundsResponse withdraw funds message response type.
Expand Down
12 changes: 9 additions & 3 deletions x/reward/client/cli/tx_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,21 @@ func CmdUpdateCampaign() *cobra.Command {

func CmdWithdrawFunds() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-funds [uid] [ticket]",
Use: "withdraw-funds [uid] [amount] [ticket]",
Short: "Withdraw funds from a campaign",
Long: "Withdrawal of the funds from a certain campaign",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Get indexes
argUID := args[0]

argWithdrawAmountCosmosInt, ok := sdkmath.NewIntFromString(args[1])
if !ok {
return types.ErrInvalidFunds
}

// Get value arguments
argTicket := args[1]
argTicket := args[2]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -114,6 +119,7 @@ func CmdWithdrawFunds() *cobra.Command {
msg := types.NewMsgWithdrawFunds(
clientCtx.GetFromAddress().String(),
argUID,
argWithdrawAmountCosmosInt,
argTicket,
)
if err := msg.ValidateBasic(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/reward/keeper/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k Keeper) GetAllCampaign(ctx sdk.Context) (list []types.Campaign) {

func (k Keeper) UpdateCampaignPool(ctx sdk.Context, campaign types.Campaign, receiver types.Receiver) {
totalAmount := receiver.SubaccountAmount.Add(receiver.MainAccountAmount) // Fixme: Check if the logic is correct
campaign.Pool.Spent = campaign.Pool.Spent.Add(totalAmount)
campaign.Pool.Spend(totalAmount)

k.SetCampaign(ctx, campaign)
}
2 changes: 1 addition & 1 deletion x/reward/keeper/campaign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func createNCampaign(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Camp
items[i].RewardAmountType = types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED
items[i].IsActive = true
items[i].Meta = "campaign " + items[i].UID
items[i].Pool = types.Pool{Spent: sdk.ZeroInt(), Total: sdkmath.NewInt(100)}
items[i].Pool = types.Pool{Spent: sdk.ZeroInt(), Withdrawn: sdk.ZeroInt(), Total: sdkmath.NewInt(100)}

keeper.SetCampaign(ctx, items[i])
}
Expand Down
19 changes: 13 additions & 6 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam
return nil, sdkerrors.Wrapf(types.ErrInFundingCampaignPool, "%s", err)
}

campaign.Pool.Total = campaign.Pool.Total.Add(msg.TopupFunds)
campaign.Pool.TopUp(msg.TopupFunds)
}

campaign.EndTS = payload.EndTs
Expand Down Expand Up @@ -173,24 +173,31 @@ func (k msgServer) WithdrawFunds(goCtx context.Context, msg *types.MsgWithdrawFu
return nil, err
}
}
availableAmount := valFound.Pool.Total.Sub(valFound.Pool.Spent)
availableAmount := valFound.Pool.AvailableAmount()
// check if the pool amount is positive
if availableAmount.IsNil() || !availableAmount.GT(sdkmath.ZeroInt()) {
return nil, sdkerrors.Wrapf(types.ErrWithdrawFromCampaignPool, "pool amount should be positive")
}

if availableAmount.LT(msg.Amount) {
return nil, sdkerrors.Wrapf(types.ErrWithdrawFromCampaignPool, "not enough withdrawable balance %s", availableAmount)
}

// transfer the funds present in campaign to the promoter
if err := k.modFunder.Refund(
types.RewardPoolFunder{}, ctx,
sdk.MustAccAddressFromBech32(payload.Promoter),
availableAmount,
msg.Amount,
); err != nil {
return nil, sdkerrors.Wrapf(types.ErrWithdrawFromCampaignPool, "%s", err)
}
// set the pool amount to zero
valFound.Pool.Total = sdkmath.ZeroInt()
// deactivate the campaign
valFound.IsActive = false
valFound.Pool.Withdraw(msg.Amount)

if valFound.Pool.AvailableAmount().LTE(sdkmath.ZeroInt()) {
// deactivate the campaign
valFound.IsActive = false
}

// store the campaign
k.SetCampaign(ctx, valFound)
Expand Down
83 changes: 66 additions & 17 deletions x/reward/types/authz.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b7d106e

Please sign in to comment.