anyone know how oraclize api is working. I read the source code files on github.com/oraclize/ethereum_api . it need a "__callback" function in my contract to receive the result, is it means there has a off-chain application on watching events, and provide the result?
Oraclize uses a backend program to listen for the queries and provides results from different types of data sources, including IPFS, random.org, and others.
They are able to provide proof that the data returned has not been manipulated. In the case of random.org, they provide the signature of the results, the final user can then recover the public key of random.org using that signature.
The pricing is a few cents per query and the subject should assume the gas cost of the transaction. Unfortunately, they do not have a way to determine the gas beforehand and the user should provide sufficient amount of ether to cover the gas cost. If not, the query won't be executed. Furthermore, they do not return the unused gas, which is pretty bad if your application requires continuous use of the service.
The results are passed to a function that should match the expected name, this is because the interface to the contract should be known beforehand, so oraclize can call it. The user can then put the code that it wants to be executed inside the callBack function.
I hope this helps,
Someone commented that is false that oraclize doesn't return the unspent gas and claim that it will remove the downvote if I remove that claim.
Thing is that Oraclize state that they do not return the unspent gas in their documentation:
Smart contract developers should estimate correctly and minimize the cost of their __callback method, as any unspent gas will be returned to Oraclize and no refund is available.
The commenter is wrong. However, it adds a piece of information that is valuable as he/she indicates that there are other payment methods that oraclize accepts, like off-chain payments in fiat in which the gas charged is what is spent plus the fee.
It's a bit tough to explain completely.
But there's an oraclize contract on the main net and testnets (Kovan, Rinkeby, Ropsten..) which you can import and use it's functions.
Then after your queries are done, a 3rd party (Oraclize.it) provides you the results. They also give result data encryption possibilities and some kind of proofs that allow them to demonstrate to you that the info you asked for is real and untouched/Unread.
__callback function allows them to send you back the data and then, you can override the function in order to do something with it.
On it's API there's plenty of examples that will allow you to understand how it works.
Hope it helps.
The Oraclize architecture on Ethereum is separated into a few parts. The core parts are made up of the on-chain smart contracts, a set of contracts deployed on the ethereum network and one you need to inherit into your own to properly connect with them, and a proprietary Oraclize engine which handles all the off-chain operations. In the case of proofs being requested, you can add the proof provider to the architecture, which in the case of TLSNotary is a locked-down AWS instance acting as the auditor, Android Proof is an Android smartphone with TEE, or in case of Random is a Ledger Nano S running a custom app.
Regarding the on-chain portion, the contract you inherit is called the oraclizeAPI. The two your contract will be reaching out to is the OraclizeAddressResolver (OAR) which serves the function of pointing to the OraclizeConnector contract, where the on-chain logic for the service lives. These mechanism allows the OraclizeConnector to be extensible and upgraded, although an important tenet for Oraclize is to ensure backwards compatibility, and we've had a few updates that have been seamless so far. The aforementioned contracts may be explored here: https://github.com/oraclize/ethereum-api/tree/master/connectors and are verified on etherscan.
The engine watches the aforementioned deployed contracts, for specific logs, and when it sees them, it executes them as requested, and then sends the results on-chain alongside some authenticity proof if requested. This engine is general purpose and not ethereum-specific, it has support for other blockchains and mechanisms of query/answer/proofs transmission.