Skip to content

Commit

Permalink
fix(solver): handle tokens that decrement max allowance (#3112)
Browse files Browse the repository at this point in the history
Do not error if below max allowance. 

issue:  #3111
  • Loading branch information
kevinhalliday authored Feb 21, 2025
1 parent 2646c43 commit a410c55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
16 changes: 13 additions & 3 deletions solver/app/approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
)

// approveOutboxes gives each outbox max allowance for all supported tokens.
// Most tokens will not decrement allowance when set to max, though some do.
// TODO: monitor allowances, alert or reset when "low" (half type(uint256).max).
func approveOutboxes(ctx context.Context, network netconf.Network, backends ethbackend.Backends, solverAddr common.Address) error {
addrs, err := contracts.GetAddresses(ctx, network.ID)
if err != nil {
Expand Down Expand Up @@ -99,7 +101,9 @@ func approveToken(ctx context.Context, backend *ethbackend.Backend, token Token,
return errors.Wrap(err, "new token")
}

isApproved := func() (bool, error) { return isAppproved(ctx, token.Address, backend, solverAddr, outboxAddr) }
isApproved := func() (bool, error) {
return isAppproved(ctx, token.Address, backend, solverAddr, outboxAddr, umath.MaxUint256)
}

if approved, err := isApproved(); err != nil {
return err
Expand Down Expand Up @@ -128,7 +132,13 @@ func approveToken(ctx context.Context, backend *ethbackend.Backend, token Token,
return nil
}

func isAppproved(ctx context.Context, token common.Address, client ethclient.Client, solverAddr, outboxAddr common.Address) (bool, error) {
func isAppproved(
ctx context.Context,
token common.Address,
client ethclient.Client,
solverAddr, outboxAddr common.Address,
spend *big.Int,
) (bool, error) {
tkn, err := bindings.NewIERC20(token, client)
if err != nil {
return false, errors.Wrap(err, "new token")
Expand All @@ -139,5 +149,5 @@ func isAppproved(ctx context.Context, token common.Address, client ethclient.Cli
return false, errors.Wrap(err, "get allowance")
}

return new(big.Int).Sub(allowance, umath.MaxUint256).Sign() >= 0, nil
return spend.Cmp(allowance) <= 0, nil
}
9 changes: 7 additions & 2 deletions solver/app/procdeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ func newFiller(
return errors.New("unsupported token, should have been rejected [BUG]", "addr", tknAddr.Hex(), "chain_id", destChainID)
}

isAppproved, err := isAppproved(ctx, tknAddr, backend, solverAddr, outboxAddr)
isAppproved, err := isAppproved(ctx, tknAddr, backend, solverAddr, outboxAddr, output.Amount)
if err != nil {
return errors.Wrap(err, "is approved")
}

if !isAppproved {
return errors.New("outbox not approved to spend token [BUG] ", "token", tkn.Symbol, "chain_id", destChainID)
return errors.New("outbox not approved to spend token",
"token", tkn.Symbol,
"chain_id", destChainID,
"addr", tknAddr.Hex(),
"amount", output.Amount,
)
}
}

Expand Down
9 changes: 7 additions & 2 deletions solver/app/reject.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,18 @@ func checkApprovals(ctx context.Context, expenses []Payment, client ethclient.Cl
continue
}

isAppproved, err := isAppproved(ctx, tkn.Address, client, solverAddr, outboxAddr)
isAppproved, err := isAppproved(ctx, tkn.Address, client, solverAddr, outboxAddr, expense.Amount)
if err != nil {
return errors.Wrap(err, "is approved")
}

if !isAppproved {
return errors.New("outbox not approved to spend token [BUG]", "token", tkn.Symbol, "chain_id", tkn.ChainID)
return errors.New("outbox not approved to spend token",
"token", tkn.Symbol,
"chain_id", tkn.ChainID,
"addr", tkn.Address.Hex(),
"amount", expense.Amount,
)
}
}

Expand Down

0 comments on commit a410c55

Please sign in to comment.