HandleMessageDexLimitOrder() is the proper handler for a `DexLimitOrder` message
(msg *MessageDexLimitOrder)
| 484 | |
| 485 | // HandleMessageDexLimitOrder() is the proper handler for a `DexLimitOrder` message |
| 486 | func (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 |
| 520 | func (s *StateMachine) HandleMessageDexLiquidityDeposit(msg *MessageDexLiquidityDeposit) (err lib.ErrorI) { |