Waffle
Overview
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.
Prerequisites
This tutorial requires Node.js to be installed. You can download it through Node.js or run the following code to complete the installation. You can verify the correct installation by requesting the version of each installation package:
Step 1: Use Waffle to create a TypeScript project
Here we use the truffle project (please refer to the first 3 steps in the truffle document to create a project module using Truffle), then configure the package.json file, and add dependencies in devDependencies:
Waffle - for writing, compiling, and testing smart contracts
Ethers - for interacting with Fractal's Ethereum API
OpenZeppelin Contracts - the contract you'll be creating will use OpenZeppelin's ERC20 base implementation
TypeScript - the project will be a TypeScript project
TS Node - for executing the deployment script you'll create later in this guide
Chai - an assertion library used alongside Waffle for writing tests
@types/chai - contains the type definitions for chai
Mocha - a testing framework for writing tests alongside Waffle
@types/mocha - contains the type definitions for mocha
Need to reinstall dependencies
Step 2: TypeScript configuration file
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.
Step 3: Compile with Waffle
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 is0.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:
Step 4: Use Mars to configure the deployment script
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.
Step 5: Create deploy script
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.
Step 6: Use Mars for deploy
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!
Last updated