Skip to content

Commit

Permalink
[#2042] Wrong ERC20 decimal rounding in "Set max"
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Sep 22, 2020
1 parent 682722b commit 397ee84
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.62.2
0.62.3
2 changes: 1 addition & 1 deletion services/wallet/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (api *API) GetTransfersByAddress(ctx context.Context, address common.Addres
}

// GetTokensBalances return mapping of token balances for every account.
func (api *API) GetTokensBalances(ctx context.Context, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*big.Int, error) {
func (api *API) GetTokensBalances(ctx context.Context, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*hexutil.Big, error) {
if api.s.client == nil {
return nil, ErrServiceNotInitialized
}
Expand Down
9 changes: 5 additions & 4 deletions services/wallet/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package wallet

import (
"context"
"math/big"
"sync"
"time"

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

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -14,11 +15,11 @@ import (
)

// GetTokensBalances takes list of accounts and tokens and returns mapping of token balances for each account.
func GetTokensBalances(parent context.Context, client *ethclient.Client, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*big.Int, error) {
func GetTokensBalances(parent context.Context, client *ethclient.Client, accounts, tokens []common.Address) (map[common.Address]map[common.Address]*hexutil.Big, error) {
var (
group = NewAtomicGroup(parent)
mu sync.Mutex
response = map[common.Address]map[common.Address]*big.Int{}
response = map[common.Address]map[common.Address]*hexutil.Big{}
)
// requested current head to request balance on the same block number
ctx, cancel := context.WithTimeout(parent, 3*time.Second)
Expand Down Expand Up @@ -48,7 +49,7 @@ func GetTokensBalances(parent context.Context, client *ethclient.Client, account
mu.Lock()
_, exist := response[account]
if !exist {
response[account] = map[common.Address]*big.Int{}
response[account] = map[common.Address]*hexutil.Big{}
}
response[account][token] = balance
mu.Unlock()
Expand Down
20 changes: 11 additions & 9 deletions services/wallet/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/big"
"testing"

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

"github.com/stretchr/testify/suite"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -63,9 +65,9 @@ func (s *BalancesSuite) SetupTest() {

func (s *BalancesSuite) TestBalanceEqualPerToken() {
base := big.NewInt(10)
expected := map[common.Address]map[common.Address]*big.Int{}
expected := map[common.Address]map[common.Address]*hexutil.Big{}
for _, account := range s.accounts {
expected[account] = map[common.Address]*big.Int{}
expected[account] = map[common.Address]*hexutil.Big{}
for i, token := range s.tokens {
balance := new(big.Int).Add(base, big.NewInt(int64(i)))
transactor, err := erc20.NewERC20Transfer(token, s.client)
Expand All @@ -74,7 +76,7 @@ func (s *BalancesSuite) TestBalanceEqualPerToken() {
s.Require().NoError(err)
_, err = bind.WaitMined(context.Background(), s.client, tx)
s.Require().NoError(err)
expected[account][token] = balance
expected[account][token] = (*hexutil.Big)(balance)
}
}
result, err := GetTokensBalances(context.Background(), s.client, s.accounts, s.tokens)
Expand All @@ -84,9 +86,9 @@ func (s *BalancesSuite) TestBalanceEqualPerToken() {

func (s *BalancesSuite) TestBalanceEqualPerAccount() {
base := big.NewInt(10)
expected := map[common.Address]map[common.Address]*big.Int{}
expected := map[common.Address]map[common.Address]*hexutil.Big{}
for i, account := range s.accounts {
expected[account] = map[common.Address]*big.Int{}
expected[account] = map[common.Address]*hexutil.Big{}
for _, token := range s.tokens {
balance := new(big.Int).Add(base, big.NewInt(int64(i)))
transactor, err := erc20.NewERC20Transfer(token, s.client)
Expand All @@ -95,7 +97,7 @@ func (s *BalancesSuite) TestBalanceEqualPerAccount() {
s.Require().NoError(err)
_, err = bind.WaitMined(context.Background(), s.client, tx)
s.Require().NoError(err)
expected[account][token] = balance
expected[account][token] = (*hexutil.Big)(balance)
}
}
result, err := GetTokensBalances(context.Background(), s.client, s.accounts, s.tokens)
Expand All @@ -108,20 +110,20 @@ func (s *BalancesSuite) TestNoBalances() {
s.Require().NoError(err)
for _, account := range s.accounts {
for _, token := range s.tokens {
s.Require().Equal(zero.Int64(), result[account][token].Int64())
s.Require().Equal(zero.Int64(), result[account][token].ToInt().Int64())
}
}
}

func (s *BalancesSuite) TestNoTokens() {
expected := map[common.Address]map[common.Address]*big.Int{}
expected := map[common.Address]map[common.Address]*hexutil.Big{}
result, err := GetTokensBalances(context.Background(), s.client, s.accounts, nil)
s.Require().NoError(err)
s.Require().Equal(expected, result)
}

func (s *BalancesSuite) TestNoAccounts() {
expected := map[common.Address]map[common.Address]*big.Int{}
expected := map[common.Address]map[common.Address]*hexutil.Big{}
result, err := GetTokensBalances(context.Background(), s.client, nil, s.tokens)
s.Require().NoError(err)
s.Require().Equal(expected, result)
Expand Down
10 changes: 6 additions & 4 deletions services/wallet/ierc20/ierc20.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 397ee84

Please sign in to comment.