Use solidity contract package
Overview
This documentation will guide you through the process of setting up and using Chromatic contracts in Solidity code, either by importing the Chromatic contracts or extending them in your own contracts.
Prerequisites
Before you can start using the Chromatic-protocol contracts package, make sure you have the following prerequisites installed on your system:
Step 1: Initialze a Yarn Project
Open your terminal and navigate to your project directory. Run the following command to initialize a new Yarn project:
$ yarn init -y
This command creates a new package.json file in your project directory.
Step 2: Add Hardhat
Hardhat is a development enviroment for Ethereum software that includes Solidity compilation, testing, and deployment. Run the following command to add Hardhat as a development dependency to your project:
$ yarn add -D hardhat
Step 3: initialize hardhat
After installing Hardhat, you need to initialize it in your project. Run the following command to initialize Hardhat and create the initial project structure:
$ yarn hardhat init
This command sets up the necessary files and directories required for your Hardhat project.
When initializing Hardhat using the yarn hardhat init
command, you may be presented with three options.
Create a JavaScript project
❯ Create a TypeScript project
Create an empty hardhat.config.js
We will proceed with the Create a TypeScript project
option with this guide. Use the arrow keys to navigate to Create a TypeScript project
and press Enter to select it.
Hardhat will now set up your project with TypeScript support. It will generate the necessary configuration files and update the project structure accordingly.
This command will create the following files and directories:
hardhat.config.ts
: This file is the main configuration file for your Hardhat project. It contains settings and options for your project's compilation, network configurations, and deployment scripts.contracts/
: This directory is where you will store your Solidity smart contracts. You can create new contracts or modify existing ones in this directory.scripts/
: This directory is where you will store your Solidity smart contracts. You can create new contracts or modify existing ones in this directory.test/
: This directory is where you can write your tests using Hardhat's testing framework. Tests allow you to verify the correctness of your contracts and ensure they behave as expected.
Step 4: Add Chromatic Contracts Package
To integrate the Chromatic-protocol contracts package into your project, you need to add it as a dependency. Run the following command to add the Chromatic-protocol contracts package:
$ yarn add -D @chromatic-protocol/contracts
This command installs the Chromatic-protocol Solidity contract package and its dependencies into your project.
Step 5: Set Compiler Version
Before you can compile the Chromatic-protocol contracts, you need to specify the Solidity compiler version in your Hardhat configuration file. Open the hardhat.config.ts
file in your project root directory and update the solc section as follows:
module.exports = {
// ...
solidity: {
version: "0.8.20",
settings: {
// ...
},
},
// ...
};
Step 6: Create a new smart contract
You can now create a new smart contract that extends the contracts provided by the Chromatic-protocol Solidity contract package. Here's an example of how your contract may look:
contracts/SampleMarketFactory.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import {ChromaticMarketFactory} from "@chromatic-protocol/contracts/core/ChromaticMarketFactory.sol";
// Zero interest market factory
contract SampleMarketFactory is ChromaticMarketFactory {
function currentInterestRate(
address token
) external view override returns (uint256 annualRateBPS) {
return 0;
}
function calculateInterest(
address token,
uint256 amount,
uint256 from, // timestamp (inclusive)
uint256 to // timestamp (exclusive)
) external view override returns (uint256) {
return 0;
}
}
Step 7: Compile
Now that you have set up your project and created the contract, you can compile it using Hardhat. Open your terminal and run the following command:
$ yarn hardhat compile
This command compiles your contracts and generates the corresponding artifacts in the artifacts directory.