On-Chain Oracle (Solana)

Our API can be consumed via a Switchboard oracle on-chain, on Solana. Both Solana NFT and Ethereum NFT prices can be consumed, which means you can build a cross-chain app on Solana that, for example, relies on Punks or BAYC prices from Ethereum.

Setting up the Oracle

With the current architecture, it's not possible to have a single Switchboard price feed report all the prices for all collections. Instead a new price feed needs to be configured per collection.

Luckily, configuring a new price feed on Switchboard takes less than 2 minutes, requires no coding, and costs peanuts.

Let us do so step by step Devnet example. We'll configure a price feed for SMBs:

  • Click + custom feed button in the top right hand corner

  • Give your feed a name, eg "SMB Smart Floor Price"

  • Click "add a job"

  • We now need to build a job definition. You can read more about Switchboard's publishers and jobs here, or simply follow the below steps:

  1. We need to find out the "collection ID" so we hit the https://api.tensor.so/sol/collections endpoint. The ID for SMBs is 9uBX3ASjxWvNBAD1xjbVaKA74mWGZys3RGSF7DdeDD3F

  2. We now can construct a link for floor prices. Since we only care for the latest price, we can ignore the at= parameter. The url becomes: https://api.tensor.so/sol/collections/9uBX3ASjxWvNBAD1xjbVaKA74mWGZys3RGSF7DdeDD3F/floor

  3. With the url in hand we can finally construct the job:

{
  "tasks": [
    {
      "httpTask": {
        "url": "http://api.tensor.so/sol/collections/9uBX3ASjxWvNBAD1xjbVaKA74mWGZys3RGSF7DdeDD3F/floor"
      }
    },
    {
      "jsonParseTask": {
        "path": "$.price"
      }
    }
  ]
}
  • Copy paste the above job into the window on Switchboard and hit test. Give it a few sec and hopefully a number appears next to test button. This means our job is working correctly!

  • Title jour job and hit "add job"

  • Hit "add feed"

  • On the right hand side, hit "configure feeds", then "checkout" at the bottom

  • Make sure you have some SOL in your devnet wallet

  • Configure the interval (how often you want the price updated) and length (how long will the price feed be live for), then complete the rest of the checkout process by signing relevant transactions

  • Once completed, you can click "view feeds" - and there it is! Your very own smart floor price oracle for SMBs on Solana!

Testing the Oracle

Now let's use it in an example app.

  • Go to our example sol oracle repo and git clone it

  • Let's do an on-chain, rust app first:

    • Collect the "aggregator public key" from your switchboard UI, mine looks like this: 8VD9EJ4njJfH1o2X2a8HLrx93Qa5noNGYt3qhyzcVZ1S

    • Go into rust/tests/lendoor.ts and update the aggregatoraccount marked with a TODO to your own

    • Run anchor test --skip-deploy

    • Follow the link printed to the console and read the logs! You should see something like this:

> Program log: Instruction: GimmeLoan
> Program log: GM and Welcome to LENDOOR Inc.
> Program log: Hm, looks like you want a loan against your SMB!
> Program log: Let's see what the big brains at Tensor are saying re SMB prices...!
> Program log: They claim the SMB Smart Floor is at 120.78269948242 SOL.
> Program log: Smart floor is a robust version of normal floor prices, designed specifically for financial use-cases like NFT lending and NFT derivatives. So we can trust it.
> Program log: I'd be prepared to give you a loan at 75% LTV, making it 90.587024611815 SOL!
> Program log: Whacha say, ser?
> Program HX7h7LUQ61ztLgXg4Hh4fqnvuWSuf1vVoh7SMdtarpiU  consumed 8974 of 200000 compute units
  • Now let's do an off-chain, js app:

    • Go to js/play.ts

    • Update the aggregator pubkey similarly where it's marked with a TODO

    • run yarn && yarn play

    • you should see a single number printed to the console, which is the SMB smart floor price reported by Tensor

And just like that, you can consume smart floor prices on-chain via on oracle!

Happy Solana hacking! 🛠

Last updated