Skip to main content
Tutorial GitHub Repository
Compiling contracts that uses 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, we will be using Foundry toolkit to develop, compile, and deploy our contracts. If you do not have Foundry installed, follow the Foundry installation instructions.

Setup

First, we need to create a new directory for our project.
Next, we will initialize a new Foundry project side that directory.
Once the project is initialized, we can proceed to installing the required dependencies needed for this tutorial. In this case, we only need Initia’s EVM contracts.

Implementing the Contract

Before writing our contract, we first need to rename the template contract to NewInitiaERC20.sol
We then update the contract from the template to be our oracle contract. Start by importing the IConnectOracle interface from the @initia/initia-evm-contracts package.
src/Oracle.sol
Next, we declare the variables that we will use in the contract’s operations.
  • connect: The interface of the ConnectOracle contract
  • currencyPair: The variable that stores the currency pair response from ConnectOracle
  • price The variable that stores the single pair price response from ConnectOracle
  • prices The variable that stores the multiple pair prices response from ConnectOracle
src/Oracle.sol
We then need to define the constructor for our contract. This will be used to initialize the contract with the ConnectOracle contract address.
src/Oracle.sol
Once the constructor is implement, we move on to defining the different functions that our contract will have
  • oracle_get_all_currency_pairs: This function will return all of the asset pairs currently supported by Connect
  • 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
Our complete contract will then look like this:
src/Oracle.sol
Our contract implementation is now ready. However, if we try to compile the contract using forge compile, we will get an error. This is because the default Oracle.t.sol expects the original Oracle.sol contract to be available. To fix this, we will rename Oracle.t.sol to OracleTest.t.sol.
We will also replace the file contents with placeholder content.
test/OracleTest.t.sol
Now running forge compile should work without any errors.

Deploying the Contract

Now that Our contract is compiled and ready, we can deploy it to the MiniEVM. To accomplish this, we will use Foundry’s forge create command
To confirm that the contract was deployed successfully, we can try calling the oracle_get_all_currency_pairs function using Foundry’s cast send command.
If you then try querying the currencyPairs variable using Foundry’s cast call command, you should see the response from Connect.