Skip to content

Commit c0b6a58

Browse files
authored
BCI-3127: erc20 client reads u256 not felt252 (#421)
* read u256 * add comment for clarity
1 parent ab79dc8 commit c0b6a58

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

relayer/pkg/chainlink/erc20/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ func (c *Client) BalanceOf(ctx context.Context, accountAddress *felt.Felt) (*big
5050
return nil, fmt.Errorf("couldn't call balance_of on erc20: %w", err)
5151
}
5252

53-
if len(balanceRes) != 1 {
53+
if len(balanceRes) != 2 {
5454
return nil, fmt.Errorf("unexpected data returned from balance_of on erc20")
5555
}
5656

57-
return starknetutils.FeltToBigInt(balanceRes[0]), nil
57+
// a u256 balance consists of 2 felts (lower 128 bits | higher 128 bits)
58+
balance := starknetutils.FeltArrToBigIntArr(balanceRes)
59+
low := balance[0]
60+
high := balance[1]
61+
62+
// left shift "high" by 128 bits
63+
summand := new(big.Int).Lsh(high, 128)
64+
total := new(big.Int).Add(low, summand)
65+
66+
return total, nil
5867

5968
}
6069

relayer/pkg/chainlink/erc20/client_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"math/big"
89
"net/http"
910
"net/http/httptest"
1011
"testing"
1112
"time"
1213

1314
"github.com/NethermindEth/juno/core/felt"
1415
starknetutils "github.com/NethermindEth/starknet.go/utils"
16+
"github.com/smartcontractkit/chainlink-common/pkg/logger"
1517
"github.com/stretchr/testify/assert"
1618
"github.com/stretchr/testify/require"
1719

18-
"github.com/smartcontractkit/chainlink-common/pkg/logger"
19-
2020
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/starknet"
2121
)
2222

@@ -53,11 +53,10 @@ func TestERC20Client(t *testing.T) {
5353
fmt.Printf("%v %v\n", reqdata.Selector, starknetutils.GetSelectorFromNameFelt("latest_transmission_details").String())
5454
switch reqdata.Selector {
5555
case starknetutils.GetSelectorFromNameFelt("decimals").String():
56-
// latest transmission details response
5756
out = []byte(`{"result":["0x1"]}`)
5857
case starknetutils.GetSelectorFromNameFelt("balance_of").String():
59-
// latest transmission details response
60-
out = []byte(`{"result":["0x0"]}`)
58+
// balance_of returns a u256 which is represented as two felts [lower 128 bits, higher 128 bits]
59+
out = []byte(`{"result":["0x2", "0x9"]}`)
6160
default:
6261
require.False(t, true, "unsupported contract method %s", reqdata.Selector)
6362
}
@@ -80,14 +79,17 @@ func TestERC20Client(t *testing.T) {
8079
client, err := NewClient(reader, lggr, &felt.Zero)
8180
assert.NoError(t, err)
8281

83-
// contractAddress, err := starknetutils.HexToFelt(ocr2ContractAddress)
84-
// require.NoError(t, err)
85-
8682
t.Run("get balance", func(t *testing.T) {
8783
balance, err := client.BalanceOf(context.Background(), &felt.Zero)
8884
require.NoError(t, err)
89-
require.Equal(t, uint64(0), balance.Uint64())
90-
// require.Equal(t, new(big.Int), balance)
85+
86+
// calculate the expected u256 value of two felts [0x2, 0x9]
87+
low := new(big.Int).SetUint64(2)
88+
high := new(big.Int).SetUint64(9)
89+
summand := new(big.Int).Lsh(high, 128)
90+
expectedTotal := new(big.Int).Add(low, summand)
91+
92+
require.Equal(t, expectedTotal.String(), balance.String())
9193
})
9294

9395
t.Run("get decimals", func(t *testing.T) {

0 commit comments

Comments
 (0)