<p>Didn't really get what you are trying to do. Are you trying to create an oracle, or use already existing one.</p>
<p>First of all, Cardano block time is 20 seconds on average, so that is as close to 'real time' as you will get.</p>
<p>Now, on off-chain code you just query the oracle address on the blockchain, find the needed utxo (which represents some oracle value, or multiple of them) and check the datum, where oracle data is stored (like token price or some other data). Depending on oracle implementation, you will get the newest possible data.</p>
<p>On on-chain side (plutus script), in order to utilize oracle efficiently, you need to use reference inputs combined with inline datums. This way, your script can read utxo datum without spending the utxo itself, and utilize this data in the script itself.</p>
<p>To implement this in real world, you should track oracle price changes off-chain and trigger on-chain logic based on some off-chain changes or events. As on-chain code also has the access to this data, it will ensure its validity and
will execute the logic using data present in the oracle at exact execution moment (same block).</p>
<p>Regarding Oracle projects in the ecosystem, currently there is not much you can use, as vast majority of them are still in development. To my understanding all of them will use reference inputs and inline datums introduced by Vasil, as it's the only way to avoid congestion issues.</p>
<p>EDIT:</p>
<p>As for creating an oracle:</p>
<p>Of course, we are not talking about real world oracle implementation. As it needs validation, reputation, slashing and etc mechanisms, before even talking about appearing on Cardano blockchain. That's another topic you can research online out of Cardano scope.</p>
<p>Now, sort of centralized oracle implementation would be, to create a simple validator script (or even a wallet) which would hold different data points in its utxos datums. For example each utxo datum could represent some asset price, and the AssetClass it is representing. Then creating the utxo with cardano-cli you would want to use <code>--tx-out $oracleAddress+$utxoOutput --tx-out-inline-datum-file ${DATUMFILE}</code>, in order to store the datum on the blockchain itself, to be accessed easily by other scripts. You should also add some identity NFT in utxo output, which would ensure the validity of the utxo (meaning it was created by YOU, not some random guy providing random price).</p>
<p>You would also need a service, which constantly fetches the needed asset prices from some source, and submits the transaction which is creating oracle utxo outputs with datums at the oracle contract/wallet address, with each utxo also containing validity NFT/FT, in order for users of the oracle to validate if the utxo is legit.</p>
<p>The only drawback of this implementation, is that it's centralized, meaning the owner of wallet/contract can write any asset prices to it and you need to completely trust them.</p>
<p>Now, in any validator script, you can simply reference this utxo holding specific AssetClass price, by simply providing <code>--read-only-tx-in-reference utxo_hash#0</code> when building a transaction. PlutusV2 script will be able to retrieve the datum from the referenced utxo and use it in its logic.</p>
<p>Also, this oracle utxo can be used by multiple transactions at once without consuming it.</p>
<p>Hope this gives the rough idea</p>

CC BY-SA 4.0
2023-01-20T14:29:39.363Z
I want to create an oracle that can contain the current USD price in real time, I want to use it in another contract.
Do you have examples of codes that use 'reference inputs' and 'inline datums'?
professor moriarty
CC BY-SA 4.0
2023-01-20T16:19:01.230Z
edited the answer in regards to creating an oracle
serx
CC BY-SA 4.0
2023-01-20T18:16:03.820Z
Could I use this type of thing on Plutus?
professor moriarty
CC BY-SA 4.0
2023-01-20T19:04:30.780Z
what I've written is specific to plutus
serx
CC BY-SA 4.0
2023-01-20T13:32:20.177Z
2023-01-20T16:18:12.533Z
2023-01-20T16:18:12.533Z
serx
<p>Is there a way to follow in real time the price of an asset like the current price of USD or any other currency, without having to create multiple transactions to update the oracle?</p>

<oracles>
Track in real time the value of an Oracle
# Track in real time the value of an Oracle

(Asked by: username_0 on 2023-01-20)

Is there a way to follow in real time the price of an asset like the current price of USD or any other currency, without having to create multiple transactions to update the oracle?

**Tags:** oracles

---

## 1 answers

### Answer by username_1 on 2023-01-20. Score: 0 | Accepted answer

Didn't really get what you are trying to do. Are you trying to create an oracle, or use already existing one.

First of all, Cardano block time is 20 seconds on average, so that is as close to 'real time' as you will get.

Now, on off-chain code you just query the oracle address on the blockchain, find the needed utxo (which represents some oracle value, or multiple of them) and check the datum, where oracle data is stored (like token price or some other data). Depending on oracle implementation, you will get the newest possible data.

On on-chain side (plutus script), in order to utilize oracle efficiently, you need to use reference inputs combined with inline datums. This way, your script can read utxo datum without spending the utxo itself, and utilize this data in the script itself.

To implement this in real world, you should track oracle price changes off-chain and trigger on-chain logic based on some off-chain changes or events. As on-chain code also has the access to this data, it will ensure its validity and
will execute the logic using data present in the oracle at exact execution moment (same block).

Regarding Oracle projects in the ecosystem, currently there is not much you can use, as vast majority of them are still in development. To my understanding all of them will use reference inputs and inline datums introduced by Vasil, as it's the only way to avoid congestion issues.

EDIT:

As for creating an oracle:

Of course, we are not talking about real world oracle implementation. As it needs validation, reputation, slashing and etc mechanisms, before even talking about appearing on Cardano blockchain. That's another topic you can research online out of Cardano scope.

Now, sort of centralized oracle implementation would be, to create a simple validator script (or even a wallet) which would hold different data points in its utxos datums. For example each utxo datum could represent some asset price, and the AssetClass it is representing. Then creating the utxo with cardano-cli you would want to use `--tx-out $oracleAddress+$utxoOutput --tx-out-inline-datum-file ${DATUMFILE}`, in order to store the datum on the blockchain itself, to be accessed easily by other scripts. You should also add some identity NFT in utxo output, which would ensure the validity of the utxo (meaning it was created by YOU, not some random guy providing random price).

You would also need a service, which constantly fetches the needed asset prices from some source, and submits the transaction which is creating oracle utxo outputs with datums at the oracle contract/wallet address, with each utxo also containing validity NFT/FT, in order for users of the oracle to validate if the utxo is legit.

The only drawback of this implementation, is that it's centralized, meaning the owner of wallet/contract can write any asset prices to it and you need to completely trust them.

Now, in any validator script, you can simply reference this utxo holding specific AssetClass price, by simply providing `--read-only-tx-in-reference utxo_hash#0` when building a transaction. PlutusV2 script will be able to retrieve the datum from the referenced utxo and use it in its logic.

Also, this oracle utxo can be used by multiple transactions at once without consuming it.

Hope this gives the rough idea

> **Comments:**
> **username_0**: I want to create an oracle that can contain the current USD price in real time, I want to use it in another contract. Do you have examples of codes that use 'reference inputs' and 'inline datums'?  
> **username_1**: edited the answer in regards to creating an oracle  
> **username_0**: Could I use this type of thing on Plutus?  
> **username_1**: what I've written is specific to plutus  

---

