Quickstart
A quickstart guide to writing assertions and using the Credible Layer CLI
Introduction
This guide will walk you through the process of creating, testing, and submitting an assertion using the pcl
(Phylax Credible Layer) CLI. By the end of this tutorial, you’ll understand how to:
- Set up your project structure
- Write a simple assertion
- Test your assertion
- Authenticate with
pcl
- Deploy your contract
- Create a project
- Store your assertion
- Submit your assertion to the Credible Layer
- Activate your assertion
- Verify that the assertion is working
Prerequisites
Before you begin, make sure you have:
pcl
CLI installed (see the Installation Guide)- Solidity compiler installed
- Foundry installed (see the Foundry Installation Guide)
- A browser wallet for authentication (MetaMask or similar)
- Access to the Credible Layer Demo, reach out to us on Telegram if you’re interested in getting access
0. Lazy Mode
We’ve created an example project with the content of this guide that you can use to follow along and use as a starting point for your own projects. The project can be found here.
The credible-layer-starter
repo has several examples that you can deploy and try out once you’re done with this guide.
Specfic instructions can be found in the README.
You can clone the example project by running the following command:
You can jump directly to the Running Tests section if you have the example project cloned and pcl
installed.
1. Project Setup
First, let’s set up a project with the correct directory structure:
Create the required directories:
Next, in order to make sure that forge works correctly, we need to the root folder of the project to be a git repository.
Installing forge-std
Next, we need to install forge-std as a project dependency:
This will add forge-std as a dependency to your project, which is required for running tests and managing dependencies.
Installing the credible-std Library
Next, you need to install the credible-std
library, which provides the base contracts and utilities for writing assertions:
After installation, create a remappings.txt
file at the root of your project with the following content:
These remappings will ensure that your imports work correctly when referencing the credible-std and forge-std libraries. The pcl
CLI will automatically detect and use these remappings when compiling your contracts.
Next, let’s create the smart contract that our assertion will monitor. This is a simple Ownable
contract that tracks ownership of a contract.
Create a file src/Ownable.sol
:
Currently, we rely on the contract having an owner()
function for ownership verification during project creation, so make sure to have a owner()
function in your contract.
2. Writing Your First Assertion
Assertions in the Credible Layer are Solidity contracts that inherit from the Assertion
base contract. They define checks that can be run against smart contracts to verify specific properties.
Let’s create a simple assertion that checks if an Ownable
contract’s ownership has changed after a transaction.
For a detailed breakdown of the assertion code, see the Assertion Guide.
Create a file assertions/src/OwnableAssertion.a.sol
:
Key Components of an Assertion:
- Inheritance: All assertions must inherit from the
Assertion
base contract. - Constructor: Initialize references to the contract you want to monitor. You can hardcode the address of the contract you want to monitor in the assertion contract, but you lose the flexibility of being able to use the same assertion for different smart contracts.
- Triggers Function: Register which assertion functions should be triggered.
- Assertion Functions: Implement the actual checks using pre and post transaction states.
3. Testing Your Assertion
To test your assertion, create a test file in the assertions/test
directory:
Create a file assertions/test/OwnableAssertion.t.sol
:
4. Running Tests
Use the pcl
CLI to run your tests:
This command will compile your assertion and run the tests. You should see output looking like this indicating that the tests have passed:
Troubleshooting Test Issues
If your tests fail, check for these common issues:
- Compilation errors: Ensure your Solidity syntax is correct
- Incorrect imports: Verify all import paths are correct
- State mismatch: Make sure your test properly sets up the initial state
- Assertion logic: Double-check the logic in your assertion function
5. Deploy Your Contract
You can deploy the Ownable contract using the following command:
Explanation of the arguments:
<RPC_URL>
: The RPC URL of the network you’re deploying to<PRIVATE_KEY_YOU_USED_TO_SIGN_IN_TO_DAPP>
: The private key of the account you used to sign in to the dApp<INITIAL_OWNER_ADDRESS>
: The address of the initial owner of the contract. Use the same address as you will be using in your browser wallet to authenticate with the Credible Layer.
Make sure to note down the address of the deployed contract as you’ll need it to create a project in the next step.
It will be the Deployed to:
address in the output of the command.
6. Authenticating with Credible Layer
Here’s a full video that you can consult to follow along with the process entire process from authentication to activating the assertion:
Before submitting your assertion, you need to authenticate:
Make sure to use the same address as you set as the initial owner of the contract in the previous step.
This will provide you with with a URL and an authentication code that you can use to authenticate with the Credible Layer.
If authentication fails, ensure:
- Your wallet has the correct network selected
- The
pcl
CLI uses the correct url - You have an internet connection
- The
pcl
CLI is properly installed
7. Create a Project
Once you have deployed your contract, you’ll need to create a project in the dApp if you don’t have one already. Navigate to the browser window opened by the pcl auth login
command and create a new project. When asked to link your contract, use the address of the contract you deployed in the previous step.
For a more detailed overview of how to use the dApp and manage projects, see the dApp Guide.
8. Storing Your Assertion
Next, store your assertion in the Assertion Data Availability layer (Assertion DA):
Here OwnableAssertion
is the name of the assertion and 0xADDRESS_OF_OWNABLE_CONTRACT
is the address of the contract you want to protect.
The 0xADDRESS_OF_OWNABLE_CONTRACT
is a constructor argument for the assertion, so if your assertion contract has a constructor argument, you need to provide it when storing the assertion.
This command submits your assertion’s bytecode and source code to be stored by the Assertion DA, making it available for verification by the network.
9. Submitting Your Assertion
Finally, submit your assertion to the Credible Layer dApp:
This will prompt you to select the project and assertion(s) you want to submit. Follow the interactive prompts to complete the submission.
Alternatively, you can specify the project and assertion directly as per the output of the pcl store
command:
Note, that <project_name>
is the name of the project you created in the dApp, capitalized in the same way as you did when creating the project.
10. Activating Your Assertion
Last step is to go to the dApp and activate the assertion. Go back to the url that you opened with the pcl auth login
command and navigate to the project that the assertions was added to.
You’ll notice that there’s one assertion ready for submission, go ahead and proceed to review and activate it.
For a more detailed overview of how to use the dApp, see the dApp Guide.
11. Verify That The Assertion Is Working
Now that your assertion is activated, let’s verify that it’s working as expected. We’ll do this by attempting to change the ownership of the contract, which should trigger our assertion and prevent the change.
First, let’s check the current owner of the Ownable contract. Replace ADDRESS_OF_OWNABLE_CONTRACT
with the address of your deployed Ownable contract and RPC_URL
with your network’s RPC URL:
This command should return the initial owner address that was set when we deployed the contract.
Next, let’s attempt to transfer ownership to a new address. Make sure you replace NEW_OWNER_ADDRESS
with an address that is not the initial owner and PRIVATE_KEY_OF_THE_OWNER
with the private key of the owner of the contract. This transaction should trigger the assertion and revert:
The transaction should timeout after about 20 seconds which means that the assertion reverted the transaction:
If you try to do another transaction with the same private key, you will most likely get this a replacement transaction error:
This is a known limitation of the system - when an assertion reverts a transaction, it gets dropped by the builder rather than being included in a block. This means that wallets and tools like cast
will still increment their local nonce, potentially causing issues with subsequent transactions. While this creates some UX friction, it only occurs when someone attempts to violate an assertion (i.e., attempt to hack a protocol), so we consider this an acceptable tradeoff. In the future, we plan to work with wallet providers to better surface these dropped transactions.
We recommend doing a simple ether transfer with a higher gas price, to replace the dropped transaction:
and then use the nonce to send a new transaction:
To confirm that the ownership hasn’t changed, let’s check the owner again:
The owner should still be the original address, confirming that our assertion successfully prevented the ownership change.
Conclusion
Congratulations! You’ve successfully created, tested, activated and verified your first assertion using the Credible Layer CLI. You can now go ahead and start implementing assertions in your own projects.
Next Steps
- Read the Assertions Book: Check out the Assertions Book for more detailed explanations and a collection of assertions for various use cases
- Try more complex assertions: We’ve created some more assertions in the credible-layer-starter repo that are ready to be deployed and used with a couple of commands
- Integrate with your own projects: Apply assertions to your existing smart contracts
- Join the community: Share your assertions and learn from others in the Phylax Telegram
For more detailed information about the Credible Layer CLI and its commands, see the CLI Reference Guide.
For a comprehensive list of terms and concepts used in the Credible Layer, see the Glossary.