fix(critical): eliminate uint256 max overflow by fixing signed int256 parsing across all event parsers

This commit resolves the uint256 max overflow causing amounts to display as +11579208923...

Root cause: UniswapV3 uses signed int256 for amounts, but multiple parsers treated them as unsigned

Files fixed:
- pkg/events/parser.go: Fixed broken signed int conversion (line 392-396)
- pkg/pools/discovery.go: Added signed parsing for UniswapV3 (lines 415-420, 705-710)

Impact: Eliminates e+59 to e+70 overflow values, enables accurate arbitrage calculations
This commit is contained in:
Krypto Kajun
2025-10-25 22:38:14 -05:00
parent 0358eed3a6
commit 85aab7e782
2 changed files with 67 additions and 18 deletions

View File

@@ -18,6 +18,27 @@ import (
"github.com/fraktal/mev-beta/pkg/uniswap"
)
// parseSignedInt256 correctly parses a signed 256-bit integer from 32 bytes
// This is critical for UniswapV3 events which use int256 for amounts
func parseSignedInt256(data []byte) *big.Int {
if len(data) != 32 {
return big.NewInt(0)
}
value := new(big.Int).SetBytes(data)
// Check if the value is negative (MSB set)
if len(data) > 0 && data[0]&0x80 != 0 {
// Convert from two's complement
// Subtract 2^256 to get the negative value
maxUint256 := new(big.Int)
maxUint256.Lsh(big.NewInt(1), 256)
value.Sub(value, maxUint256)
}
return value
}
// EventType represents the type of DEX event
type EventType int
@@ -388,21 +409,13 @@ func (ep *EventParser) parseUniswapV3Swap(log *types.Log, blockNumber uint64, ti
return nil, fmt.Errorf("invalid Uniswap V3 Swap event log")
}
// Parse the data fields
amount0 := new(big.Int).SetBytes(log.Data[0:32])
amount1 := new(big.Int).SetBytes(log.Data[32:64])
// Parse the data fields - UniswapV3 uses signed int256 for amounts
amount0 := parseSignedInt256(log.Data[0:32])
amount1 := parseSignedInt256(log.Data[32:64])
sqrtPriceX96 := new(big.Int).SetBytes(log.Data[64:96])
liquidity := new(big.Int).SetBytes(log.Data[96:128])
tick := new(big.Int).SetBytes(log.Data[128:160])
// Convert to signed values if needed
if amount0.Cmp(big.NewInt(0)) > 0x7fffffffffffffff {
amount0 = amount0.Sub(amount0, new(big.Int).Lsh(big.NewInt(1), 256))
}
if amount1.Cmp(big.NewInt(0)) > 0x7fffffffffffffff {
amount1 = amount1.Sub(amount1, new(big.Int).Lsh(big.NewInt(1), 256))
}
event := &Event{
Type: Swap,
Protocol: "UniswapV3",