Skip to content

Commit

Permalink
BCI-3127: erc20 client reads u256 not felt252 (#421)
Browse files Browse the repository at this point in the history
* read u256

* add comment for clarity
  • Loading branch information
augustbleeds authored Apr 26, 2024
1 parent ab79dc8 commit c0b6a58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
13 changes: 11 additions & 2 deletions relayer/pkg/chainlink/erc20/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@ func (c *Client) BalanceOf(ctx context.Context, accountAddress *felt.Felt) (*big
return nil, fmt.Errorf("couldn't call balance_of on erc20: %w", err)
}

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

return starknetutils.FeltToBigInt(balanceRes[0]), nil
// a u256 balance consists of 2 felts (lower 128 bits | higher 128 bits)
balance := starknetutils.FeltArrToBigIntArr(balanceRes)
low := balance[0]
high := balance[1]

// left shift "high" by 128 bits
summand := new(big.Int).Lsh(high, 128)
total := new(big.Int).Add(low, summand)

return total, nil

}

Expand Down
22 changes: 12 additions & 10 deletions relayer/pkg/chainlink/erc20/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/NethermindEth/juno/core/felt"
starknetutils "github.com/NethermindEth/starknet.go/utils"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"

"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/starknet"
)

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

// contractAddress, err := starknetutils.HexToFelt(ocr2ContractAddress)
// require.NoError(t, err)

t.Run("get balance", func(t *testing.T) {
balance, err := client.BalanceOf(context.Background(), &felt.Zero)
require.NoError(t, err)
require.Equal(t, uint64(0), balance.Uint64())
// require.Equal(t, new(big.Int), balance)

// calculate the expected u256 value of two felts [0x2, 0x9]
low := new(big.Int).SetUint64(2)
high := new(big.Int).SetUint64(9)
summand := new(big.Int).Lsh(high, 128)
expectedTotal := new(big.Int).Add(low, summand)

require.Equal(t, expectedTotal.String(), balance.String())
})

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

0 comments on commit c0b6a58

Please sign in to comment.