The Algebra Guides
In this step-by-step guide, we'll try to provide all the necessary pieces of information, hints, and tools for you to create a whole development environment, that you can further use to build the rest of the examples in the Guides section of the docs, or start your own project with our integration!
In order not to waste time and to provide the most comfortable structure to all readers, we have created two different options. You can find the Quick Start section below, in which you can clone a boilerplate and start building on your own, without diving deep into the base, or you can start from scratch and learn all the underlying concepts, by clicking on the Start from Scratch section. Both sections are ok as long as you know exactly what are your goals at this point. Letâs get started!
Quick Start
The Algebra boilerplate repo provides those who want to build with our codebase with a basic Hardhat environment with required imports already preloaded for you. You can simply clone it and install the dependencies:
Then go to the Local Node with a Polygon Fork
to complete your set-up and start building.
Start from Scratch
Node is considered to be one of the most common Javascript execution mechanisms. That means that for our goals, it will provide scripting that we can further use to compile and test our contracts. If you havenât already done so, install NodeJS and its package manager NPM before the start.
Set Up Dependencies
Node is one of the most common Javascript runtimes. For our purposes it will provide scripting we can use to compile and test our contracts. If you havenât already, install NodeJS and its package manager NPM.
Once those dependencies are set up, we can initialize our project:
This is the Ethereum's development toolset â a vital development environment, providing quite a lot of features, such as Solidity compilation, testing and deployment. All of them are contained in a single convenient wrapper, making it easy to use. Weâll use NPM to add Hardhat to our project:
By installing Hardhat, we can bring it up to create a development environment. When you first run Hardhat, youâll have three options of running a project: start with a templated Javascript; start with a Typescript project, or an empty project. Taking into account that Hardhat relies heavily on folder structure, we strongly recommend you to start with any of the templated options. Initialize Hardhat, follow every prompt to make your selection, and answer yes to the follow-up prompts:
After Hardhat has finished initializing, take a close look at what has been installed. The folder structure should be intuitive and easy to navigate through. Keep in mind that ./contracts is where youâll write your Solidity contracts, ./test is where youâll write your tests and ./scripts is where you can write scripts to perform actions like deploying. Remember! Hardhat is designed to use this exact folder structure, so make sure you donât change anything unless you know what youâre doing.
For the next step, weâll use NPM to add the Algebra contracts, which will allow us to seamlessly integrate with the protocol in our new contracts:
The Algebra contracts were written using a past version of the solidity compiler. Since weâre building integrations using the Algebra codebase, we have to make Hardhat to use the correct compiler to build these files. Go to the ./hardhat.config.js
file and change the Solidity version to â0.7.6â:
Now, weâre done! After all these steps, you should have a functional development environment, allowing you to start building on-chain Algebra integrations. For now, letâs run a quick test to make sure everything is set up just as it should be.
Creating a Main Contract
To make sure that our environment is set up correctly, weâll try to compile a basic Swap. For that purpose, create a new file, ./contracts/Swap.sol
and paste the following code into it.
Single Swaps Contract Guide
In order to compile all the contracts in the ./contracts
folder, we will have to use the Hardhat compile command:
If the environment is compiled properly, youâll see the message pop up:
Local Node with a Polygon Fork
In the processes of building and testing integrations with on-chain protocols, developers might face a major issue: liquidity on the live chain is critical to thoroughly testing their code, but testing against a live network like Polygon can be extremely expensive.
Luckily, Hardhat has a great feature that allows developers to run a local Polygon test node that uses a fork of Polygon. This way, it is possible for us to test against simulated liquidity for free.
As a prerequisite, weâll need an RPC that supports Forking. Alchemy includes forking in its free tier, so itâs a great place to start.
Sign up and get an Alchemy API key
After that, you're free to run the following Hardhat command to start your node:
With your local node is running, you are able to use the --network localhost
flag in tests to direct the Hardhat testing suite to that local host:
Next Steps
Once your development environment is set up, youâre all ready to start building. Enter the Guides section to learn more about the Algebra functions that you can integrate with. Donât forget to add all contracts (.sol files) to the ./contracts
folder and their subsequent tests to the ./tests
folder. You can then test them against your local forked node by running: