-
Notifications
You must be signed in to change notification settings - Fork 568
/
msgs.go
171 lines (141 loc) · 4.69 KB
/
msgs.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
package stableswap
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/v19/x/gamm/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v19/x/poolmanager/types"
)
const (
TypeMsgCreateStableswapPool = "create_stableswap_pool"
TypeMsgStableSwapAdjustScalingFactors = "stable_swap_adjust_scaling_factors"
)
var (
_ sdk.Msg = &MsgCreateStableswapPool{}
_ poolmanagertypes.CreatePoolMsg = &MsgCreateStableswapPool{}
)
func NewMsgCreateStableswapPool(
sender sdk.AccAddress,
poolParams PoolParams,
initialLiquidity sdk.Coins,
scalingFactors []uint64,
futurePoolGovernor string,
) MsgCreateStableswapPool {
return MsgCreateStableswapPool{
Sender: sender.String(),
PoolParams: &poolParams,
InitialPoolLiquidity: initialLiquidity,
ScalingFactors: scalingFactors,
FuturePoolGovernor: futurePoolGovernor,
}
}
func (msg MsgCreateStableswapPool) Route() string { return types.RouterKey }
func (msg MsgCreateStableswapPool) Type() string { return TypeMsgCreateStableswapPool }
func (msg MsgCreateStableswapPool) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", err)
}
err = msg.PoolParams.Validate()
if err != nil {
return err
}
// validation for scaling factors
scalingFactors := msg.ScalingFactors
// The message's scaling factors must be empty or a valid set of scaling factors
if len(scalingFactors) != 0 {
if err = validateScalingFactors(scalingFactors, len(msg.InitialPoolLiquidity)); err != nil {
return err
}
} else {
for i := 0; i < len(msg.InitialPoolLiquidity); i += 1 {
scalingFactors = append(scalingFactors, 1)
}
}
// validation for pool initial liquidity
// The message's pool liquidity must have between 2 and 8 assets with at most 10B post-scaled units in each
if err = validatePoolLiquidity(msg.InitialPoolLiquidity, scalingFactors); err != nil {
return err
}
// validation for scaling factor owner
if err = validateScalingFactorController(msg.ScalingFactorController); err != nil {
return err
}
// validation for future governor
if err = types.ValidateFutureGovernor(msg.FuturePoolGovernor); err != nil {
return err
}
return nil
}
func (msg MsgCreateStableswapPool) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgCreateStableswapPool) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
/// Implement the CreatePoolMsg interface
func (msg MsgCreateStableswapPool) PoolCreator() sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return sender
}
func (msg MsgCreateStableswapPool) Validate(ctx sdk.Context) error {
return msg.ValidateBasic()
}
func (msg MsgCreateStableswapPool) InitialLiquidity() sdk.Coins {
return msg.InitialPoolLiquidity
}
func (msg MsgCreateStableswapPool) CreatePool(ctx sdk.Context, poolId uint64) (poolmanagertypes.PoolI, error) {
stableswapPool, err := NewStableswapPool(poolId, *msg.PoolParams, msg.InitialPoolLiquidity,
msg.ScalingFactors, msg.ScalingFactorController, msg.FuturePoolGovernor)
if err != nil {
return nil, err
}
return &stableswapPool, nil
}
func (msg MsgCreateStableswapPool) GetPoolType() poolmanagertypes.PoolType {
return poolmanagertypes.Stableswap
}
var _ sdk.Msg = &MsgStableSwapAdjustScalingFactors{}
// Implement sdk.Msg
func NewMsgStableSwapAdjustScalingFactors(
sender string,
poolID uint64,
scalingFactors []uint64,
) MsgStableSwapAdjustScalingFactors {
return MsgStableSwapAdjustScalingFactors{
Sender: sender,
PoolID: poolID,
ScalingFactors: scalingFactors,
}
}
func (msg MsgStableSwapAdjustScalingFactors) Route() string {
return types.RouterKey
}
func (msg MsgStableSwapAdjustScalingFactors) Type() string { return TypeMsgCreateStableswapPool }
func (msg MsgStableSwapAdjustScalingFactors) ValidateBasic() error {
if msg.Sender == "" {
return nil
}
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", err)
}
return nil
}
func (msg MsgStableSwapAdjustScalingFactors) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgStableSwapAdjustScalingFactors) GetSigners() []sdk.AccAddress {
scalingFactorGovernor, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{scalingFactorGovernor}
}