Skip to content

Commit

Permalink
reverse check order when creating batches
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrajovic committed Jul 5, 2024
1 parent a718819 commit b747ed6
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions orchestrator/batch_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@ func (l *batchCreator) requestTokenBatch(ctx context.Context, fee *peggytypes.Ba
tokenAddress := gethcommon.HexToAddress(fee.Token)
tokenDenom := l.getTokenDenom(tokenAddress)

tokenPriceUSD, err := l.priceFeed.QueryUSDPrice(tokenAddress)
if err != nil {
l.Log().WithError(err).Warningln("failed to query price feed", "token_addr", tokenAddress.String())
return
}

tokenDecimals, err := l.ethereum.TokenDecimals(ctx, tokenAddress)
if err != nil {
l.Log().WithError(err).Warningln("is token address valid?")
return
}

if !l.checkMinBatchFee(fee, tokenPriceUSD, tokenDecimals) {
if !l.checkMinBatchFee(fee, tokenAddress, tokenDecimals) {
return
}

Expand All @@ -98,22 +92,28 @@ func (l *batchCreator) getTokenDenom(tokenAddr gethcommon.Address) string {
return peggytypes.PeggyDenomString(tokenAddr)
}

func (l *batchCreator) checkMinBatchFee(fee *peggytypes.BatchFees, tokenPriceInUSD float64, tokenDecimals uint8) bool {
func (l *batchCreator) checkMinBatchFee(fee *peggytypes.BatchFees, tokenAddress gethcommon.Address, tokenDecimals uint8) bool {
if l.cfg.MinBatchFeeUSD == 0 {
return true
}

tokenPriceUSDFloat, err := l.priceFeed.QueryUSDPrice(tokenAddress)
if err != nil {
l.Log().WithError(err).Warningln("failed to query price feed", "token_addr", tokenAddress.String())
return false
}

var (
minFeeUSD = decimal.NewFromFloat(l.cfg.MinBatchFeeUSD)
tokenPriceUSD = decimal.NewFromFloat(tokenPriceInUSD)
tokenPriceUSD = decimal.NewFromFloat(tokenPriceUSDFloat)
totalFeeUSD = decimal.NewFromBigInt(fee.TotalFees.BigInt(), -1*int32(tokenDecimals)).Mul(tokenPriceUSD)
)

l.Log().WithFields(log.Fields{
"token_addr": fee.Token,
"total_fee": totalFeeUSD.String() + "USD",
"min_fee": minFeeUSD.String() + "USD",
}).Debugln("checking token withdrawal fees")
}).Debugln("checking total batch fees")

if totalFeeUSD.LessThan(minFeeUSD) {
return false
Expand Down

0 comments on commit b747ed6

Please sign in to comment.