Skip to content

Commit

Permalink
Refactoring/simplifying hexadecimal value parsing/trimming (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanz0rz authored Jan 23, 2025
1 parent 9b6d9f6 commit 3cfb070
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 243 deletions.
9 changes: 5 additions & 4 deletions internal/bytesbuilder/bytesbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package bytesbuilder
import (
"encoding/hex"
"math/big"
"strings"

"github.com/1inch/1inch-sdk-go/internal/hexadecimal"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -56,12 +56,13 @@ func (b *BytesBuilder) AddAddress(address common.Address) {
b.data = append(b.data, address.Bytes()...)
}

func (b *BytesBuilder) AddBytes(data string) {
bytes, err := hex.DecodeString(strings.TrimPrefix(data, "0x"))
func (b *BytesBuilder) AddBytes(data string) error {
bytes, err := hex.DecodeString(hexadecimal.Trim0x(data))
if err != nil {
panic("invalid hex string")
return err
}
b.data = append(b.data, bytes...)
return nil
}

func (b *BytesBuilder) AsHex() string {
Expand Down
39 changes: 17 additions & 22 deletions internal/bytesiterator/bytesiter_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
package bytesiterator

import (
"encoding/hex"
"fmt"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func hexToBytes(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")
return hex.DecodeString(s)
}

func bigIntFromHex(hexStr string) (*big.Int, error) {
b, err := hexToBytes(hexStr)
b, err := hexutil.Decode(hexStr)
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(b), nil
}

func uint32FromHex(hexStr string) (uint32, error) {
b, err := hexToBytes(hexStr)
b, err := hexutil.Decode(hexStr)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -172,7 +167,7 @@ func TestBytesIter_NextByte(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand All @@ -190,7 +185,7 @@ func TestBytesIter_NextByte(t *testing.T) {
if tt.expectError {
assert.Error(t, readErr)
} else {
expected, err := hexToBytes(tt.expectedHex)
expected, err := hexutil.Decode(tt.expectedHex)
require.NoError(t, err)
assert.NoError(t, readErr)
assert.Equal(t, expected, result)
Expand Down Expand Up @@ -222,7 +217,7 @@ func TestBytesIter_NextUint16(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down Expand Up @@ -263,7 +258,7 @@ func TestBytesIter_NextUint24(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down Expand Up @@ -304,7 +299,7 @@ func TestBytesIter_NextUint32(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down Expand Up @@ -343,7 +338,7 @@ func TestBytesIter_NextUint160(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down Expand Up @@ -381,7 +376,7 @@ func TestBytesIter_NextUint256(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down Expand Up @@ -437,7 +432,7 @@ func TestBytesIter_NextBytes(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand All @@ -447,7 +442,7 @@ func TestBytesIter_NextBytes(t *testing.T) {
assert.Error(t, err)
} else {
require.NoError(t, err)
expected, err := hexToBytes(tt.expectedHex)
expected, err := hexutil.Decode(tt.expectedHex)
require.NoError(t, err)
assert.Equal(t, expected, val)
}
Expand Down Expand Up @@ -495,7 +490,7 @@ func TestBytesIter_NextString(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand All @@ -505,7 +500,7 @@ func TestBytesIter_NextString(t *testing.T) {
assert.Error(t, err)
} else {
require.NoError(t, err)
expectedBytes, err := hexToBytes(tt.expectedHex)
expectedBytes, err := hexutil.Decode(tt.expectedHex)
require.NoError(t, err)
expectedStr := string(expectedBytes)
assert.Equal(t, expectedStr, strVal)
Expand Down Expand Up @@ -544,7 +539,7 @@ func TestBytesIter_Rest(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand All @@ -559,7 +554,7 @@ func TestBytesIter_Rest(t *testing.T) {
// Expect no remaining bytes; rest can be nil or empty
assert.Empty(t, rest, "Expected no remaining bytes, but some were found")
} else {
expected, err := hexToBytes(strings.TrimPrefix(tt.expectedHex, "0x"))
expected, err := hexutil.Decode(tt.expectedHex)
require.NoError(t, err)
assert.Equal(t, expected, rest)
}
Expand Down Expand Up @@ -608,7 +603,7 @@ func TestBytesIter_IsEmpty(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := hexToBytes(tt.hexData)
data, err := hexutil.Decode(tt.hexData)
require.NoError(t, err)
iter := New(data)

Expand Down
15 changes: 15 additions & 0 deletions internal/hexadecimal/hexadecimal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hexadecimal

import (
"encoding/hex"
"strings"
)

func IsHexBytes(s string) bool {
_, err := hex.DecodeString(Trim0x(s))
return err == nil
}

func Trim0x(s string) string {
return strings.TrimPrefix(s, "0x")
}
4 changes: 2 additions & 2 deletions sdk-clients/aggregation/examples/approve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/hex"
"fmt"
"log"
"math/big"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/1inch/1inch-sdk-go/constants"
"github.com/1inch/1inch-sdk-go/sdk-clients/aggregation"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

var (
Expand Down Expand Up @@ -62,7 +62,7 @@ func main() {
if err != nil {
log.Fatalf("Failed to get approve data: %v\n", err)
}
data, err := hex.DecodeString(approveData.Data[2:])
data, err := hexutil.Decode(approveData.Data)
if err != nil {
log.Fatalf("Failed to decode approve data: %v\n", err)
}
Expand Down
6 changes: 3 additions & 3 deletions sdk-clients/aggregation/normalization.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package aggregation

import (
"encoding/hex"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/1inch/1inch-sdk-go/constants"
)
Expand All @@ -30,7 +30,7 @@ func normalizeSwapResponse(resp SwapResponse) (*SwapResponseExtended, error) {
return nil, errors.New("invalid 'Value' value")
}

data, err := hex.DecodeString(resp.Tx.Data[2:])
data, err := hexutil.Decode(resp.Tx.Data)
if err != nil {
return nil, errors.New("invalid 'Data' value")
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func normalizeApproveCallDataResponse(resp ApproveCallDataResponse) (*ApproveCal
return nil, errors.New("invalid 'Value' value")
}

data, err := hex.DecodeString(resp.Data[2:])
data, err := hexutil.Decode(resp.Data)
if err != nil {
return nil, errors.New("invalid 'Data' value")
}
Expand Down
6 changes: 3 additions & 3 deletions sdk-clients/aggregation/normalization_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package aggregation

import (
"encoding/hex"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"

"github.com/1inch/1inch-sdk-go/constants"
)

func TestNormalizeSwapResponse(t *testing.T) {
d := "0x0502b1c50000000000000000000000005a98fcbea516cf06857215779fd812ca3bef1b32000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000003b6d0340c558f600b34a5f69dd2f0d06cb8a88d829b7420ade8bb62d"
wantedDataLDOWETH, err := hex.DecodeString(d[2:])
wantedDataLDOWETH, err := hexutil.Decode(d)
assert.NoError(t, err)

testCases := []struct {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestNormalizeSwapResponse(t *testing.T) {

func TestNormalizeApproveCallDataResponse(t *testing.T) {
d := "0x095ea7b30000000000000000000000001111111254eeb25477b68fb85ed929f73a9605820000000000000000000000000000000000000000000000000000000556cd83c2"
ldoApproveData, err := hex.DecodeString(d[2:])
ldoApproveData, err := hexutil.Decode(d)
assert.NoError(t, err)

testCases := []struct {
Expand Down
6 changes: 3 additions & 3 deletions sdk-clients/fusion/examples/place_order/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
const (
usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359"
wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270"
amount = "1000000"
amount = "1000000000000000000"
chainId = 137
)

Expand All @@ -40,8 +40,8 @@ func main() {
}
ctx := context.Background()

fromToken := usdc
toToken := wmatic
fromToken := wmatic
toToken := usdc

response, err := client.GetQuote(ctx, fusion.QuoterControllerGetQuoteParamsFixed{
FromTokenAddress: fromToken,
Expand Down
Loading

0 comments on commit 3cfb070

Please sign in to comment.