-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp(tests): Add function to get ERC-20 balance to integration utils (…
…#2635) * add get erc20 balance util * add changelog entry * add missing license
- Loading branch information
1 parent
d2cad75
commit c6f7fcf
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright Tharsis Labs Ltd.(Evmos) | ||
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) | ||
|
||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/evmos/evmos/v18/contracts" | ||
"github.com/evmos/evmos/v18/testutil/integration/evmos/network" | ||
evmtypes "github.com/evmos/evmos/v18/x/evm/types" | ||
) | ||
|
||
// GetERC20Balance returns the token balance of a given account address for | ||
// an ERC-20 token at the given contract address. | ||
func GetERC20Balance(nw network.Network, tokenAddress, accountAddress common.Address) (*big.Int, error) { | ||
input, err := contracts.ERC20MinterBurnerDecimalsContract.ABI.Pack( | ||
"balanceOf", | ||
accountAddress, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
callData, err := json.Marshal(evmtypes.TransactionArgs{ | ||
To: &tokenAddress, | ||
Input: (*hexutil.Bytes)(&input), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ethRes, err := nw.GetEvmClient().EthCall( | ||
nw.GetContext(), | ||
&evmtypes.EthCallRequest{ | ||
Args: callData, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var balance *big.Int | ||
err = contracts.ERC20MinterBurnerDecimalsContract.ABI.UnpackIntoInterface(&balance, "balanceOf", ethRes.Ret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return balance, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package utils_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/evmos/evmos/v18/contracts" | ||
testfactory "github.com/evmos/evmos/v18/testutil/integration/evmos/factory" | ||
testhandler "github.com/evmos/evmos/v18/testutil/integration/evmos/grpc" | ||
testkeyring "github.com/evmos/evmos/v18/testutil/integration/evmos/keyring" | ||
testnetwork "github.com/evmos/evmos/v18/testutil/integration/evmos/network" | ||
"github.com/evmos/evmos/v18/testutil/integration/evmos/utils" | ||
evmtypes "github.com/evmos/evmos/v18/x/evm/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetERC20Balance(t *testing.T) { | ||
keyring := testkeyring.New(1) | ||
network := testnetwork.NewUnitTestNetwork( | ||
testnetwork.WithPreFundedAccounts(keyring.GetAllAccAddrs()...), | ||
) | ||
handler := testhandler.NewIntegrationHandler(network) | ||
factory := testfactory.New(network, handler) | ||
|
||
sender := keyring.GetKey(0) | ||
mintAmount := big.NewInt(100) | ||
|
||
// Deploy an ERC-20 contract | ||
erc20Addr, err := factory.DeployContract( | ||
sender.Priv, | ||
evmtypes.EvmTxArgs{}, | ||
testfactory.ContractDeploymentData{ | ||
Contract: contracts.ERC20MinterBurnerDecimalsContract, | ||
ConstructorArgs: []interface{}{"TestToken", "TT", uint8(18)}, | ||
}, | ||
) | ||
require.NoError(t, err, "failed to deploy contract") | ||
require.NoError(t, network.NextBlock(), "failed to advance block") | ||
|
||
balance, err := utils.GetERC20Balance(network, erc20Addr, sender.Addr) | ||
require.NoError(t, err, "failed to get ERC20 balance") | ||
require.Equal(t, common.Big0.Int64(), balance.Int64(), "expected no balance before minting") | ||
|
||
// Mint some tokens | ||
_, err = factory.ExecuteContractCall( | ||
sender.Priv, | ||
evmtypes.EvmTxArgs{ | ||
To: &erc20Addr, | ||
}, | ||
testfactory.CallArgs{ | ||
ContractABI: contracts.ERC20MinterBurnerDecimalsContract.ABI, | ||
MethodName: "mint", | ||
Args: []interface{}{sender.Addr, mintAmount}, | ||
}, | ||
) | ||
require.NoError(t, err, "failed to mint tokens") | ||
|
||
require.NoError(t, network.NextBlock(), "failed to advance block") | ||
|
||
balance, err = utils.GetERC20Balance(network, erc20Addr, sender.Addr) | ||
require.NoError(t, err, "failed to get ERC20 balance") | ||
require.Equal(t, mintAmount.Int64(), balance.Int64(), "expected different balance after minting") | ||
} |