Skip to content

Commit

Permalink
feat: Withdraw funds from campaign msg server
Browse files Browse the repository at this point in the history
  • Loading branch information
3eyedraga committed Nov 7, 2023
1 parent 9156b40 commit 10d30bf
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 141 deletions.
10 changes: 0 additions & 10 deletions proto/sge/reward/ticket.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ message WithdrawFundsPayload {
// promoter is the address of campaign promoter.
// Funds would be transferred to this account.
string promoter = 1;

// amount is the funds that needs to be withdrawn.
string amount = 8 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"amount\""
];

// is_active is the flag to check if the campaign is active or not.
bool is_active = 9;
}

message RewardPayloadCommon {
Expand Down
53 changes: 51 additions & 2 deletions x/reward/keeper/msg_server_campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
sdkmath "cosmossdk.io/math"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -92,7 +93,7 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam
return nil, sdkerrors.Wrap(sdkerrtypes.ErrKeyNotFound, "index not set")
}

// Checks if the the msg creator is the same as the current owner
// Checks if the msg creator is the same as the current owner
if msg.Creator != valFound.Promoter {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, valFound.Promoter, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
Expand All @@ -110,5 +111,53 @@ func (k msgServer) UpdateCampaign(goCtx context.Context, msg *types.MsgUpdateCam
}

func (k msgServer) WithdrawFunds(goCtx context.Context, msg *types.MsgWithdrawFunds) (*types.MsgWithdrawFundsResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)

var payload types.WithdrawFundsPayload
if err := k.ovmKeeper.VerifyTicketUnmarshal(goCtx, msg.Ticket, &payload); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

// Validate ticket payload
if err := payload.Validate(); err != nil {
return nil, err
}

// Check if the campaign exists
valFound, isFound := k.GetCampaign(ctx, msg.Uid)
if !isFound {
return nil, sdkerrors.Wrap(sdkerrtypes.ErrKeyNotFound, "campaign not found")
}

// Checks if the msg creator is the same as the current owner
if msg.Creator != valFound.Promoter {
if err := utils.ValidateMsgAuthorization(k.authzKeeper, ctx, msg.Creator, valFound.Promoter, msg,
types.ErrAuthorizationNotFound, types.ErrAuthorizationNotAccepted); err != nil {
return nil, err
}
}
// check if the pool amount is positive
if valFound.Pool.Total.IsNil() || !valFound.Pool.Total.GT(sdkmath.ZeroInt()) {
return nil, sdkerrors.Wrapf(types.ErrWithdrawFromCampaignPool, "pool amount should be positive")
}

// transfer the funds present in campaign to the promoter
if err := k.modFunder.Refund(
types.RewardPoolFunder{}, ctx,
sdk.MustAccAddressFromBech32(payload.Promoter),
valFound.Pool.Total,
); 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

// store the campaign
k.SetCampaign(ctx, valFound)
// emit withdraw event
msg.EmitEvent(&ctx, msg.Uid)

return &types.MsgWithdrawFundsResponse{}, nil
}
1 change: 1 addition & 0 deletions x/reward/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
ErrCampaignPoolBalance = sdkerrors.Register(ModuleName, 7106, "not enough campaign pool balance")
ErrUnknownRewardType = sdkerrors.Register(ModuleName, 7107, "unknown reward type")
ErrInFundingCampaignPool = sdkerrors.Register(ModuleName, 7108, "error in funding the campaign pool")
ErrWithdrawFromCampaignPool = sdkerrors.Register(ModuleName, 7109, "error in withdrawing from the campaign pool")
ErrUnknownAccType = sdkerrors.Register(ModuleName, 7109, "unknown account type")
ErrCampaignEnded = sdkerrors.Register(ModuleName, 7110, "campaign validity period is ended")
ErrInsufficientPoolBalance = sdkerrors.Register(ModuleName, 7111, "insufficient campaign pool balance")
Expand Down
9 changes: 9 additions & 0 deletions x/reward/types/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ func (payload *UpdateCampaignPayload) Validate(blockTime uint64) error {

return nil
}

// Validate validates campaign withdraw funds ticket payload.
func (payload *WithdrawFundsPayload) Validate() error {
_, err := sdk.AccAddressFromBech32(payload.Promoter)
if err != nil {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid promoter address (%s)", err)
}

}

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / build (arm64)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / build (amd64)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / tests (02)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / tests (02)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / test-race (03)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / tests (01)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

missing return (typecheck)

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

missing return) (typecheck)

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

missing return) (typecheck)

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

missing return) (typecheck)

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

missing return) (typecheck)

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / tests (03)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / test-race (00)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / tests (00)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / test-race (02)

missing return

Check failure on line 95 in x/reward/types/ticket.go

View workflow job for this annotation

GitHub Actions / test-race (01)

missing return
167 changes: 38 additions & 129 deletions x/reward/types/ticket.pb.go

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

0 comments on commit 10d30bf

Please sign in to comment.