Graph Api Oracle with Ink! Smart Contract (on Astar Network) and Ink! Phat Contract (on Phala Network)
Scenario described here in the communication between Ink! Smart Contract on Astar Network and Ink! Phat Contract on Phala Network:
- The Phat Contract
GraphApiOracle
(on Phala Network) queries the data from a graph api (subsquid, subquery or graph) and pushes the data into the Smart ContractGraphApiConsumer
(on Astar Network) - The Smart Contract
GraphApiConsumer
(on Astar Network) verifies the data and saves them to be displayed in the UI
You can find a demo here: TODO
The Phat Contract and Ink! Smart Contract have been built with the Phat Offchain Rollup. The full documentation of this SDK can be found here: https://github.com/Phala-Network/phat-offchain-rollup
The Phat Contract GraphApiOracle
is implemented by the generic Phat Contract JS Offchain Rollup
and a specific js script to in charge of :
- format the data in input (graph api endpoint + data coming from Ink! Smart Contract by example)
- query the graph indexer and do business logic
- format the data in output
We use the @phala/fn
CLI tool to generates the js code.
To deploy this Phat Contract you can build the contract or use existing artifacts
More information here: phat/contracts/js_offchain_rollup/README.md
To build the contract:
cd phat/contracts/js_offchain_rollup
cargo contract build
All artifacts are here: phat/artifacts
To deploy this Ink! Smart Contract you can build the contract or use existing artifacts
More information here: ink/contracts/graph_api_consumer/README.md
To build the contract:
cd ink/contracts/graph_api_consumer
cargo contract build
All artifacts are here: ink/artifacts
You have to configure the rpc, the pallet id, the call id and the contract id to call the Smart Contract GraphApiConsumer
.
For example:
RPC=https://shibuya.public.blastapi.io
PALLET_ID=70
CALL_ID=6
#public key of the contract aesULxtrttD4VGe1oDWGnDihbknjQ44GYwN1L8RXMcWZxis
CONTRACT_ID=0xd0a5af9b2cd1fa7ca7b8c37cb1323b6596c7810b661085dbc32bdcd3a498219c
Meta transaction allows the Phat Contract to submit rollup tx with attest key signature while using arbitrary account to pay the gas fee. To enable meta tx in the unit test you have to set the private key
For example, the private key of account //bob: 0x398f0c28f98885e046333d4a41c19cee4c37368a9832c6502f6cfd182e2aef89
If you don't use Meta-Tx, you have to be sure that address of Phat Contract GraphApiOracle
will be able to pay transaction fees on Astar Network.
You have to configure the http endpoint to reach to query the data.
You have to configure the js source code that will run inside the crate phat::js.
The graph api endoing configured above will be given in parameter in the main method of js code.
All data comming from the Ink! Smart Contract GraphApiConsumer
will be also given in parameters.
You have to grant the Phat Contract GraphApiOracle
as attestor in the Smart Contract GraphApiConsumer
.
If you use the Meta-Tx, you have to grant the ecdsa address.
If you don't use the Meta-Tx, you have to grant the sr25519 public key.
And grant the Phat Contract as attestor
In the Smart Contract GraphApiConsumer
, you have to register the hash of the source code used to query the data by the Phat Contract GraphApiOracle
.
Query the data from the graph api and push them into the Smart Contract GraphApiConsumer
.
In result you have the transaction id.