-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg_server.go
executable file
·105 lines (84 loc) · 2.91 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/fibonacci-chain/core/x/oracle/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the oracle MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
func (ms msgServer) AggregateExchangeRateVote(goCtx context.Context, msg *types.MsgAggregateExchangeRateVote) (*types.MsgAggregateExchangeRateVoteResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return nil, err
}
if err := ms.ValidateFeeder(ctx, feederAddr, valAddr); err != nil {
return nil, err
}
exchangeRateTuples, err := types.ParseExchangeRateTuples(msg.ExchangeRates)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, err.Error())
}
// check all denoms are in the vote target
for _, tuple := range exchangeRateTuples {
if !ms.IsVoteTarget(ctx, tuple.Denom) {
return nil, sdkerrors.Wrap(types.ErrUnknownDenom, tuple.Denom)
}
}
ms.SetAggregateExchangeRateVote(ctx, valAddr, types.NewAggregateExchangeRateVote(exchangeRateTuples, valAddr))
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAggregateVote,
sdk.NewAttribute(types.AttributeKeyVoter, msg.Validator),
sdk.NewAttribute(types.AttributeKeyExchangeRates, msg.ExchangeRates),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Feeder),
),
})
return &types.MsgAggregateExchangeRateVoteResponse{}, nil
}
func (ms msgServer) DelegateFeedConsent(goCtx context.Context, msg *types.MsgDelegateFeedConsent) (*types.MsgDelegateFeedConsentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
operatorAddr, err := sdk.ValAddressFromBech32(msg.Operator)
if err != nil {
return nil, err
}
delegateAddr, err := sdk.AccAddressFromBech32(msg.Delegate)
if err != nil {
return nil, err
}
// Check the delegator is a validator
val := ms.StakingKeeper.Validator(ctx, operatorAddr)
if val == nil {
return nil, sdkerrors.Wrap(stakingtypes.ErrNoValidatorFound, msg.Operator)
}
// Set the delegation
ms.SetFeederDelegation(ctx, operatorAddr, delegateAddr)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeFeedDelegate,
sdk.NewAttribute(types.AttributeKeyFeeder, msg.Delegate),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Operator),
),
})
return &types.MsgDelegateFeedConsentResponse{}, nil
}