zero existing erc20 balances

This commit is contained in:
pragmaxim
2026-02-10 12:54:00 +01:00
parent 4ab7046cb9
commit 213a7e160c
2 changed files with 41 additions and 2 deletions

View File

@@ -419,8 +419,14 @@ func addToContract(c *unpackedAddrContract, contractIndex int, index int32, cont
}
if transfer.Standard == bchain.FungibleToken {
// Skip ERC20 balance aggregation; ensure a zero value is available for packing.
if c.Value.Value == nil && len(c.Value.Slice) == 0 {
c.Value.Value = big.NewInt(0)
if c.Value.Value == nil { // no decoded bigint yet; normalize before first use
if len(c.Value.Slice) != 0 { // packed value exists; drop it so we don't re-pack stale data
c.Value.Slice = nil
}
c.Value.Value = new(big.Int) // initialize zero value
} else if len(c.Value.Slice) != 0 || c.Value.Value.Sign() != 0 { // packed or non-zero decoded value present; force zero
c.Value.Slice = nil
c.Value.Value.SetUint64(0)
}
} else if transfer.Standard == bchain.NonFungibleToken {
if index < 0 {

View File

@@ -1435,6 +1435,39 @@ func Test_addToContracts(t *testing.T) {
}
}
func Test_addToContract_ERC20ZeroesExistingValue(t *testing.T) {
transfer := &bchain.TokenTransfer{
Standard: bchain.FungibleToken,
Value: *big.NewInt(1),
}
c := &unpackedAddrContract{
Standard: bchain.FungibleToken,
Contract: makeTestAddrDesc(123),
Value: unpackedBigInt{Value: big.NewInt(123456)},
}
addToContract(c, 0, 1, c.Contract, transfer, false)
if c.Value.Value == nil || c.Value.Value.Sign() != 0 {
t.Fatalf("expected ERC20 value to be zeroed, got %v", c.Value.Value)
}
if len(c.Value.Slice) != 0 {
t.Fatalf("expected ERC20 packed slice to be cleared, got %d bytes", len(c.Value.Slice))
}
c = &unpackedAddrContract{
Standard: bchain.FungibleToken,
Contract: makeTestAddrDesc(124),
Value: unpackedBigInt{Slice: []byte{0x1, 0x2}},
}
addToContract(c, 0, 1, c.Contract, transfer, false)
if c.Value.Value == nil || c.Value.Value.Sign() != 0 {
t.Fatalf("expected ERC20 value to be zeroed after slice, got %v", c.Value.Value)
}
if len(c.Value.Slice) != 0 {
t.Fatalf("expected ERC20 packed slice to be cleared, got %d bytes", len(c.Value.Slice))
}
}
func Test_packUnpackBlockTx(t *testing.T) {
parser := ethereumTestnetParser()
tests := []struct {