Multi-chain Testing Using Starknet
Learn how you can test connecting to multiple networks and providers, if you wanted to test on both Starknet and Ethereum at the same time.
Introduction to Multi-chain Testing
The Ape software is an elegant and efficient way to build smart contracts on multiple blockchains. With our modular approach, you can easily create your own code base and deploy it across multiple chains.
Testing smart contracts in a multi-chain environment doesn't have to be complicated. Ape provides a modular approach making your builder journey easy and efficient.
In this tutorial, we will walk you through multi-chain testing with Starknet. We will review how you can test connecting to multiple networks and providers, like testing both Starknet and Ethereum contracts simultaneously.
The reason why ApeWorX supports multi-chain testing is that many dApps are multi-chain. While this tutorial is an example of L1 and L2 testing, you can also test on other chains unrelated to Ethereum. We wanted to create something that would make it more accessible for developers to test and develop on whatever chain they want to work with.
Developers can also find information for testing and multi-chain testing in our documentation here.
I will use a demo project from our team to show how this testing works. You can find the link to the GitHub repo here.
Setting up the demo Project
This demo project already contains both Cairo and Vyper contracts used by Starknet and Ethereum, respectively.
Let’s go into ape-config.yaml. If you want to set a default ecosystem for your ape project, you need to set the default ecosystem by putting this line below the plugins:
To see how the multi-chain testing works, let’s first go into the conftest.py file and see that we have already set up pytest fixtures for the Ethereum and Starknet accounts.
Now let’s go into the test_multichain.py file. Here, we have also set up two pytest fixtures for the Ethereum and Starknet contracts.
We have also set up individual tests to ensure that the Ethereum and Starknet contracts are working correctly.
Testing Multi-chain
Now let’s focus on the function for testing multi-chain in the same test. We start the test connected to the Starknet network and then test to ensure that the Starknet contract works correctly.
Then we switch the current active network for all the code under the indent using the line
with networks.parse_network_choice(“ethereum:local”):
This line adds the provider to the top of the stack.
We then assert the test again to make sure that the lines below the network switch run properly.
Following the test, we switch back to the default network, Starknet, and show that the test has switched back to the default provider.
The Ethereum network has not been disconnected; it's just no longer the active network. The new active network will be the previous network that was at the top of the stack.
Ape only disconnects from all providers at the end of the test. This lets you switch between different networks as much as you need to during the test.
Now we can run ape test in the terminal to see that all the tests work correctly.
We will also show this by running the script multichain_demo.py using ape run multichain_demo.
In this example, we start connected with the default network provider, Starknet. Then we switch the active network to Ethereum, Fantom, and Arbitrum. After connecting to each network, we exit the with block and pop the network off the stack. The active network provider changes to the previous network at the top of the stack. You can see it in action where we print the current active network provider name. In order, it will show Starknet, Ethereum, Fantom, Arbitrum, Fantom, Ethereum, and Starknet.
Ape simplifies the process of creating a test environment for your smart contracts by providing a simple interface for configuring the network, accounts, and keys. This lets you to focus on writing your tests instead of configuring the network. If you have any questions, visit our discord and connect with peers to learn how to optimize your dApps and on-chain interactions.