Skip to content

Commit

Permalink
Expose BalanceAt method
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Nov 17, 2020
1 parent 8af0579 commit 8786268
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/chain/ethereum/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type EthereumClient interface {
bind.ContractBackend
ethereum.ChainReader
ethereum.TransactionReader

BalanceAt(
ctx context.Context,
account common.Address,
blockNumber *big.Int,
) (*big.Int, error)
}

// AddressFromHex converts the passed string to a common.Address and returns it,
Expand Down
14 changes: 14 additions & 0 deletions pkg/chain/ethereum/ethutil/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,17 @@ func (rl *rateLimiter) TransactionReceipt(

return rl.EthereumClient.TransactionReceipt(ctx, txHash)
}

func (rl *rateLimiter) BalanceAt(
ctx context.Context,
account common.Address,
blockNumber *big.Int,
) (*big.Int, error) {
err := rl.acquirePermit()
if err != nil {
return nil, fmt.Errorf("cannot acquire rate limiter permit: [%v]", err)
}
defer rl.releasePermit()

return rl.EthereumClient.BalanceAt(ctx, account, blockNumber)
}
19 changes: 19 additions & 0 deletions pkg/chain/ethereum/ethutil/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,15 @@ func (mec *mockEthereumClient) TransactionReceipt(
return nil, nil
}

func (mec *mockEthereumClient) BalanceAt(
ctx context.Context,
account common.Address,
blockNumber *big.Int,
) (*big.Int, error) {
mec.mockRequest()
return nil, nil
}

func getTests(
client EthereumClient,
) map[string]struct{ function func() error } {
Expand Down Expand Up @@ -639,5 +648,15 @@ func getTests(
return err
},
},
"test BalanceAt": {
function: func() error {
_, err := client.BalanceAt(
context.Background(),
common.Address{},
big.NewInt(0),
)
return err
},
},
}
}

0 comments on commit 8786268

Please sign in to comment.