github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/dex/types/msgs.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 ) 10 11 const ( 12 typeMsgDeposit = "deposit" 13 typeMsgWithdraw = "withdraw" 14 typeMsgTransferOwnership = "transferOwnership" 15 typeMsgUpdateOperator = "updateOperator" 16 typeMsgCreateOperator = "createOperator" 17 ) 18 19 // MsgList - high level transaction of the dex module 20 type MsgList struct { 21 Owner sdk.AccAddress `json:"owner"` 22 ListAsset string `json:"list_asset"` // Symbol of asset listed on Dex. 23 QuoteAsset string `json:"quote_asset"` // Symbol of asset quoted by asset listed on Dex. 24 InitPrice sdk.Dec `json:"init_price"` 25 } 26 27 // NewMsgList creates a new MsgList 28 func NewMsgList(owner sdk.AccAddress, listAsset, quoteAsset string, initPrice sdk.Dec) MsgList { 29 return MsgList{ 30 Owner: owner, 31 ListAsset: listAsset, 32 QuoteAsset: quoteAsset, 33 InitPrice: initPrice, 34 } 35 } 36 37 // Route Implements Msg 38 func (msg MsgList) Route() string { return RouterKey } 39 40 // Type Implements Msg 41 func (msg MsgList) Type() string { return "list" } 42 43 // ValidateBasic Implements Msg 44 func (msg MsgList) ValidateBasic() sdk.Error { 45 if msg.ListAsset == msg.QuoteAsset { 46 return ErrInvalidCoins() 47 } 48 49 if !msg.InitPrice.IsPositive() { 50 return ErrInitPriceIsNotPositive() 51 } 52 53 if msg.Owner.Empty() { 54 return ErrAddressIsRequired("owner") 55 } 56 return nil 57 } 58 59 // GetSignBytes Implements Msg 60 func (msg MsgList) GetSignBytes() []byte { 61 bz := ModuleCdc.MustMarshalJSON(msg) 62 return sdk.MustSortJSON(bz) 63 } 64 65 // GetSigners Implements Msg 66 func (msg MsgList) GetSigners() []sdk.AccAddress { 67 return []sdk.AccAddress{msg.Owner} 68 } 69 70 // MsgDeposit - high level transaction of the dex module 71 type MsgDeposit struct { 72 Product string `json:"product"` // product for trading pair in full name of the tokens 73 Amount sdk.SysCoin `json:"amount"` // Coins to add to the deposit 74 Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor 75 } 76 77 // NewMsgDeposit creates a new MsgDeposit 78 func NewMsgDeposit(product string, amount sdk.SysCoin, depositor sdk.AccAddress) MsgDeposit { 79 return MsgDeposit{product, amount, depositor} 80 } 81 82 // Route Implements Msg 83 func (msg MsgDeposit) Route() string { return RouterKey } 84 85 // Type Implements Msg 86 func (msg MsgDeposit) Type() string { return typeMsgDeposit } 87 88 // ValidateBasic Implements Msg 89 func (msg MsgDeposit) ValidateBasic() sdk.Error { 90 if msg.Depositor.Empty() { 91 return ErrAddressIsRequired("depositor") 92 } 93 if !msg.Amount.IsValid() || !msg.Amount.IsPositive() { 94 return ErrInvalidCoins() 95 } 96 97 return nil 98 } 99 100 // GetSignBytes Implements Msg 101 func (msg MsgDeposit) GetSignBytes() []byte { 102 bz := ModuleCdc.MustMarshalJSON(msg) 103 return sdk.MustSortJSON(bz) 104 } 105 106 // GetSigners Implements Msg 107 func (msg MsgDeposit) GetSigners() []sdk.AccAddress { 108 return []sdk.AccAddress{msg.Depositor} 109 } 110 111 // MsgWithdraw - high level transaction of the dex module 112 type MsgWithdraw struct { 113 Product string `json:"product"` // product for trading pair in full name of the tokens 114 Amount sdk.SysCoin `json:"amount"` // Coins to add to the deposit 115 Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor 116 } 117 118 // NewMsgWithdraw creates a new MsgWithdraw 119 func NewMsgWithdraw(product string, amount sdk.SysCoin, depositor sdk.AccAddress) MsgWithdraw { 120 return MsgWithdraw{product, amount, depositor} 121 } 122 123 // Route Implements Msg 124 func (msg MsgWithdraw) Route() string { return RouterKey } 125 126 // Type Implements Msg 127 func (msg MsgWithdraw) Type() string { return typeMsgWithdraw } 128 129 // ValidateBasic Implements Msg 130 func (msg MsgWithdraw) ValidateBasic() sdk.Error { 131 if msg.Depositor.Empty() { 132 return ErrAddressIsRequired("depositor") 133 } 134 if !msg.Amount.IsValid() || !msg.Amount.IsPositive() { 135 return ErrInvalidCoins() 136 } 137 138 return nil 139 } 140 141 // GetSignBytes Implements Msg 142 func (msg MsgWithdraw) GetSignBytes() []byte { 143 bz := ModuleCdc.MustMarshalJSON(msg) 144 return sdk.MustSortJSON(bz) 145 } 146 147 // GetSigners Implements Msg 148 func (msg MsgWithdraw) GetSigners() []sdk.AccAddress { 149 return []sdk.AccAddress{msg.Depositor} 150 } 151 152 // MsgTransferOwnership - high level transaction of the dex module 153 type MsgTransferOwnership struct { 154 FromAddress sdk.AccAddress `json:"from_address"` 155 ToAddress sdk.AccAddress `json:"to_address"` 156 Product string `json:"product"` 157 //ToSignature auth.StdSignature `json:"to_signature"` 158 } 159 160 // NewMsgTransferOwnership create a new MsgTransferOwnership 161 func NewMsgTransferOwnership(from, to sdk.AccAddress, product string) MsgTransferOwnership { 162 return MsgTransferOwnership{ 163 FromAddress: from, 164 ToAddress: to, 165 Product: product, 166 } 167 } 168 169 // Route Implements Msg 170 func (msg MsgTransferOwnership) Route() string { return RouterKey } 171 172 // Type Implements Msg 173 func (msg MsgTransferOwnership) Type() string { return typeMsgTransferOwnership } 174 175 // ValidateBasic Implements Msg 176 func (msg MsgTransferOwnership) ValidateBasic() sdk.Error { 177 if msg.FromAddress.Empty() { 178 return ErrAddressIsRequired("sender") 179 } 180 181 if msg.ToAddress.Empty() { 182 return ErrAddressIsRequired("recipient") 183 } 184 185 if msg.Product == "" { 186 return ErrTokenPairIsRequired() 187 } 188 return nil 189 } 190 191 // GetSignBytes Implements Msg 192 func (msg MsgTransferOwnership) GetSignBytes() []byte { 193 bz := ModuleCdc.MustMarshalJSON(msg) 194 return sdk.MustSortJSON(bz) 195 } 196 197 // GetSigners Implements Msg 198 func (msg MsgTransferOwnership) GetSigners() []sdk.AccAddress { 199 return []sdk.AccAddress{msg.FromAddress} 200 } 201 202 // MsgConfirmOwnership - high level transaction of the coin module 203 type MsgConfirmOwnership struct { 204 Product string `json:"product"` 205 Address sdk.AccAddress `json:"new_owner"` 206 } 207 208 func NewMsgConfirmOwnership(newOwner sdk.AccAddress, product string) MsgConfirmOwnership { 209 return MsgConfirmOwnership{ 210 Product: product, 211 Address: newOwner, 212 } 213 } 214 215 func (msg MsgConfirmOwnership) Route() string { return RouterKey } 216 217 func (msg MsgConfirmOwnership) Type() string { return "confirm" } 218 219 func (msg MsgConfirmOwnership) ValidateBasic() sdk.Error { 220 if msg.Address.Empty() { 221 return ErrAddressIsRequired("sender") 222 } 223 if len(msg.Product) == 0 { 224 return ErrTokenPairIsRequired() 225 } 226 return nil 227 } 228 229 func (msg MsgConfirmOwnership) GetSignBytes() []byte { 230 bz := ModuleCdc.MustMarshalJSON(msg) 231 return sdk.MustSortJSON(bz) 232 } 233 234 func (msg MsgConfirmOwnership) GetSigners() []sdk.AccAddress { 235 return []sdk.AccAddress{msg.Address} 236 } 237 238 // MsgCreateOperator register a new DEXOperator or update it 239 // Addr represent an DEXOperator 240 // if DEXOperator not exist, register a new DEXOperator 241 // else update Website or HandlingFeeAddress 242 type MsgCreateOperator struct { 243 Owner sdk.AccAddress `json:"owner"` 244 Website string `json:"website"` 245 HandlingFeeAddress sdk.AccAddress `json:"handling_fee_address"` 246 } 247 248 // NewMsgCreateOperator creates a new MsgCreateOperator 249 func NewMsgCreateOperator(website string, owner, handlingFeeAddress sdk.AccAddress) MsgCreateOperator { 250 if handlingFeeAddress.Empty() { 251 handlingFeeAddress = owner 252 } 253 return MsgCreateOperator{owner, strings.TrimSpace(website), handlingFeeAddress} 254 } 255 256 // Route Implements Msg 257 func (msg MsgCreateOperator) Route() string { return RouterKey } 258 259 // Type Implements Msg 260 func (msg MsgCreateOperator) Type() string { return typeMsgCreateOperator } 261 262 // ValidateBasic Implements Msg 263 func (msg MsgCreateOperator) ValidateBasic() sdk.Error { 264 if msg.Owner.Empty() { 265 return ErrAddressIsRequired("owner") 266 } 267 if msg.HandlingFeeAddress.Empty() { 268 return ErrAddressIsRequired("handling fee") 269 } 270 return checkWebsite(msg.Website) 271 } 272 273 // GetSignBytes Implements Msg 274 func (msg MsgCreateOperator) GetSignBytes() []byte { 275 bz := ModuleCdc.MustMarshalJSON(msg) 276 return sdk.MustSortJSON(bz) 277 } 278 279 // GetSigners Implements Msg 280 func (msg MsgCreateOperator) GetSigners() []sdk.AccAddress { 281 return []sdk.AccAddress{msg.Owner} 282 } 283 284 // MsgUpdateOperator register a new DEXOperator or update it 285 // Addr represent an DEXOperator 286 // if DEXOperator not exist, register a new DEXOperator 287 // else update Website or HandlingFeeAddress 288 type MsgUpdateOperator struct { 289 Owner sdk.AccAddress `json:"owner"` 290 Website string `json:"website"` 291 HandlingFeeAddress sdk.AccAddress `json:"handling_fee_address"` 292 } 293 294 // NewMsgUpdateOperator creates a new MsgUpdateOperator 295 func NewMsgUpdateOperator(website string, owner, handlingFeeAddress sdk.AccAddress) MsgUpdateOperator { 296 if handlingFeeAddress.Empty() { 297 handlingFeeAddress = owner 298 } 299 return MsgUpdateOperator{owner, strings.TrimSpace(website), handlingFeeAddress} 300 } 301 302 // Route Implements Msg 303 func (msg MsgUpdateOperator) Route() string { return RouterKey } 304 305 // Type Implements Msg 306 func (msg MsgUpdateOperator) Type() string { return typeMsgUpdateOperator } 307 308 // ValidateBasic Implements Msg 309 func (msg MsgUpdateOperator) ValidateBasic() sdk.Error { 310 if msg.HandlingFeeAddress.Empty() { 311 return ErrAddressIsRequired("handling fee") 312 } 313 return checkWebsite(msg.Website) 314 } 315 316 // GetSignBytes Implements Msg 317 func (msg MsgUpdateOperator) GetSignBytes() []byte { 318 bz := ModuleCdc.MustMarshalJSON(msg) 319 return sdk.MustSortJSON(bz) 320 } 321 322 // GetSigners Implements Msg 323 func (msg MsgUpdateOperator) GetSigners() []sdk.AccAddress { 324 return []sdk.AccAddress{msg.Owner} 325 } 326 327 func checkWebsite(website string) sdk.Error { 328 if len(website) == 0 { 329 return nil 330 } 331 if len(website) > 1024 { 332 return ErrInvalidWebsiteLength(len(website), 1024) 333 } 334 u, err := url.Parse(website) 335 if err != nil { 336 return ErrInvalidWebsiteURL(err.Error()) 337 } 338 if u.Scheme != "http" && u.Scheme != "https" { 339 return ErrInvalidWebsiteURL(fmt.Sprintf("got: %s, expected: http or https", u.Scheme)) 340 } 341 return nil 342 }