Skip to content

Commit

Permalink
use explicit block number when checking LINK contract balance (#14186)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz <[email protected]>
  • Loading branch information
Tofel and lukaszcl authored Aug 22, 2024
1 parent d884838 commit 8322953
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions integration-tests/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
ethContracts "github.com/smartcontractkit/chainlink/integration-tests/contracts/ethereum"
"github.com/smartcontractkit/chainlink/integration-tests/wrappers"

"github.com/ethereum/go-ethereum/accounts/abi"

Expand Down Expand Up @@ -1024,18 +1025,42 @@ func SendLinkFundsToDeploymentAddresses(

toTransferToMultiCallContract := big.NewInt(0).Mul(linkAmountPerUpkeep, big.NewInt(int64(totalUpkeeps+concurrency)))
toTransferPerClient := big.NewInt(0).Mul(linkAmountPerUpkeep, big.NewInt(int64(operationsPerAddress+1)))
err := linkToken.Transfer(multicallAddress.Hex(), toTransferToMultiCallContract)

// As a hack we use the geth wrapper directly, because we need to access receipt to get block number, which we will use to query the balance
// This is needed as querying with 'latest' block number very rarely, but still, return stale balance. That's happening even though we wait for
// the transaction to be mined.
linkInstance, err := link_token_interface.NewLinkToken(common.HexToAddress(linkToken.Address()), wrappers.MustNewWrappedContractBackend(nil, chainClient))
if err != nil {
return errors.Wrapf(err, "Error transferring LINK to multicall contract")
return err
}

balance, err := linkToken.BalanceOf(context.Background(), multicallAddress.Hex())
tx, err := chainClient.Decode(linkInstance.Transfer(chainClient.NewTXOpts(), multicallAddress, toTransferToMultiCallContract))
if err != nil {
return err
}

if tx.Receipt == nil {
return fmt.Errorf("transaction receipt for LINK transfer to multicall contract is nil")
}

multiBalance, err := linkInstance.BalanceOf(&bind.CallOpts{From: chainClient.Addresses[0], BlockNumber: tx.Receipt.BlockNumber}, multicallAddress)
if err != nil {
return errors.Wrapf(err, "Error getting LINK balance of multicall contract")
}

if balance.Cmp(toTransferToMultiCallContract) < 0 {
return fmt.Errorf("Incorrect LINK balance of multicall contract. Expected at least: %s. Got: %s", toTransferToMultiCallContract.String(), balance.String())
// Old code that's querying latest block
//err := linkToken.Transfer(multicallAddress.Hex(), toTransferToMultiCallContract)
//if err != nil {
// return errors.Wrapf(err, "Error transferring LINK to multicall contract")
//}
//
//balance, err := linkToken.BalanceOf(context.Background(), multicallAddress.Hex())
//if err != nil {
// return errors.Wrapf(err, "Error getting LINK balance of multicall contract")
//}

if multiBalance.Cmp(toTransferToMultiCallContract) < 0 {
return fmt.Errorf("Incorrect LINK balance of multicall contract. Expected at least: %s. Got: %s", toTransferToMultiCallContract.String(), multiBalance.String())
}

// Transfer LINK to ephemeral keys
Expand All @@ -1060,18 +1085,24 @@ func SendLinkFundsToDeploymentAddresses(
}
boundContract := bind.NewBoundContract(multicallAddress, multiCallABI, chainClient.Client, chainClient.Client, chainClient.Client)
// call aggregate3 to group all msg call data and send them in a single transaction
_, err = chainClient.Decode(boundContract.Transact(chainClient.NewTXOpts(), "aggregate3", call))
ephemeralTx, err := chainClient.Decode(boundContract.Transact(chainClient.NewTXOpts(), "aggregate3", call))
if err != nil {
return errors.Wrapf(err, "Error calling Multicall contract")
}

if ephemeralTx.Receipt == nil {
return fmt.Errorf("transaction receipt for LINK transfer to ephemeral keys is nil")
}

for i := 1; i <= concurrency; i++ {
balance, err := linkToken.BalanceOf(context.Background(), chainClient.Addresses[i].Hex())
ephemeralBalance, err := linkInstance.BalanceOf(&bind.CallOpts{From: chainClient.Addresses[0], BlockNumber: ephemeralTx.Receipt.BlockNumber}, chainClient.Addresses[i])
// Old code that's querying latest block, for now we prefer to use block number from the transaction receipt
//balance, err := linkToken.BalanceOf(context.Background(), chainClient.Addresses[i].Hex())
if err != nil {
return errors.Wrapf(err, "Error getting LINK balance of ephemeral key %d", i)
}
if balance.Cmp(toTransferPerClient) < 0 {
return fmt.Errorf("Incorrect LINK balance after transfer. Ephemeral key %d. Expected: %s. Got: %s", i, toTransferPerClient.String(), balance.String())
if ephemeralBalance.Cmp(toTransferPerClient) < 0 {
return fmt.Errorf("Incorrect LINK balance after transfer. Ephemeral key %d. Expected: %s. Got: %s", i, toTransferPerClient.String(), ephemeralBalance.String())
}
}

Expand Down

0 comments on commit 8322953

Please sign in to comment.