github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/farm/types/msg.go (about) 1 package types 2 3 import ( 4 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 5 ) 6 7 const ( 8 MaxPoolNameLength = 128 9 10 createPoolMsgType = "create_pool" 11 destroyPoolMsgType = "destroy_pool" 12 provideMsgType = "provide" 13 lockMsgType = "lock" 14 unlockMsgType = "unlock" 15 claimMsgType = "claim" 16 ) 17 18 type MsgCreatePool struct { 19 Owner sdk.AccAddress `json:"owner" yaml:"owner"` 20 PoolName string `json:"pool_name" yaml:"pool_name"` 21 MinLockAmount sdk.SysCoin `json:"min_lock_amount" yaml:"min_lock_amount"` 22 YieldedSymbol string `json:"yielded_symbol" yaml:"yielded_symbol"` 23 } 24 25 var _ sdk.Msg = MsgCreatePool{} 26 27 func NewMsgCreatePool(address sdk.AccAddress, poolName string, minLockAmount sdk.SysCoin, yieldedSymbol string) MsgCreatePool { 28 return MsgCreatePool{ 29 Owner: address, 30 PoolName: poolName, 31 MinLockAmount: minLockAmount, 32 YieldedSymbol: yieldedSymbol, 33 } 34 } 35 36 func (m MsgCreatePool) Route() string { 37 return RouterKey 38 } 39 40 func (m MsgCreatePool) Type() string { 41 return createPoolMsgType 42 } 43 44 func (m MsgCreatePool) ValidateBasic() sdk.Error { 45 if m.Owner.Empty() { 46 return ErrNilAddress() 47 } 48 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 49 return ErrPoolNameLength(m.PoolName, len(m.PoolName), MaxPoolNameLength) 50 } 51 if m.MinLockAmount.Amount.LT(sdk.ZeroDec()) || !m.MinLockAmount.IsValid() { 52 return ErrInvalidInputAmount(m.MinLockAmount.String()) 53 } 54 if m.YieldedSymbol == "" { 55 return ErrInvalidInput("yielded symbol is empty") 56 } 57 return nil 58 } 59 60 func (m MsgCreatePool) GetSignBytes() []byte { 61 bz := ModuleCdc.MustMarshalJSON(m) 62 return sdk.MustSortJSON(bz) 63 } 64 65 func (m MsgCreatePool) GetSigners() []sdk.AccAddress { 66 return []sdk.AccAddress{m.Owner} 67 } 68 69 type MsgDestroyPool struct { 70 Owner sdk.AccAddress `json:"owner" yaml:"owner"` 71 PoolName string `json:"pool_name" yaml:"pool_name"` 72 } 73 74 var _ sdk.Msg = MsgDestroyPool{} 75 76 func NewMsgDestroyPool(address sdk.AccAddress, poolName string) MsgDestroyPool { 77 return MsgDestroyPool{ 78 Owner: address, 79 PoolName: poolName, 80 } 81 } 82 83 func (m MsgDestroyPool) Route() string { 84 return RouterKey 85 } 86 87 func (m MsgDestroyPool) Type() string { 88 return destroyPoolMsgType 89 } 90 91 func (m MsgDestroyPool) ValidateBasic() sdk.Error { 92 if m.Owner.Empty() { 93 return ErrNilAddress() 94 } 95 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 96 return ErrPoolNameLength(m.PoolName, len(m.PoolName), MaxPoolNameLength) 97 } 98 return nil 99 } 100 101 func (m MsgDestroyPool) GetSignBytes() []byte { 102 bz := ModuleCdc.MustMarshalJSON(m) 103 return sdk.MustSortJSON(bz) 104 } 105 106 func (m MsgDestroyPool) GetSigners() []sdk.AccAddress { 107 return []sdk.AccAddress{m.Owner} 108 } 109 110 type MsgProvide struct { 111 PoolName string `json:"pool_name" yaml:"pool_name"` 112 Address sdk.AccAddress `json:"address" yaml:"address"` 113 Amount sdk.SysCoin `json:"amount" yaml:"amount"` 114 AmountYieldedPerBlock sdk.Dec `json:"amount_yielded_per_block" yaml:"amount_yielded_per_block"` 115 StartHeightToYield int64 `json:"start_height_to_yield" yaml:"start_height_to_yield"` 116 } 117 118 func NewMsgProvide(poolName string, address sdk.AccAddress, amount sdk.SysCoin, 119 amountYieldedPerBlock sdk.Dec, startHeightToYield int64) MsgProvide { 120 return MsgProvide{ 121 PoolName: poolName, 122 Address: address, 123 Amount: amount, 124 AmountYieldedPerBlock: amountYieldedPerBlock, 125 StartHeightToYield: startHeightToYield, 126 } 127 } 128 129 var _ sdk.Msg = MsgProvide{} 130 131 func (m MsgProvide) Route() string { 132 return RouterKey 133 } 134 135 func (m MsgProvide) Type() string { 136 return provideMsgType 137 } 138 139 func (m MsgProvide) ValidateBasic() sdk.Error { 140 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 141 return ErrInvalidInput(m.PoolName) 142 } 143 if m.Address.Empty() { 144 return ErrNilAddress() 145 } 146 if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() { 147 return ErrInvalidInputAmount(m.Amount.String()) 148 } 149 if m.AmountYieldedPerBlock.LTE(sdk.ZeroDec()) { 150 return ErrInvalidInput("amount yielded per block must be > 0") 151 } 152 if m.Amount.Amount.LT(m.AmountYieldedPerBlock) { 153 return ErrInvalidInput("provided amount must be bigger than amount_yielded_per_block") 154 } 155 if m.StartHeightToYield <= 0 { 156 return ErrInvalidInput("start height to yield must be > 0") 157 } 158 return nil 159 } 160 161 func (m MsgProvide) GetSignBytes() []byte { 162 bz := ModuleCdc.MustMarshalJSON(m) 163 return sdk.MustSortJSON(bz) 164 } 165 166 func (m MsgProvide) GetSigners() []sdk.AccAddress { 167 return []sdk.AccAddress{m.Address} 168 } 169 170 type MsgLock struct { 171 PoolName string `json:"pool_name" yaml:"pool_name"` 172 Address sdk.AccAddress `json:"address" yaml:"address"` 173 Amount sdk.SysCoin `json:"amount" yaml:"amount"` 174 } 175 176 func NewMsgLock(poolName string, address sdk.AccAddress, amount sdk.SysCoin) MsgLock { 177 return MsgLock{ 178 PoolName: poolName, 179 Address: address, 180 Amount: amount, 181 } 182 } 183 184 var _ sdk.Msg = MsgLock{} 185 186 func (m MsgLock) Route() string { 187 return RouterKey 188 } 189 190 func (m MsgLock) Type() string { 191 return lockMsgType 192 } 193 194 func (m MsgLock) ValidateBasic() sdk.Error { 195 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 196 return ErrInvalidInput(m.PoolName) 197 } 198 if m.Address.Empty() { 199 return ErrNilAddress() 200 } 201 if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() { 202 return ErrInvalidInputAmount(m.Amount.Amount.String()) 203 } 204 return nil 205 } 206 207 func (m MsgLock) GetSignBytes() []byte { 208 bz := ModuleCdc.MustMarshalJSON(m) 209 return sdk.MustSortJSON(bz) 210 } 211 212 func (m MsgLock) GetSigners() []sdk.AccAddress { 213 return []sdk.AccAddress{m.Address} 214 } 215 216 type MsgUnlock struct { 217 PoolName string `json:"pool_name" yaml:"pool_name"` 218 Address sdk.AccAddress `json:"address" yaml:"address"` 219 Amount sdk.SysCoin `json:"amount" yaml:"amount"` 220 } 221 222 func NewMsgUnlock(poolName string, address sdk.AccAddress, amount sdk.SysCoin) MsgUnlock { 223 return MsgUnlock{ 224 PoolName: poolName, 225 Address: address, 226 Amount: amount, 227 } 228 } 229 230 var _ sdk.Msg = MsgUnlock{} 231 232 func (m MsgUnlock) Route() string { 233 return RouterKey 234 } 235 236 func (m MsgUnlock) Type() string { 237 return unlockMsgType 238 } 239 240 func (m MsgUnlock) ValidateBasic() sdk.Error { 241 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 242 return ErrInvalidInput(m.PoolName) 243 } 244 if m.Address.Empty() { 245 return ErrNilAddress() 246 } 247 if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() { 248 return ErrInvalidInputAmount(m.Amount.Amount.String()) 249 } 250 return nil 251 } 252 253 func (m MsgUnlock) GetSignBytes() []byte { 254 bz := ModuleCdc.MustMarshalJSON(m) 255 return sdk.MustSortJSON(bz) 256 } 257 258 func (m MsgUnlock) GetSigners() []sdk.AccAddress { 259 return []sdk.AccAddress{m.Address} 260 } 261 262 type MsgClaim struct { 263 PoolName string `json:"pool_name" yaml:"pool_name"` 264 Address sdk.AccAddress `json:"address" yaml:"address"` 265 } 266 267 func NewMsgClaim(poolName string, address sdk.AccAddress) MsgClaim { 268 return MsgClaim{ 269 PoolName: poolName, 270 Address: address, 271 } 272 } 273 274 var _ sdk.Msg = MsgClaim{} 275 276 func (m MsgClaim) Route() string { 277 return RouterKey 278 } 279 280 func (m MsgClaim) Type() string { 281 return claimMsgType 282 } 283 284 func (m MsgClaim) ValidateBasic() sdk.Error { 285 if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength { 286 return ErrInvalidInput(m.PoolName) 287 } 288 if m.Address.Empty() { 289 return ErrNilAddress() 290 } 291 return nil 292 } 293 294 func (m MsgClaim) GetSignBytes() []byte { 295 bz := ModuleCdc.MustMarshalJSON(m) 296 return sdk.MustSortJSON(bz) 297 } 298 299 func (m MsgClaim) GetSigners() []sdk.AccAddress { 300 return []sdk.AccAddress{m.Address} 301 }