100 Days of Web3 (Days 61-70)
I finished up with my second Web3 project during days 61-70 of the 100 Days of Web3 challenge. The project is a simplified version of the Maker protocol built using Foundry. I was also finally able to start learning about layer two solutions like Polygon.
Day 61
Having finished up with the core contracts of the simplified Maker protocol, I moved on to implementing my own version of the dog contract. In the Maker protocol, the dog contract allows users to liquidate unhealthy vaults. I decided to call the contract Liquidator instead of dog. Although the Maker protocol’s dog contract incentivizes users to liquidate the vault by paying them a reward, I decided to remove this feature to keep the functionality simple.
I also implemented a simplified version of the clipper contract which I called Auctioneer. The Auctioneer contract has three main methods which do the following:
- Start a collateral auction
- Restart a collateral auction
- Buy auctioned collateral
Day 62
Next, I wrote some tests for the Auctioneer contract. It was tricky to write these tests because some conditions depended on how much time had passed since the auction started. However, I was able to simulate time passing by using Foundry’s warp cheatcode. I tried to write tests which covered most common scenarios, including one where two different users purchased collateral from the auction and surplus collateral was returned to the owner.
Day 63
I then created the RateUpdater contract which was my equivalent of the pot contract from the Maker protocol. The main purpose of this contract is to update the stability fees of the collateral within the Vaults contract. The effect of the RateUpdater depends on how much time has passed since the rate was last updated. Once again, I used the warp cheatcode from Foundry to test the contract. In order to calculate the new stability fee, I had to use an exponential function. I took the implementation of that function directly from the Maker pot contract. The function was written in assembly, presumably for gas-efficiency.
Day 64
In order to demonstrate the functionality of the Simple Maker protocol, I also needed to implement some collateral tokens. I created a very simple implementation of a collateral token by leveraging the ERC20 contract from OpenZeppelin. I added a faucet method to the contract for easier testing.
Before starting with the contract deployment, I decided to write a few more tests. I wrote one test which goes through the full lifecycle of a user migrating their collateral tokens to the protocol, opening a vault, minting Dai and then paying it back. I realized that the steps in the setup method of the test, would be the same steps required to deploy and initialize the contracts in the protocol.
Day 65
I wrote one more extensive test which follows the sequence below:
- User A opens a vault and withdraws the maximum amount of Dai
- The test simulates the passing of 1 day using the warp cheatcode
- Updates the stability fee using the RateUpdater contract, causing user A’s vault to become unsafe
- User B liquidates user A’s vault, thereby starting a new collateral auction
- User B buys all of user A’s collateral
Day 66
Since I was satisfied with my tests, I began to work on a deployment script. Since I used Foundry to develop this project, I decided to use Foundry’s Cast tool. Cast is a command line tool which can be used to interact with and deploy contracts. I’m not very experienced with shell scripting, so it took me quite a while to find all the right commands. However, I was finally able to deploy the Vaults contract to the Kovan network. I used the sed utility to extract the contract address from the output of the deployment command.
Day 67
I completed the deployment script by using Foundry’s Cast tool to deploy all contracts in the protocol to the Kovan testnet. I also added all the necessary contract calls to set up the protocol to work with the test collateral token I created. These calls included making the necessary contracts authorize each other as well as setting various parameters on each contract such as the collateral token price, the auction duration and the stability fee.
Developing smart contracts and writing tests is a pleasure with Foundry. However, writing the deployment scripts felt clunky and slow. When it comes to deployments and scripting, Foundry falls short of Hardhat. However, I’d say that Foundry has the upper hand when it comes to testing.
Day 68
Having set up and deployed all contracts to the testnet, I decided to write a script to test them. This was once again a bash script which made heavy use of the Cast tool from Foundry. The script was similar to one of the unit tests I had written but interacted with the contracts deployed to the Kovan network. The script did the following:
- Minted collateral tokens for the user
- Migrated the tokens into the protocol
- Opened a vault
- Minted Dai for the user
- Made the user pay back the Dai and close the vault
Day 69
I added one more script to test the liquidation worfklow of the protocol. Once again, the script mimicked one of the unit tests I had already written. I was puzzled at first how I could make the vault unsafe in order to liquidate it. I settled on temporarily dropping the price of the collateral token so that the debt of the vault exceeded the collateral value. I was then able to run the liquidation script. I changed the price of the collateral token back after testing the liquidation script.
The Simple Maker protocol was finally complete. I updated the readme file on GitHub with details on the project and how to run it. You can check it out here.
Day 70
One of the most important and exciting topics in Web3 is the use of layer two solutions. One incredibly popular layer two solution is Polygon. I decided to take the Polygon course from EatTheBlocks, since I still had some time left on my subscription! During the initial part of the course, the instructor explained the architecture of the Polygon network including the following layers:
- Ethereum Layer: The Polygon contracts deployed on Ethereum
- Heimdall Layer: Securely syncs state between Ethereum and Polygon
- Bor Layer: Produces blocks in the Polygon chain
I also learned about PolygonScan, which is Polygon’s block explorer. I experimented with the PolygonScan API by doing some basic tasks such as retrieving token balances.
Summary
The Simple Maker protocol took longer to finish than I had expected, and I was glad to finally complete it. I’m looking forward to completing the Polygon course and then exploring another layer two solution like Arbitrum. It would also be nice if I could create a small project which involved a layer two chain. The next step after that would be writing contracts for a non-EVM chain like Solana.