MCPcopy
hub / github.com/canopy-network/canopy / HandleMessageDexLimitOrder

Method HandleMessageDexLimitOrder

fsm/message.go:486–517  ·  view source on GitHub ↗

HandleMessageDexLimitOrder() is the proper handler for a `DexLimitOrder` message

(msg *MessageDexLimitOrder)

Source from the content-addressed store, hash-verified

484
485// HandleMessageDexLimitOrder() is the proper handler for a `DexLimitOrder` message
486func (s *StateMachine) HandleMessageDexLimitOrder(msg *MessageDexLimitOrder) (err lib.ErrorI) {
487 // get the next sell batch
488 batch, err := s.GetDexBatch(msg.ChainId, false)
489 if err != nil {
490 return err
491 }
492 // ensure there's some liquidity in the pool
493 if batch.PoolSize == 0 || s.Config.ChainId == msg.ChainId {
494 return ErrInvalidLiquidityPool()
495 }
496 // hard limit orders to 10K per batch to prevent unchecked state growth
497 if len(batch.Orders) >= lib.MaxOrdersPerDexBatch {
498 return ErrMaxDexBatchSize()
499 }
500 // move funds from user
501 if err = s.AccountSub(crypto.NewAddress(msg.Address), msg.AmountForSale); err != nil {
502 return err
503 }
504 // add funds to holding pool
505 if err = s.PoolAdd(msg.ChainId+HoldingPoolAddend, msg.AmountForSale); err != nil {
506 return err
507 }
508 // add the order to the batch
509 batch.Orders = append(batch.Orders, &lib.DexLimitOrder{
510 AmountForSale: msg.AmountForSale,
511 RequestedAmount: msg.RequestedAmount,
512 Address: msg.Address,
513 OrderId: msg.OrderId,
514 })
515 // update next sell batch
516 return s.SetDexBatch(KeyForNextBatch(msg.ChainId), batch)
517}
518
519// HandleMessageDexLiquidityDeposit() is the proper handler for a `DexLiquidityDeposit` message
520func (s *StateMachine) HandleMessageDexLiquidityDeposit(msg *MessageDexLiquidityDeposit) (err lib.ErrorI) {

Callers 4

HandleMessageMethod · 0.95
TestDexSwapFunction · 0.80
applyLocalOpsFunction · 0.80
validateCrossChainSwapFunction · 0.80

Calls 8

GetDexBatchMethod · 0.95
AccountSubMethod · 0.95
PoolAddMethod · 0.95
SetDexBatchMethod · 0.95
NewAddressFunction · 0.92
ErrInvalidLiquidityPoolFunction · 0.85
ErrMaxDexBatchSizeFunction · 0.85
KeyForNextBatchFunction · 0.85

Tested by 3

TestDexSwapFunction · 0.64
applyLocalOpsFunction · 0.64
validateCrossChainSwapFunction · 0.64