Skip to main content
Compiling contracts that use ConnectOracle.sol requires the viaIR feature. For Foundry/Forge, this can be done by using the --via-ir flag. The relevant methods for other tools may vary.

Foundry

For this tutorial, you will be using the Foundry toolkit to develop, compile, and deploy your contracts. If you do not have Foundry installed, follow the Foundry installation instructions.

Setup

Create a new project directory and initialize it with forge init:

Implementing the Contract

Before writing the contract, first rename the template contract to Oracle.sol.
Then update the contract from the template to be your oracle contract. Declare the IConnectOracle interface, which will be used to interact with the ConnectOracle contract.
src/Oracle.sol
Next, define the constructor for the contract. This will be used to initialize the contract with the ConnectOracle contract address.
The ConnectOracle contract is on MiniEVM precompiles. You can get its address by querying ${REST_URL}/minievm/evm/v1/connect_oracle where ${REST_URL} refers to the REST endpoint URL of the rollup.
The output will look like this:
src/Oracle.sol
Once the constructor is implemented, move on to defining the different functions that the contract will have.
  • oracle_get_price: This function will return the price of a single asset pair
  • oracle_get_prices: This function will return the price of multiple asset pairs
src/Oracle.sol
The complete contract will then look like this:
src/Oracle.sol
Running forge compile will fail unless you provide a test file that matches the Oracle.sol contract. Foundry expects a test file like Oracle.t.sol to exist and import the contract under test. To resolve this, rename the existing test file Counter.t.sol to Oracle.t.sol.
Also replace the file contents with placeholder content.
test/Oracle.t.sol
Now running forge compile should work without any errors.

Deploying the Contract

Now that the contract is compiled and ready, you can deploy it to the MiniEVM. To accomplish this, use Foundry’s forge script command. First, create a script file to handle the deployment. Create a new file named Oracle.s.sol in the script directory.
script/Oracle.s.sol
Set your environment variables and run the deployment. Be sure to replace PRIVATE_KEY with the deployer’s private key, and JSON_RPC_URL with your rollup’s JSON-RPC endpoint.
Output should look like this:
To query the oracle_get_price() function, use Foundry’s cast call command.
Output should look like this:
The output is an ABI-encoded hexadecimal result containing the price and timestamp. The first 32 bytes represent the price, and the next 32 bytes represent the timestamp.