Skip to content

Commit

Permalink
Swap balance/qty validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyMashukov committed Jul 8, 2024
1 parent c134b91 commit d6232fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/config/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,23 @@ func InitServiceContainer() Container {
SBSSwapFinder: &exchange.SBSSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
SwapFirstAmendmentSteps: exchange.SwapFirstAmendmentSteps,
SwapSecondAmendmentSteps: exchange.SwapSecondAmendmentSteps,
SwapThirdAmendmentSteps: exchange.SwapThirdAmendmentSteps,
},
SSBSwapFinder: &exchange.SSBSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
SwapFirstAmendmentSteps: exchange.SwapFirstAmendmentSteps,
SwapSecondAmendmentSteps: exchange.SwapSecondAmendmentSteps,
SwapThirdAmendmentSteps: exchange.SwapThirdAmendmentSteps,
},
SBBSwapFinder: &exchange.SBBSwapFinder{
ExchangeRepository: &exchangeRepository,
Formatter: &formatter,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
SwapFirstAmendmentSteps: exchange.SwapFirstAmendmentSteps,
SwapSecondAmendmentSteps: exchange.SwapSecondAmendmentSteps,
SwapThirdAmendmentSteps: exchange.SwapThirdAmendmentSteps,
},
}

Expand Down Expand Up @@ -417,9 +417,9 @@ func InitServiceContainer() Container {
Formatter: &formatter,
TimeService: &timeService,
CurrentBot: currentBot,
SwapFirstAmendmentSteps: 10,
SwapSecondAmendmentSteps: 50,
SwapThirdAmendmentSteps: 250,
SwapFirstAmendmentSteps: exchange.SwapFirstAmendmentSteps,
SwapSecondAmendmentSteps: exchange.SwapSecondAmendmentSteps,
SwapThirdAmendmentSteps: exchange.SwapThirdAmendmentSteps,
},
SwapValidator: &swapValidator,
Formatter: &formatter,
Expand Down
23 changes: 22 additions & 1 deletion src/service/exchange/swap_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,21 @@ func (s *SwapExecutor) ExecuteSwapTwo(
// Calculate how much we earn, and sell it!
quantity := swapOneOrder.CummulativeQuoteQty

initialQty := quantity
if quantity > balance {
quantity = balance
}

if s.Formatter.ComparePercentage(initialQty, quantity).Lte(99.9) {
log.Printf(
"[%d] swap quantity is less than allowed: %.10f > %.10f (step two)",
swapAction.Id,
initialQty,
quantity,
)
return nil
}

log.Printf(
"[%s] Swap [%d] two balance %s is %f, operation SELL %s",
swapChain.SwapTwo.Symbol,
Expand Down Expand Up @@ -475,11 +486,21 @@ func (s *SwapExecutor) ExecuteSwapThree(
quantity = swapTwoOrder.ExecutedQty
}

// todo: check difference and validate...
initialQty := quantity
if quantity > balance {
quantity = balance
}

if s.Formatter.ComparePercentage(initialQty, quantity).Lte(99.9) {
log.Printf(
"[%d] swap quantity is less than allowed: %.10f > %.10f (step three)",
swapAction.Id,
initialQty,
quantity,
)
return nil
}

log.Printf(
"[%s] Swap [%d] three balance %s is %f, operation BUY %s",
swapChain.SwapThree.Symbol,
Expand Down

0 comments on commit d6232fb

Please sign in to comment.