Deploy a Smart Contract
A deploy-local.py
script is provided to deploy and configure the necessary contracts on a local stack, launched from make devnet-up
in a Boba/Optimism repository.
This is an example of how to execute it:
This script uses the Foundry toolkit to deploy the base contracts and a set of examples, via Forge scripts located in rundler-hc/crates/types/contracts/hc_scripts
.
The Forge scripts are written in Solidity, with toolkit extensions to support scripting, such as reading system environment variables and using them within Solidity functions. Code which executes within a block (like the following) is translated into a transaction which is first simulated locally and then executed on-chain:
The addresses of newly deployed contracts are then returned as an array of values:
Within deploy-local.py
the Forge scripts are invoked like this:
The Forge scripts may be run manually by setting the corresponding environment variables and then using the forge script
command as documented at https://book.getfoundry.sh/reference/forge/forge-script
We deploy with Foundry, but you may deploy with whatever tooling you wish.
Further system setup is performed through the web3.py
API, first by connecting to our L1 and L2 networks:
Next, we access the contracts using a loadContract()
function, such as for our HybridAccount
contract:
Here path_prefix
indicates where the contract's source code is located. The current deployer script will compile the Solidity code itself to determine the contract ABI rather than requiring a JSON file from a previous deployment.
After loading the contract, we can interact with it like this:
Here we perform a sequence of transactions to add individual example contract addresses to the list of contracts which are permitted to use our HybridAccount
. There is a check so that this operation is only performed for contracts which have not yet been registered.
Additional Examples
The preceding documentation was written to focus on the example showing addition and subtraction of two numbers. Additionally, the hybrid-compute/
folder contains more examples that can be used and experimented with. As Hybrid Compute in general is under active development, some of these examples may not be up to date with recent changes.
To integrate additional methods into our offchain-RPC server-loop
:
You can see how these methods are invoked from the TestHybrid.sol
contract. For example, "ramble" is used in a simple word-guessing game:
Note the common pattern of preparing an ABI-encoded request, invoking HA.CallOffchain(userKey, req)
, and then processing the response. This is the core of the Hybrid Compute framework, and the rest is up to you. We look forward to seeing your ideas and applications.
You've now reached the end of this tutorial! These examples aim to demonstrate the different functions and best practices you should keep in mind as you develop your own efficient, flexible smart contracts with Hybrid Compute.
Last updated