OpenBook Quickstart

INFO

The goal of this section is to develop a smart contract to interact with the OpenBook deployment on the Eclipse Devnet

Prerequisites​

Install and configure Rust​

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Check if Rust is installed​

rustc --version
cargo --version

WARNING

Make sure you add the Rust toolchain to your PATH

Make sure you add the Rust toolchain to your PATH

Finally, lets go ahead and install the solana cli tools​

sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

Switching to the Eclipse Devnet​

Now set your solana cli to the Eclipse Devnet​

solana config set --url https://staging-rpc.dev.eclipsenetwork.xyz

Tokens​

solana airdrop 10

Now you should be good to go!​


Writing the smart contract:​

Interfacing with OpenBook​

  • We need our smart contract to connect with the OpenBook deployment on Eclipse

  • OpenBook essentially allows us keep records of the trades and transactions that take place on our network.

  • The Eclipse OpenBook deployment has its own API which is essential for us to interact with it.

  • We will mostly follow the standard deployment process for any Solana app

  • We will also need the programId for the OpenBook deployment on the Eclipse Devnet, which is as follows: xY9r3jzQQLYuxBycxfT31xytsPDrpacWnyswMskn16s

  • Lets begin by writing out our lib.rs file.

  • First we need to include the anchor crates and declare the programId

use anchor_lang::prelude::*;

declare_id!("<smart_contract_programId>");

...
  • Now lets go ahead and write the logic for our smart contract here.

  • Since everything is an account on Solana, we need to define our accounts here.

  • We can also add any required function and it's logic here

...

#[program]
pub mod my_smart_contract {
    use super::*;

    #[derive(Accounts)]
    pub struct MyAccounts<'info> {
    }

    #[access_control(allow)]
    pub fn trade(ctx: Context<MyAccounts>) -> ProgramResult {
        Ok(())
    }
}
...
  • Now lets go ahead and interact with the OpenBook deployment.

  • First lets connect to the Eclipse devnet by supplying the RPC url

  • Now we can use the programId to connect to the OpenBook deployment

  • After this we can write whatever API calls we need

...

#[tokio::main]
async fn main() {
    let ecl_rpc = "https://staging-rpc.dev.eclipsenetwork.xyz";
    let provider = Provider::new(ecl_rpc).await.unwrap();

    let dex_program_id = Pubkey::from_str("xY9r3jzQQLYuxBycxfT31xytsPDrpacWnyswMskn16s").unwrap();
    // First we need to perform authorization based on the token. This is the first account.
    struct Identity;

    impl Identity {
      fn prepare_pda<'info>(acc_info: &AccountInfo<'info>) -> AccountInfo<'info> {
        let mut acc_info = acc_info.clone();
        acc_info.is_signer = true;
        acc_info
      }
  }
    // Now to interact with the deployment
  impl MarketMiddleware for Identity {
    // Here is an example API call
        fn new_order_v3(&self, ctx: &mut Context, _ix: &mut NewOrderInstructionV3) -> ProgramResult {
        verify_and_strip_auth(ctx)
    }
    // Note that these calls are based on the public crate that is available for the deployment. This example assumes the availability of the default crates from a standard openbook deployment.
  }
}

...

Deploying the smart contract:​

  • To deploy the smart contract with anchor we can just do the following:

  anchor build

And then

  anchor deploy
  • Using this template you can write your own trade functions and deploy it on the Eclipse Devnet.

Last updated