/
msg_server.go
284 lines (237 loc) · 8.97 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
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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/CosmosContracts/juno/v15/x/feeshare/types"
)
var _ types.MsgServer = &Keeper{}
func (k Keeper) GetIfContractWasCreatedFromFactory(ctx sdk.Context, _ sdk.AccAddress, info *wasmTypes.ContractInfo) bool {
// This will allow ANYONE to register FeeShare funds to its own contract if it was created from a factory contract
// Note: if there is no admin but a creator made it, then the creator can register it how they wish
creator, err := sdk.AccAddressFromBech32(info.Creator)
if err != nil {
return false
}
isFactoryContract := false
if len(info.Admin) == 0 {
isFactoryContract = k.wasmKeeper.HasContractInfo(ctx, creator)
} else {
admin, err := sdk.AccAddressFromBech32(info.Admin)
if err != nil {
return false
}
isFactoryContract = k.wasmKeeper.HasContractInfo(ctx, admin)
}
return isFactoryContract
}
// GetContractAdminOrCreatorAddress ensures the deployer is the contract's admin OR creator if no admin is set for all msg_server feeshare functions.
func (k Keeper) GetContractAdminOrCreatorAddress(ctx sdk.Context, contract sdk.AccAddress, deployer string) (sdk.AccAddress, error) {
var controllingAccount sdk.AccAddress
// Ensures deployer String is valid
_, err := sdk.AccAddressFromBech32(deployer)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid deployer address %s", deployer)
}
info := k.wasmKeeper.GetContractInfo(ctx, contract)
if len(info.Admin) == 0 {
// no admin, see if they are the creator of the contract
if info.Creator != deployer {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "you are not the creator of this contract %s", info.Creator)
}
creatorAddr, err := sdk.AccAddressFromBech32(info.Creator)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address %s", info.Creator)
}
controllingAccount = creatorAddr
} else {
// Admin is set, so we check if the deployer is the admin
if info.Admin != deployer {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "you are not an admin of this contract %s", deployer)
}
adminAddr, err := sdk.AccAddressFromBech32(info.Admin)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid admin address %s", info.Admin)
}
controllingAccount = adminAddr
}
return controllingAccount, nil
}
// RegisterFeeShare registers a contract to receive transaction fees
func (k Keeper) RegisterFeeShare(
goCtx context.Context,
msg *types.MsgRegisterFeeShare,
) (*types.MsgRegisterFeeShareResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
if !params.EnableFeeShare {
return nil, types.ErrFeeShareDisabled
}
// Get Contract
contract, err := sdk.AccAddressFromBech32(msg.ContractAddress)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid contract address (%s)", err)
}
// Check if contract is already registered
if k.IsFeeShareRegistered(ctx, contract) {
return nil, sdkerrors.Wrapf(types.ErrFeeShareAlreadyRegistered, "contract is already registered %s", contract)
}
// Get the withdraw address of the contract
withdrawer, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid withdrawer address %s", msg.WithdrawerAddress)
}
var deployer sdk.AccAddress
if k.GetIfContractWasCreatedFromFactory(ctx, contract, k.wasmKeeper.GetContractInfo(ctx, contract)) {
// Anyone is allowed to register a contract to itself if it was created from a factory contract
if msg.WithdrawerAddress != msg.ContractAddress {
return nil, sdkerrors.Wrapf(types.ErrFeeShareInvalidWithdrawer, "withdrawer address must be the same as the contract address if it is from a factory contract withdraw:%s contract:%s", msg.WithdrawerAddress, msg.ContractAddress)
}
// set the deployer address to the contract address so it can self register
msg.DeployerAddress = msg.ContractAddress
deployer, err = sdk.AccAddressFromBech32(msg.DeployerAddress)
if err != nil {
return nil, err
}
} else {
// Check that the person who signed the message is the wasm contract admin or creator (if no admin)
deployer, err = k.GetContractAdminOrCreatorAddress(ctx, contract, msg.DeployerAddress)
if err != nil {
return nil, err
}
}
// prevent storing the same address for deployer and withdrawer
feeshare := types.NewFeeShare(contract, deployer, withdrawer)
k.SetFeeShare(ctx, feeshare)
k.SetDeployerMap(ctx, deployer, contract)
k.SetWithdrawerMap(ctx, withdrawer, contract)
k.Logger(ctx).Debug(
"registering contract for transaction fees",
"contract", msg.ContractAddress,
"deployer", msg.DeployerAddress,
"withdraw", msg.WithdrawerAddress,
)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterFeeShare,
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
sdk.NewAttribute(types.AttributeKeyWithdrawerAddress, msg.WithdrawerAddress),
),
},
)
return &types.MsgRegisterFeeShareResponse{}, nil
}
// UpdateFeeShare updates the withdraw address of a given FeeShare. If the given
// withdraw address is empty or the same as the deployer address, the withdraw
// address is removed.
func (k Keeper) UpdateFeeShare(
goCtx context.Context,
msg *types.MsgUpdateFeeShare,
) (*types.MsgUpdateFeeShareResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
if !params.EnableFeeShare {
return nil, types.ErrFeeShareDisabled
}
contract, err := sdk.AccAddressFromBech32(msg.ContractAddress)
if err != nil {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"invalid contract address (%s)", err,
)
}
feeshare, found := k.GetFeeShare(ctx, contract)
if !found {
return nil, sdkerrors.Wrapf(
types.ErrFeeShareContractNotRegistered,
"contract %s is not registered", msg.ContractAddress,
)
}
// feeshare with the given withdraw address is already registered
if msg.WithdrawerAddress == feeshare.WithdrawerAddress {
return nil, sdkerrors.Wrapf(types.ErrFeeShareAlreadyRegistered, "feeshare with withdraw address %s is already registered", msg.WithdrawerAddress)
}
// Check that the person who signed the message is the wasm contract admin, if so return the deployer address
_, err = k.GetContractAdminOrCreatorAddress(ctx, contract, msg.DeployerAddress)
if err != nil {
return nil, err
}
withdrawAddr, err := sdk.AccAddressFromBech32(feeshare.WithdrawerAddress)
if err != nil {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"invalid withdrawer address (%s)", err,
)
}
newWithdrawAddr, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid WithdrawerAddress %s", msg.WithdrawerAddress)
}
k.DeleteWithdrawerMap(ctx, withdrawAddr, contract)
k.SetWithdrawerMap(ctx, newWithdrawAddr, contract)
// update feeshare
feeshare.WithdrawerAddress = newWithdrawAddr.String()
k.SetFeeShare(ctx, feeshare)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeUpdateFeeShare,
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyWithdrawerAddress, msg.WithdrawerAddress),
),
},
)
return &types.MsgUpdateFeeShareResponse{}, nil
}
// CancelFeeShare deletes the FeeShare for a given contract
func (k Keeper) CancelFeeShare(
goCtx context.Context,
msg *types.MsgCancelFeeShare,
) (*types.MsgCancelFeeShareResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.GetParams(ctx)
if !params.EnableFeeShare {
return nil, types.ErrFeeShareDisabled
}
contract, err := sdk.AccAddressFromBech32(msg.ContractAddress)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid contract address (%s)", err)
}
fee, found := k.GetFeeShare(ctx, contract)
if !found {
return nil, sdkerrors.Wrapf(types.ErrFeeShareContractNotRegistered, "contract %s is not registered", msg.ContractAddress)
}
// Check that the person who signed the message is the wasm contract admin, if so return the deployer address
_, err = k.GetContractAdminOrCreatorAddress(ctx, contract, msg.DeployerAddress)
if err != nil {
return nil, err
}
k.DeleteFeeShare(ctx, fee)
k.DeleteDeployerMap(
ctx,
fee.GetDeployerAddr(),
contract,
)
withdrawAddr := fee.GetWithdrawerAddr()
if withdrawAddr != nil {
k.DeleteWithdrawerMap(
ctx,
withdrawAddr,
contract,
)
}
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeCancelFeeShare,
sdk.NewAttribute(sdk.AttributeKeySender, msg.DeployerAddress),
sdk.NewAttribute(types.AttributeKeyContract, msg.ContractAddress),
),
},
)
return &types.MsgCancelFeeShareResponse{}, nil
}