Client contract tutorial
The Client Contract allows developers to create storage deals programmatically via smart contracts.
In this tutorial, we will cover the background of creating storage deals via smart contracts and how to create storage deals with smart contracts on the FEVM (Ethereum Virtual Machine on top of the Filecoin blockchain). Before continuing, you will need the following software preinstalled on your computer:
- Yarn or NPM (Yarn is used in this tutorial)
- A code editor such as VS Code
- A wallet with Calibration testnet FIL
Before we get started, we recommend reading about programmatic storage with Filecoin here. There are two methods to programmtic storage, which are direct dealmaking and aggregated dealmaking. We will cover direct dealmaking with the client contract tutorial.
Let’s now run through how to create storage deals via smart contracts.
First, let’s grab the kit and set up the development environment.
- 1.Clone the Filecoin virtual machine deal-making kit, including all submodules:git clone --recurse-submodules https://github.com/filecoin-project/fvm-starter-kit-deal-making.gitThis will copy the fvm deal-making kit into your current directory and initiate the
- 2.Moving into the
fvm-starter-kit-deal-makingdirectory and grab all the dependencies using
- 3.Now that all the packages are downloaded, we will need to create a
.envfile with your private key. This is so the hardhat kit knows what wallet to use for transactions. Open up the repo in your code editor of choice and find the file titled
.env.example. Rename the file to
.env:mv .env.example .env
- 4.Replace the example private key with your actual private key. If you are using Metamask, follow this tutorial to get your private key.Remember to take precautions to never share your private key with anyone or check it into Git! The
.gitignoreof the hardhat kit is already set to ignore
- 5.Deploy the contracts with
hardhat:yarn hardhat deployThis should compile and deploy all the contracts, including the client contract, which is the one we will be interacting with. Copy and take note of the address of the deployed contract for later.
Before storing a file with a storage provider, it needs to be prepared by turning it into a
.carfile, and the metadata must be recorded. To do this the Hardhat kit has a tool which can do this for you. However, to keep things nice and simple, we’re going to use the FVM Data Depot website. This website will automatically convert files to the
.carformat, output all the necessary metadata, and act as an HTTP retrieval point for the storage providers.
- 2.Click Upload File and select a file you wish to upload.
- 3.Select the File ID of the file to read all the relevant metadata. Make a note of the:
We’ll use this information in the next step when invoking the
- Piece CID
- Payload CID
- Car size
- Piece size
Now that we have the
.carfile prepared in the data depot, we can invoke the MakeDealProposal method on the smart contract we deployed earlier. To do this, we will run the
make-deal-proposaltask in Hardhat. There are quite a few parameters to include in this call:
contract: the address of your deployed
- `piece-cid: gathered from the previous step.
piece-size: gathered from the previous step.
car-size: gathered from the previous step.
location-ref: The location of where the storage provider can find the .car file. This is the
URLfrom the previous step.
When calling the
make-deal-proposaltask in Hardhat, your command will look something like this:
yarn hardhat make-deal-proposal \
--contract 0x0219eB1740C315fe5e20612D7E13AE2A883dB3f4 \
--piece-cid baga6ea4seaqn4eomxfk3ttog7lnvlvedu7nia377w4gotw2pm746k6kq7gwe6ga \
--piece-size 2097152 \
--verified-deal false \
--car-size 1439368 \
--label baga6ea4seaqn4eomxfk3ttog7lnvlvedu7nia377w4gotw2pm746k6kq7gwe6ga \
--start-epoch 180000 \
--end-epoch 700000 \
--storage-price-per-epoch 0 \
--provider-collateral 0 \
--client-collateral 0 \
--extra-params-version 1 \
--location-ref "https://bafybeidguwwno5ohjss7g4l6ygvyy3dzxxrkkgtxqkobnnxnu62aw4ipxa.ipfs.w3s.link/ipfs/bafybeidguwwno5ohjss7g4l6ygvyy3dzxxrkkgtxqkobnnxnu62aw4ipxa/baga6ea4seaqn4eomxfk3ttog7lnvlvedu7nia377w4gotw2pm746k6kq7gwe6ga.car \
--skip-ipni-announce true \
--remove-unsealed-copy true \
Parameters such as the
price-per-epochare set to
0. On mainnet, these would be determined by storage providers, but since this is on the Calibration testnet, the storage providers should pick up the jobs even with these parameters set to
Now if you’ve invoked the task with all the correct parameters, the method will execute on-chain and emit an event that Boost storage providers will be listening to. If the deal is well-formed and the parameters are acceptable, they will download the .car file, double-check to ensure the
piece-sizematch the deal and publish your storage deal! This could take up to a day. Once the deal is published, you can find it on a Calibration testnet block explorer. The client in the deal should be the
t4address of the smart contract we called
After emitting an event and waiting for storage providers to accept your deal, you can monitor its status on a provided Boost logs dashboard. This feature is only available on the Calibration testnet. See this guide on GitHub for help diagnosing why deals might not be accepted and adjusting your proposal for re-submission.
During this tutorial, we have shown the significance of making deals using smart contracts and then walked through making a storage deal using the FVM deal-making kit and web3.storage. Developers can make use of this workflow to integrate decentralized storage on Filecoin with their smart contracts and decentralized applications.