-
Notifications
You must be signed in to change notification settings - Fork 574
/
msgs.go
150 lines (123 loc) · 4 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
package stableswap
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/v12/x/gamm/types"
)
const (
TypeMsgCreateStableswapPool = "create_stableswap_pool"
TypeMsgStableSwapAdjustScalingFactors = "stable_swap_adjust_scaling_factors"
)
var (
_ sdk.Msg = &MsgCreateStableswapPool{}
_ types.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 sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", err)
}
err = msg.PoolParams.Validate()
if err != nil {
return err
}
// validation for pool initial liquidity
// TO DO: expand this check to accommodate multi-asset pools for stableswap
if len(msg.InitialPoolLiquidity) < 2 {
return types.ErrTooFewPoolAssets
} else if len(msg.InitialPoolLiquidity) > 2 {
return types.ErrTooManyPoolAssets
}
// valid scaling factor lengths are 0, or one factor for each asset
if len(msg.ScalingFactors) != 0 && len(msg.ScalingFactors) != len(msg.InitialPoolLiquidity) {
return types.ErrInvalidScalingFactors
}
// validation for future owner
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) (types.PoolI, error) {
stableswapPool, err := NewStableswapPool(poolId, *msg.PoolParams, msg.InitialPoolLiquidity, msg.ScalingFactors, msg.FuturePoolGovernor)
if err != nil {
return nil, err
}
return &stableswapPool, nil
}
var _ sdk.Msg = &MsgStableSwapAdjustScalingFactors{}
// Implement sdk.Msg
func NewMsgStableSwapAdjustScalingFactors(
sender string,
poolID uint64,
) MsgStableSwapAdjustScalingFactors {
return MsgStableSwapAdjustScalingFactors{
Sender: sender,
PoolID: poolID,
}
}
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 sdkerrors.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}
}