🫂Connect Airstack Phat Contract to EVM Consumer Contract
In the previous section we described how the Airstack Starter Kit works. Let's revisit the diagram and description below.
The diagram above displays the user journey of (1) Alice requesting a trust score for Eve from the EVM Consumer Contract. When (2) a new action request is added to the queue, (3) the Phala Network Phat Contract will pop the action off the queue and perform the off-chain work to compute a trust score.
First (4) the Phat Contract will create a batchHttpRequest
to query 3 separate subgraph endpoints to determine if Eve has transacted on-chain with Alice, has a Lens Profile, has a Farcaster account, has a primary ENS Domain, and if Alice & Eve have common POAPs. Once this data is returned then (5) the Phat Contract will compute a score based on some scoring criteria. You can view the code here. Lastly, (6) the trust score for Eve has been returned to the EVM Consumer Contract and Eve's score is set in the Consumer Contract's storage for anyone to query.
Getting Started
If you have not setup Airstack code repo locally, go back to the Quick Start section and follow the initial setup steps.
Before Deployment
Before deploying, you will need to either export your Phala Account via polkadot.js extension or store your private key in the .env (optionally you can manually enter during deployment as well).
Option 1: Export Polkadot account as json file
Go to your browser and click on the polkadot.js extension. Select your account and click "Export Account". Next, you will be prompted for your password before saving the file to your project directory. Note this is what will be set to POLKADOT_WALLET_ACCOUNT_PASSPHRASE
. Make sure to save the file as polkadot-account.json
in the root of your project directory.
Option 2: Set mnemonic phrase to POLKADOT_WALLET_SURI
After creating your Phala Profile, set your .env
variable POLKADOT_WALLET_SURI
to the mnemonic phrase from generating the new Polkadot Account.
Here is a screenshot of how to set POLKADOT_WALLET_SURI
:
Deployments (Local, Testnet, Mainnet)
This guide will be separated into 3 tabs including:
Local: Local Testnet Deployment
Testnet: PoC6 Testnet & EVM Chain Testnet Deployment
Mainnet: Phala Mainnet & EVM Chain Mainnet Deployment
Secrets (What are Secrets?):
apiUrl
- The endpoint base URL to the separate subgraph endpoints hosted on Airstack
apiKey
- an API key created on Airstack
In the previous Quick Start section, we installed the dependencies and ran 2 separate tests locally, but these tests were not run against a live local testnet.
This section will describe the process of:
Start up a local hardhat node
Deploy the EVM Consumer Contract to the local testnet
Run the
@phala/fn watch
command to run a local instance of the Airstack Phat ContractSimulate a sample request by executing
npm run localhost-push-request
See the Airstack Phat Contract reply with a result to the EVM Consumer Contract
Testing Locally
First step is to install the package dependencies with the following command:
With all the dependencies installed, we are ready to build Airstack Phat Contract.
To simulate the expected result locally, run the Phat Contract script now with this command:
Use
decode
andencode
playground at https://playground.ethers.org.
Here is the expected output of this call where the encoded call will request a trust score result for the address hashwarlock.eth
. The result is 16
.
Deploy a Local Hardhat Node
Now that we have a simple understanding of the expected functionality of the Phat Contract, we can now take our tests to a local testnet. Here we will use Hardhat to deploy the EVM Consumer Contract then listen from new action requests and reply with built Phat Contract script.
First we will start a local hardhat node.
With our hardhat node running locally, we can now deploy the OracleConsumerContract.sol
contract to the local hardhat network.
Make sure to copy the deployed contract address when you deploy your own contract locally. Note you contract address will be different than 0x5FbDB2315678afecb367f032d93F642f64180aa3
. We will now start watching the hardhat node deployed contract for any new requests from Airstack Phat Contract.
Let’s now make a new request and see what happens with the listener’s output. In separate tab, you will push a request with the following.
Note: The file can be edited here where you can change the
target
address.
If you check back the tab where the Phat Contract is listening for new requests, the console log may look similar to below:
🎉 Congratulations!
You've completed deploying and testing the Airstack Phat Contract successfully in a local testnet. Now let's move to deploying to an EVM Testnet and connecting a deployed Phat Contract on PoC6 Testnet to visualize how this works autonomously.
Last updated