Plugin Testing
Introduction
Auto-testing is a critical component of smart contract development, ensuring that your Algebra Plugin functions as intended and are free from vulnerabilities. Automated tests simulate interactions with your hook contract, validate edge cases, and verify gas efficiency. This guide covers how to set up and run auto-tests for a Algebra Plugin using Hardhat, a popular Ethereum development framework.
Setting Up
Algebra provides a basic test suite in the algebra-plugin-template repo. It contains fixture for deployments before each test and some utility functions that you might want to use.
Developing auto-tests with Hardhat usually starts with writing a fixture. In the repo it is present as test/shared/fixtures.ts file. Firstly, we have to add some imports from Algebra core package to extract bytecode and ABI for Pool and a types for some contracts:
Then, we define an interface for our fixture.
With this one we will be able to further acces Plugin Factory, Plugin, a Pool it is attached to and Tokens which represent a Pool.
Preparations are ready, now we can implement the actual logic of the fixture:
Let's break down the code above:
Executes entrypointFixture which provides us a Custom Entrypoint, a Pool Factory and a Tokens addresses.
Deploys a Plugin Factory using artifacts. Custom Entrypoint's adderss is required as a constructor argument.
Deploys a Custom Pool via a call to Plugin Factory contract.
Then it fetches deployed pool's address via Factory.
On the nexts rows it creates a pool object for the later usage when writing tests.
Similarly, it fetches a Plugin address and creates its object.
Writing Tests
Our fixture is ready to be used in tests to deploy a fresh set of contracts before each test. We will walk through some tests present in the test/DynamicFeePlugin.spec.ts file.
We start with defining some variables used in tests and before
, beforeEach
hooks for a convinince:
wallet
represents a wallet which acts as a signer (origin) of every transaction.other
is some random wallet. In case you would like to use. For example to track their balances or find its address in some event.plugin
,pluginFactory
,pool
are initialized inbeforeEach
hook (beofre each test).
Finally, let's write some tests for beforeSwap
hook:
Here we have 2 tests checking that our Plugin returns right fee values on zeroToOne
and oneToZero
swaps.
The key thing here is that to able to call beforeSwap hook on our plugin we have to "impersonate" the pool. Send a transaction where from is the pool's address. This is because beforeSwap hook is protected by onlyPool
modifier. So we make a staticCall
(simulation) which allows us to do so.
Inside the tests whe check that:
in case of
zeroToOne
the plugin returns overrideFee = 0.5%, pluginFee = 1%in case of
oneToZero
the plugin returns overrideFee = 1%, pluginFee = 1%
Next Steps
Congratulations on building and testing your very first Algebra Plugin! Now you are ready to proceed to a deployment of your masterpiece. Plugin Deployment section will help you with that task.
Last updated