From 397ee840c650b7fd4a414b38e200274d2d1b716c Mon Sep 17 00:00:00 2001 From: andrey Date: Tue, 15 Sep 2020 13:46:28 +0200 Subject: [PATCH] [#2042] Wrong ERC20 decimal rounding in "Set max" --- VERSION | 2 +- services/wallet/api.go | 2 +- services/wallet/balance.go | 9 +++++---- services/wallet/balance_test.go | 20 +++++++++++--------- services/wallet/ierc20/ierc20.go | 10 ++++++---- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/VERSION b/VERSION index 92c648bd8..eb324e6fd 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.62.2 +0.62.3 diff --git a/services/wallet/api.go b/services/wallet/api.go index 56e658597..72fc4127c 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -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 } diff --git a/services/wallet/balance.go b/services/wallet/balance.go index 8dfc9d0c1..f79acb090 100644 --- a/services/wallet/balance.go +++ b/services/wallet/balance.go @@ -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" @@ -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) @@ -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() diff --git a/services/wallet/balance_test.go b/services/wallet/balance_test.go index 1bbe0ef25..5804599c5 100644 --- a/services/wallet/balance_test.go +++ b/services/wallet/balance_test.go @@ -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" @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/services/wallet/ierc20/ierc20.go b/services/wallet/ierc20/ierc20.go index 8ddfd6dc8..834ca2739 100644 --- a/services/wallet/ierc20/ierc20.go +++ b/services/wallet/ierc20/ierc20.go @@ -7,6 +7,8 @@ import ( "math/big" "strings" + "github.com/ethereum/go-ethereum/common/hexutil" + ethereum "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -217,26 +219,26 @@ func (_IERC20 *IERC20CallerSession) Allowance(owner common.Address, spender comm // BalanceOf is a free data retrieval call binding the contract method 0x70a08231. // // Solidity: function balanceOf(address account) constant returns(uint256) -func (_IERC20 *IERC20Caller) BalanceOf(opts *bind.CallOpts, account common.Address) (*big.Int, error) { +func (_IERC20 *IERC20Caller) BalanceOf(opts *bind.CallOpts, account common.Address) (*hexutil.Big, error) { var ( ret0 = new(*big.Int) ) out := ret0 err := _IERC20.contract.Call(opts, out, "balanceOf", account) - return *ret0, err + return (*hexutil.Big)(*ret0), err } // BalanceOf is a free data retrieval call binding the contract method 0x70a08231. // // Solidity: function balanceOf(address account) constant returns(uint256) -func (_IERC20 *IERC20Session) BalanceOf(account common.Address) (*big.Int, error) { +func (_IERC20 *IERC20Session) BalanceOf(account common.Address) (*hexutil.Big, error) { return _IERC20.Contract.BalanceOf(&_IERC20.CallOpts, account) } // BalanceOf is a free data retrieval call binding the contract method 0x70a08231. // // Solidity: function balanceOf(address account) constant returns(uint256) -func (_IERC20 *IERC20CallerSession) BalanceOf(account common.Address) (*big.Int, error) { +func (_IERC20 *IERC20CallerSession) BalanceOf(account common.Address) (*hexutil.Big, error) { return _IERC20.Contract.BalanceOf(&_IERC20.CallOpts, account) }