Skip to content

Commit

Permalink
shovel: bugfix. negative int256 values
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotsmith committed May 30, 2024
1 parent c86768d commit 9ded180
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion dig/dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package dig
import (
"bytes"
"context"
"database/sql/driver"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -1066,9 +1067,27 @@ func (ig Integration) processLog(rows [][]any, lwc *logWithCtx, pgmut *sync.Mute
return rows, nil
}

type negInt struct {
i *uint256.Int
}

func (ni *negInt) Value() (driver.Value, error) {
v, err := ni.i.Value()
if ni.i.Sign() < 0 {
x := uint256.NewInt(0).Neg(ni.i)
v, err = x.Value()
return "-" + v.(string), err
}
return v, err
}

func dbtype(abitype string, d []byte) any {
switch {
case strings.HasPrefix(abitype, "uint"), strings.HasPrefix(abitype, "int"):
case strings.HasPrefix(abitype, "int"):
x := &uint256.Int{}
x.SetBytes(d)
return &negInt{x}
case strings.HasPrefix(abitype, "uint"):
var x uint256.Int
x.SetBytes(d)
return &x
Expand Down
6 changes: 6 additions & 0 deletions dig/dig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ func TestDBType(t *testing.T) {
true,
reflect.Bool,
},
{
"int256",
hb("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
&negInt{uint256.NewInt(0).Neg(uint256.NewInt(1))},
reflect.Ptr,
},
}
for _, tc := range cases {
got := dbtype(tc.abitype, tc.input)
Expand Down

0 comments on commit 9ded180

Please sign in to comment.