Create and deploy a protocol that integrates with Cannon packages
Start by installing/upgrading the Cannon command-line interface:
npm i -g @usecannon/cli
Select your framework
Create a new Foundry project with forge init sample-project
. This will generate the following contract:
Create a cannonfile.toml in the root directory of the project with the following contents. If you plan to publish this package, you should customize the name. This will deploy the contract and set the number to 420:
setConfiguration
(instead of/in addition toaddConfiguration
or removeConfiguration
) such that changes to an invoke
operation will result in predictable behavior.Now build the cannonfile for local development and testing:
cannon build
This compiled your code and created a local deployment of your nascent protocol. You can now run this package locally using the command-line tool. (Here, we add the --registry-priority local
option to ensure we’re using the version of this package that you just built, regardless of what others have published.)
cannon sample-foundry-project --registry-priority local
Deploying is just building on a remote network!
cannon build --network REPLACE_WITH_RPC_ENDPOINT --private-key REPLACE_WITH_KEY_THAT_HAS_GAS_TOKENS
Verify your project's contracts on Etherscan:
cannon verify sample-foundry-project --api-key REPLACE_WITH_ETHERSCAN_API_KEY --chain-id REPLACE_WITH_CHAIN_ID
Finally, publish your package on the Cannon registry:
cannon publish sample-foundry-project --private-key REPLACE_WITH_KEY_THAT_HAS_ETH_ON_MAINNET
You can use packages in your Cannonfiles with the pull and clone operations.
pull
 packages to reference the addresses in their deployment data. Find which networks each package has deployment data for on the registry explorer.
For example, the Synthetix Sandbox contains a Cannonfile that deploys the sample integration contract connected to the official deployment addresses. The relevant code looks like this:
clone
packages to deploy new instances of their protocol's contracts.
For example, the Synthetix Sandbox contains a Cannonfile that clones a new instance of Synthetix and sets up a custom development environment. This is a simplified version of the relevant code:
Install Cannon for Foundry:
forge install usecannon/cannon-std
Grant your Foundry project permission to read from the filesystem. Add the following line to your foundry.toml
 file:
fs_permissions = [{ access = "read", path = "./"}]
Include the Cannon.sol library in your tests. Here's an example:
Use the test command to run them. (Note that the --chain-id
option can be used to run tests against a forked network.)
npx cannon test
Create a new Hardhat project by following the instructions here. Then install the Hardhat Cannon plug-in:
npm install hardhat-cannon
Load the plug-in at the top of your hardhat.config.js file with require('hardhat-cannon');
 or import 'hardhat-cannon';
 if you're using Typescript.
In the configuration file, set the default network like so: defaultNetwork: "cannon"
Your project should have the following contract:
Create a cannonfile.toml in the root directory of the project with the following contents. If you plan to publish this package, you should customize the name. This will deploy the contract and set the unlock time to 1700000000:
Now build the cannonfile for local development and testing:
npx hardhat cannon:build
This compiled your code and created a local deployment of your nascent protocol. You can now run this package locally using the command-line tool. (Here, we add the --registry-priority local
option to ensure we’re using the version of this package that you just built, regardless of what others have published.)
cannon sample-hardhat-project --registry-priority local
Deploying is just building on a remote network! Be sure to use a network name that you've specified in your Hardhat Configuration file.
npx hardhat cannon:build --network REPLACE_WITH_NETWORK_NAME
Set up the and verify your project's contracts::
npx hardhat cannon:verify
Finally, publish your package on the Cannon registry:
npx hardhat cannon:publish --private-key REPLACE_WITH_KEY_THAT_HAS_ETH_ON_MAINNET
You can use packages in your Cannonfiles with the pull and clone operations.
pull
 packages to reference the addresses in their deployment data. Find which networks each package has deployment data for on the registry explorer.
For example, the Synthetix Sandbox contains a Cannonfile that deploys the sample integration contract connected to the official deployment addresses. The relevant code looks like this:
clone
packages to deploy new instances of their protocol's contracts.
For example, the Synthetix Sandbox contains a Cannonfile that clones a new instance of Synthetix and sets up a custom development environment. This is a simplified version of the relevant code:
You can use the build task in your tests and optionally use the built-in TypeChain support. Here's an example from the Hardhat sample project: