-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler.go
288 lines (229 loc) · 8.66 KB
/
handler.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package treasury
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/anathatech/project-anatha/x/treasury/internal/types"
"time"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case MsgAddOperator:
return handleMsgAddOperator(ctx, k, msg)
case MsgRemoveOperator:
return handleMsgRemoveOperator(ctx, k, msg)
case MsgDisburse:
return handleMsgDisburse(ctx, k, msg)
case MsgDisburseToEscrow:
return handleMsgDisburseToEscrow(ctx, k, msg)
case MsgDisburseFromEscrow:
return handleMsgDisburseFromEscrow(ctx, k, msg)
case MsgRevertFromEscrow:
return handleMsgRevertFromEscrow(ctx, k, msg)
case MsgCancelDisbursement:
return handleMsgCancelDisbursement(ctx, k, msg)
case MsgCreateSellOrder:
return handleMsgCreateSellOrder(ctx, k, msg)
case MsgCreateBuyOrder:
return handleMsgCreateBuyOrder(ctx, k, msg)
case MsgSwap:
return handleMsgSwap(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
func NewTreasuryProposalHandler(k Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case AddBuyBackLiquidityProposal:
return handleAddBuyBackLiquidityProposal(ctx, k, c)
case RemoveBuyBackLiquidityProposal:
return handleRemoveBuyBackLiquidityProposal(ctx, k, c)
case BurnDistributionProfitsProposal:
return handleBurnDistributionProfitsProposal(ctx, k, c)
case TransferFromDistributionProfitsToBuyBackLiquidityProposal:
return handleTransferFromDistributionProfitsToBuyBackLiquidityProposal(ctx, k, c)
case TransferFromTreasuryToSwapEscrowProposal:
return handleTransferFromTreasuryToSwapEscrowProposal(ctx, k, c)
case TransferFromSwapEscrowToBuyBackProposal:
return handleTransferSwapEscrowToBuyBackProposal(ctx, k, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized treasury proposal content type: %T", c)
}
}
}
func handleAddBuyBackLiquidityProposal(ctx sdk.Context, k Keeper, p AddBuyBackLiquidityProposal) error {
err := k.MintCoinsToBuyBackLiquidityFund(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeAddBuyBackLiquidity,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleRemoveBuyBackLiquidityProposal(ctx sdk.Context, k Keeper, p RemoveBuyBackLiquidityProposal) error {
err := k.BurnCoinsFromBuyBackLiquidityFund(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRemoveBuyBackLiquidity,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleBurnDistributionProfitsProposal(ctx sdk.Context, k Keeper, p BurnDistributionProfitsProposal) error {
err := k.BurnFromDistributionProfits(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeBurnDistributionProfits,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleTransferFromDistributionProfitsToBuyBackLiquidityProposal(ctx sdk.Context, k Keeper, p TransferFromDistributionProfitsToBuyBackLiquidityProposal) error {
err := k.TransferFromDistributionProfitsToBuyBackLiquidity(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTransferFromDistributionProfitsToBuyBackLiquidity,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleTransferFromTreasuryToSwapEscrowProposal(ctx sdk.Context, k Keeper, p TransferFromTreasuryToSwapEscrowProposal) error {
err := k.TransferFromTreasuryToSwapEscrow(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTransferFromTreasuryToSwapEscrow,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleTransferSwapEscrowToBuyBackProposal(ctx sdk.Context, k Keeper, p TransferFromSwapEscrowToBuyBackProposal) error {
err := k.TransferFromSwapEscrowToBuyBack(ctx, p.Amount)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTransferSwapEscrowToBuyBack,
sdk.NewAttribute(types.AttributeKeyTitle, p.Title),
sdk.NewAttribute(types.AttributeKeyDescription, p.Description),
sdk.NewAttribute(types.AttributeKeyAmount, p.Amount.String()),
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueModule),
),
)
return nil
}
func handleMsgAddOperator(ctx sdk.Context, k Keeper, msg MsgAddOperator) (*sdk.Result, error) {
err := k.HandleAddOperator(ctx, msg.Sender, msg.Operator)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgRemoveOperator(ctx sdk.Context, k Keeper, msg MsgRemoveOperator) (*sdk.Result, error) {
err := k.HandleRemoveOperator(ctx, msg.Sender, msg.Operator)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgDisburse(ctx sdk.Context, k Keeper, msg MsgDisburse) (*sdk.Result, error) {
err := k.HandleDisburse(ctx, msg.Operator, msg.Recipient, msg.Amount, msg.Reference)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgDisburseToEscrow(ctx sdk.Context, k Keeper, msg MsgDisburseToEscrow) (*sdk.Result, error) {
err := k.HandleDisburseToEscrow(ctx, msg.Operator, msg.Amount, msg.Reference)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgDisburseFromEscrow(ctx sdk.Context, k Keeper, msg MsgDisburseFromEscrow) (*sdk.Result, error) {
err := k.HandleDisburseFromEscrow(ctx, msg.Operator, msg.Reference, msg.Recipient)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgRevertFromEscrow(ctx sdk.Context, k Keeper, msg MsgRevertFromEscrow) (*sdk.Result, error) {
err := k.HandleRevertFromEscrow(ctx, msg.Operator, msg.Amount, msg.Reference)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgCancelDisbursement(ctx sdk.Context, k Keeper, msg MsgCancelDisbursement) (*sdk.Result, error) {
scheduledFor, err := time.Parse("2006-01-02T15:04:05.99999999999Z", msg.ScheduledFor)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidTime, msg.ScheduledFor)
}
err = k.HandleCancelDisbursement(ctx, msg.Manager, msg.Recipient, scheduledFor)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgCreateSellOrder(ctx sdk.Context, k Keeper, msg MsgCreateSellOrder) (*sdk.Result, error) {
err := k.HandleCreateSellOrder(ctx, msg.Seller, msg.Amount)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgCreateBuyOrder(ctx sdk.Context, k Keeper, msg MsgCreateBuyOrder) (*sdk.Result, error) {
err := k.HandleCreateBuyOrder(ctx, msg.Buyer, msg.Amount)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgSwap(ctx sdk.Context, k Keeper, msg MsgSwap) (*sdk.Result, error) {
err := k.HandleSwap(ctx, msg.Operator, msg.Recipient, msg.Amount, msg.Reference)
if err != nil {
return nil, err
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}