hex parser + test

This commit is contained in:
Stephan D
2025-12-24 03:53:20 +01:00
parent 915ed66b08
commit 342dd5328f
5 changed files with 73 additions and 28 deletions

View File

@@ -10,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/shopspring/decimal"
"github.com/tech/sendico/gateway/chain/internal/service/gateway/rpcclient"
@@ -199,17 +198,11 @@ func erc20Decimals(ctx context.Context, client *rpc.Client, token common.Address
if err := client.CallContext(ctx, &hexResp, "eth_call", call, "latest"); err != nil {
return 0, merrors.Internal("decimals call failed: " + err.Error())
}
val, err := hexutil.DecodeBig(hexResp)
val, err := shared.DecodeHexUint8(hexResp)
if err != nil {
return 0, merrors.Internal("decimals decode failed: " + err.Error())
}
if val == nil {
return 0, merrors.Internal("decimals decode failed: empty response")
}
if val.BitLen() > 8 {
return 0, merrors.Internal("decimals decode failed: value out of range")
}
return uint8(val.Uint64()), nil
return val, nil
}
func toBaseUnits(amount string, decimals uint8) (*big.Int, error) {

View File

@@ -8,9 +8,9 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/shopspring/decimal"
"github.com/tech/sendico/gateway/chain/internal/service/gateway/shared"
"github.com/tech/sendico/gateway/chain/storage/model"
"github.com/tech/sendico/pkg/merrors"
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
@@ -104,17 +104,11 @@ func readDecimals(ctx context.Context, client *rpc.Client, token string) (uint8,
if err := client.CallContext(ctx, &hexResp, "eth_call", call, "latest"); err != nil {
return 0, merrors.Internal("decimals call failed: " + err.Error())
}
val, err := hexutil.DecodeBig(hexResp)
val, err := shared.DecodeHexUint8(hexResp)
if err != nil {
return 0, merrors.Internal("decimals decode failed: " + err.Error())
}
if val == nil {
return 0, merrors.Internal("decimals decode failed: empty response")
}
if val.BitLen() > 8 {
return 0, merrors.Internal("decimals decode failed: value out of range")
}
return uint8(val.Uint64()), nil
return val, nil
}
func readBalanceOf(ctx context.Context, client *rpc.Client, token string, wallet string) (*big.Int, error) {
@@ -132,7 +126,7 @@ func readBalanceOf(ctx context.Context, client *rpc.Client, token string, wallet
if err := client.CallContext(ctx, &hexResp, "eth_call", call, "latest"); err != nil {
return nil, merrors.Internal("balanceOf call failed: " + err.Error())
}
bigVal, err := hexutil.DecodeBig(hexResp)
bigVal, err := shared.DecodeHexBig(hexResp)
if err != nil {
return nil, merrors.Internal("balanceOf decode failed: " + err.Error())
}