Suppose I have a contract which depends on the current price of a real-world asset (like the price of gold). How would the contract obtain that price? How would I ensure that every time the contract is run (say, by BPs), the same result is obtained?
3 Answers
Contracts can't query off-chain data since that would make it impossible to reproduce the contracts' behavior. If you need to feed data into a contract, send it in an action. The contract can reject actions from unauthorized parties.
There needs to be an external service, usually called oracle that writes this data to blockchain.
Then contracts read this data off from the oracle contract.
Oracles can be third parties, compensated for writing this data. Or in the case of early ecosystem, you just run the oracle yourself on your server with a simple script that does
- Fetching the data, usually from URL
- Processing the result
- Writing the interesting parts of data to EOS storage
- Maintaining balance for the write operations
Because blockchain has to be deterministic, there is no way for smart contracts or block producers to rely on third party data sources.
This is decentralized application so generally you can't push/pull outside data to/from it. That's also the reason why Oracle service is there to responsible for it.
BTW, your question quite similar to this: How do you stream data from an input file when writing contracts on EOS?