Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Nov 18, 2024
1 parent 27aa6c4 commit ac8e653
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
42 changes: 26 additions & 16 deletions libwallet/instantswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,32 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
sourceAccountBalance, err := sourceWallet.GetAccountBalance(params.Order.SourceAccountNumber)
if err != nil {
log.Error("unable to get account balance")
return errors.E(op, err)
return err
}

if sourceAccountBalance.Spendable.ToCoin() <= params.BalanceToMaintain {
walletBalance := sourceAccountBalance.Spendable.ToCoin()
if walletBalance <= params.BalanceToMaintain {
if !lastOrderTime.IsZero() { // some orders have already been concluded
return nil
}

log.Error("source wallet balance is less than or equals the set balance to maintain")
return errors.E(op, "source wallet balance is less than or equals the set balance to maintain") // stop scheduling if the source wallet balance is less than or equals the set balance to maintain
}

if !lastOrderTime.IsZero() {
log.Info("Order Scheduler: creating next order based on selected frequency")

// calculate time until the next order
timeUntilNextOrder := params.Frequency - time.Since(lastOrderTime)
if timeUntilNextOrder <= 0 {
log.Info("Order Scheduler: the scheduler start time is equal to or greater than the frequency, starting next order immediately")
} else {
log.Infof("Order Scheduler: %s until the next order is executed", timeUntilNextOrder)
time.Sleep(timeUntilNextOrder)
}
}

fromCur := params.Order.FromCurrency
toCur := params.Order.ToCurrency
rateRequestParams := api.ExchangeRateRequest{
Expand Down Expand Up @@ -164,7 +182,6 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap

// set the max send amount to the max limit set by the server
invoicedAmount := res.Max
walletBalance := sourceAccountBalance.Spendable.ToCoin()

estimatedBalanceAfterExchange := walletBalance - invoicedAmount
// if the max send limit is 0, then the server does not have a max limit
Expand Down Expand Up @@ -223,7 +240,7 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
}

log.Info("Order Scheduler: broadcasting tx")
_, err = sourceWallet.Broadcast(params.SpendingPassphrase, "")
txHash, err := sourceWallet.Broadcast(params.SpendingPassphrase, "")
if err != nil {
log.Error("error broadcasting tx: ", err.Error())
return errors.E(op, err)
Expand Down Expand Up @@ -257,6 +274,11 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
return errors.E(op, err)
}

// If this is empty for any reason, default to the actual tx hash.
if orderInfo.TxID == "" {
orderInfo.TxID = txHash
}

if orderInfo.Status == api.OrderStatusRefunded {
log.Info("order was refunded. verifying that the order was refunded successfully from the blockchain explorer")
tmpFromCurrency := params.Order.FromCurrency
Expand Down Expand Up @@ -315,20 +337,8 @@ func (mgr *AssetsManager) StartScheduler(ctx context.Context, params instantswap
break // order is completed, break out of the loop
}

log.Info("Order Scheduler: order is not completed, checking again")
continue // order is not completed, check again
}

log.Info("Order Scheduler: creating next order based on selected frequency")

// calculate time until the next order
timeUntilNextOrder := params.Frequency - time.Since(lastOrderTime)
if timeUntilNextOrder <= 0 {
log.Info("Order Scheduler: the scheduler start time is equal to or greater than the frequency, starting next order immediately")
} else {
log.Infof("Order Scheduler: %s until the next order is executed", timeUntilNextOrder)
time.Sleep(timeUntilNextOrder)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ui/page/exchange/order_scheduler_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (osm *orderSchedulerModal) startOrderScheduler() {
// the error modal.
successModal.Dismiss()

errModal := modal.NewErrorModal(osm.Load, values.String(values.StrOrderScheduler), modal.DefaultClickFunc()).
errModal := modal.NewErrorModal(osm.Load, values.String(values.StrUnexpectedError), modal.DefaultClickFunc()).
Body(values.StringF(values.StrUnexpectedErrorMsgFmt, err.Error()))
osm.ParentWindow().ShowModal(errModal)
}
Expand Down

0 comments on commit ac8e653

Please sign in to comment.