Skip to content

Commit

Permalink
feat(accounts): change SmartAccount.Balance to utilize CallOpts
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `SmartAccount.Balance` method utilizes `CallOpts`.
  • Loading branch information
danijelTxFusion committed Nov 5, 2024
1 parent 1d28994 commit e4155d6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 69 deletions.
16 changes: 6 additions & 10 deletions accounts/smart_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/zksync-sdk/zksync2-go/clients"
Expand Down Expand Up @@ -76,31 +75,28 @@ func (a *SmartAccount) Address() common.Address {

// Balance returns the balance of the specified token that can be either base token or any ERC20 token.
// The block number can be nil, in which case the balance is taken from the latest known block.
func (a *SmartAccount) Balance(ctx context.Context, token common.Address, at *big.Int) (*big.Int, error) {
err := a.cacheData(ctx)
func (a *SmartAccount) Balance(opts *CallOpts, token common.Address) (*big.Int, error) {
callOpts := ensureCallOpts(opts).ToCallOpts(a.Address())
err := a.cacheData(callOpts.Context)
if err != nil {
return nil, err
}

if token == utils.LegacyEthAddress || token == utils.EthAddressInContracts {
token, err = a.client.L2TokenAddress(ctx, token)
token, err = a.client.L2TokenAddress(callOpts.Context, token)
if err != nil {
return nil, err
}
}

if token == utils.L2BaseTokenAddress {
return a.client.BalanceAt(ensureContext(ctx), a.Address(), at)
return a.client.BalanceAt(ensureContext(callOpts.Context), a.Address(), callOpts.BlockNumber)
}
erc20Token, err := erc20.NewIERC20(token, a.client)
if err != nil {
return nil, err
}
return erc20Token.BalanceOf(&bind.CallOpts{
From: a.Address(),
BlockNumber: at,
Context: ensureContext(ctx),
}, a.Address())
return erc20Token.BalanceOf(callOpts, a.Address())
}

// AllBalances returns all balances for confirmed tokens given by an associated account.
Expand Down
4 changes: 2 additions & 2 deletions test/account_abstraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestIntegration_ApprovalPaymaster(t *testing.T) {
assert.NoError(t, err, "client.WaitMined should not return an error")

// Read token and base token balances from user and paymaster accounts
balanceBefore, err := wallet.Balance(context.Background(), utils.L2BaseTokenAddress, nil)
balanceBefore, err := wallet.Balance(nil, utils.L2BaseTokenAddress)
assert.NoError(t, err, "Balance should not return an error")

tokenBalanceBefore, err := token.BalanceOf(nil, wallet.Address())
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestIntegration_ApprovalPaymaster(t *testing.T) {
_, err = client.WaitMined(context.Background(), hash)
assert.NoError(t, err, "client.WaitMined should not return an error")

balanceAfter, err := wallet.Balance(context.Background(), utils.L2BaseTokenAddress, nil)
balanceAfter, err := wallet.Balance(nil, utils.L2BaseTokenAddress)
assert.NoError(t, err, "Balance should not return an error")

tokenBalanceAfter, err := token.BalanceOf(nil, wallet.Address())
Expand Down
Loading

0 comments on commit e4155d6

Please sign in to comment.