-
Notifications
You must be signed in to change notification settings - Fork 201
/
msg_server.go
130 lines (100 loc) · 3.33 KB
/
msg_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/Stride-Labs/stride/v12/x/claim/types"
)
type msgServer struct {
keeper Keeper
}
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}
var _ types.MsgServer = msgServer{}
func (server msgServer) SetAirdropAllocations(goCtx context.Context, msg *types.MsgSetAirdropAllocations) (*types.MsgSetAirdropAllocationsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
records := []types.ClaimRecord{}
totalWeight, err := server.keeper.GetTotalWeight(ctx, msg.AirdropIdentifier)
if err != nil {
return nil, err
}
airdropDistributor, err := server.keeper.GetAirdropDistributor(ctx, msg.AirdropIdentifier)
if err != nil {
return nil, err
}
addr, err := sdk.AccAddressFromBech32(msg.Allocator)
if err != nil {
return nil, err
}
if !addr.Equals(airdropDistributor) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
}
users, weights := server.keeper.GetUnallocatedUsers(ctx, msg.AirdropIdentifier, msg.Users, msg.Weights)
for idx, user := range users {
record := types.ClaimRecord{
Address: user,
Weight: weights[idx],
ActionCompleted: []bool{false, false, false},
AirdropIdentifier: msg.AirdropIdentifier,
}
records = append(records, record)
totalWeight = totalWeight.Add(weights[idx])
}
server.keeper.SetTotalWeight(ctx, totalWeight, msg.AirdropIdentifier)
err = server.keeper.SetClaimRecords(ctx, records)
if err != nil {
return nil, err
}
return &types.MsgSetAirdropAllocationsResponse{}, nil
}
func (server msgServer) ClaimFreeAmount(goCtx context.Context, msg *types.MsgClaimFreeAmount) (*types.MsgClaimFreeAmountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(msg.User)
if err != nil {
return nil, err
}
coins, err := server.keeper.ClaimAllCoinsForAction(ctx, addr, types.ACTION_FREE)
if err != nil {
return nil, err
}
return &types.MsgClaimFreeAmountResponse{ClaimedAmount: coins}, nil
}
func (server msgServer) CreateAirdrop(goCtx context.Context, msg *types.MsgCreateAirdrop) (*types.MsgCreateAirdropResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := sdk.AccAddressFromBech32(msg.Distributor)
if err != nil {
return nil, err
}
airdrop := server.keeper.GetAirdropByDistributor(ctx, msg.Distributor)
if airdrop != nil {
return nil, types.ErrDistributorAlreadyExists
}
err = server.keeper.CreateAirdropAndEpoch(ctx, *msg)
if err != nil {
return nil, err
}
return &types.MsgCreateAirdropResponse{}, nil
}
func (server msgServer) DeleteAirdrop(goCtx context.Context, msg *types.MsgDeleteAirdrop) (*types.MsgDeleteAirdropResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := sdk.AccAddressFromBech32(msg.Distributor)
if err != nil {
return nil, err
}
distributor, err := server.keeper.GetAirdropDistributor(ctx, msg.Identifier)
if err != nil {
return nil, err
}
if !addr.Equals(distributor) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address")
}
err = server.keeper.DeleteAirdropAndEpoch(ctx, msg.Identifier)
if err != nil {
return nil, err
}
return &types.MsgDeleteAirdropResponse{}, nil
}