Testing EVM contracts
Contents:
- Test using the NEAR Web3 Provider.
- Test by pointing to an Ethereum RPC Proxy address.
NEAR Web3 Provider
At the moment, running tests via NEAR Web3 Provider requires an extra step compared to the NEAR examples developers may be familiar with at:
We'll need to use our betanet account to:
- Run tests that require only one account
- Create new test accounts before running tests that require multiple accounts. Typically, these tests will have accounts interacting with one another, like sending fungible tokens back and forth, for instance.
The first step is to get your account to save a key pair on your local machine. So far the betanet account you created with NEAR Wallet has key pairs living in the browser. We'll want to store a key pair as an unencrypted file in your computer's home directory. (Specifically ~/.near-credentials
.)
To create a local key pair, use the following NEAR CLI command and follow the instructions:
near login
The test file for NEAR Pet Shop is simple and doesn't require the creation of more accounts. Use the command below:
NEAR_MASTER_ACCOUNT=you.betanet truffle test --network near_betanet
NEAR CLI knows to look in the directory where the key pair file was saved after logging in.
An example of a test requiring multiple accounts is this test file from Balancer. Before running a test like this, we'll use the third NEAR CLI command mentioned earlier:
NEAR_ENV=betanet near evm-dev-init mike.betanet 3
This will create 3 subaccounts under mike.betanet
. (It will use timestamps, for instance 1608085465606.mike.betanet
, 1608085468832.mike.betanet
, etc.) If the 3
part of the command was omitted, it would use the default of 5. After creating the necessary number of accounts, run the test with the same command (…truffle test
…) from a moment ago.
Proxy RPC server
Testing can also be done by pointing to a local proxy RPC server. To demonstrate this, we can run the Balancer Core repository (for https://balancer.finance) and a local proxy RPC.
- Follow the directions in the README for the proxy RPC to get a local server running.
- Clone the
balancer-core
repository, following directions according to the README.
Then simply run:
npm run test
The above command is the same as running truffle test
if your system has Truffle installed globally.
Truffle's default network points to localhost
at the port 8545
which is the same as tools like Ganache and the proxy server covered here.