Waffle
Last updated
Last updated
Waffle is a library for compiling and testing smart contracts, and Mars is a deployment manager. Waffle and Mars can be used together to write, compile, test, and deploy Ethereum smart contracts. Due to the Ethereum compatibility of Fractal, smart contracts can be deployed to the Fractal Devnet testnet using Waffle and Mars.
Waffle uses minimal dependencies, has an easy-to-learn and expand writing syntax, and provides fast execution time when compiling and testing smart contracts. In addition, the compatibility of Waffle and TypeScript and the use of Chai matchers make it easy to view and write tests.
Mars provides a simple, TypeScript-compatible framework for creating advanced deployment scripts and keeping them in sync with state changes. Mars focuses on "infrastructure as code", allowing developers to specify how to deploy their smart contracts, and then use these specifications to automatically handle state changes and deployment.
In this tutorial, you need to create a TypeScript project first, then use Waffle to write, compile and test the smart contract, and then use Mars to deploy it to the Fractal Devnet.
This tutorial requires Node.js to be installed. You can download it through or run the following code to complete the installation. You can verify the correct installation by requesting the version of each installation package:
Here we use the truffle project (please refer to the first 3 steps in to create a project module using Truffle), then configure the package.json file, and add dependencies in devDependencies:
Need to reinstall dependencies
Create a TypeScript configuration file
Add basic TypeScript configuration
You should now have a basic TypeScript project with the dependencies needed to build with Waffle and Mars.
Go back to the root directory of the project and create a waffle.json
file to configure Waffle:
Edit waffle.json
to specify the compiler configuration, including the contract directory, etc. In this example, we will use solcjs and the version of Solidity you used for the contract, which is 0.6.12
:
Add a script in package.json
to run Waffle:
This is all the steps to configure Waffle, now you can use the build script to compile the MyContract contract:
Create a file (MyContract.test.ts
) in the test directory to test your MyContract contract:
Now, you need to configure the deployment of the MyContract contract for the Fractal Devnet.
You need to generate artifacts for Mars to enable type-checking in the deployment script:
Update existing script to run Waffle in package.json
to include Mars:
Generate artifacts and create the artifacts.ts
file required for deployment
If you open the build directory, you can see an artifacts.ts
file, which contains artifact data required for deployment. You need to write a deployment script before you can proceed with the deployment. The deployment script will be used to explain which contract Mars deploys, which network to deploy to, and which account is used to trigger the deployment.
In this step, you will create a deployment script that will define how the contract should be deployed. Mars provides a deploy function to which you can pass options, such as the account private key used to deploy the contract, the network to be deployed, and so on. The deploy function is used to define the contract to be deployed. Mars has a contract function that accepts name, artifact and constructorArgs.
Create a src directory to contain your deployment script and create a script to deploy the MyContract contract:
In deploy.ts
, use Mars' deploy function to create a script and deploy to Fractal Devnet using your account’s private key:
Set the deploy function to deploy the MyContract contract created in the above steps:
Add the deployment script to the scripts object in package.json
:
So far, you should have created a deployment script in deploy.ts to deploy the MyContract contract to Fractal Devnet, and added the ability to easily call the script and deploy the contract.
If you have configured the deployment, you can now actually deploy to Fractal Devnet.
Now deploy the contract using the script you just created:
Congratulations! You have successfully deployed the contract on Fractal Devnet through Waffle and Mars!
- for writing, compiling, and testing smart contracts
- for interacting with Fractal's Ethereum API
- the contract you'll be creating will use OpenZeppelin's ERC20 base implementation
- the project will be a TypeScript project
- for executing the deployment script you'll create later in this guide
- an assertion library used alongside Waffle for writing tests
- contains the type definitions for chai
- a testing framework for writing tests alongside Waffle
- contains the type definitions for mocha