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?
1 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
-
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'? Commented Jan 20, 2023 at 14:29
-
-
-