Skip to content

Latest commit

 

History

History
121 lines (92 loc) · 4.82 KB

EvmRollup.md

File metadata and controls

121 lines (92 loc) · 4.82 KB

Deploy a Phat-EVM Oracle with Offchain Rollup

This repo has implemented a Phat Contract serving as a data source of an EVM oracle. It can:

  • Fetch price data from CoinGecko.
  • Push-mode oracle: Create and config a price feed on the EVM side, and receive price quotes constantly streamed from the Phat Contract.
  • Pull-mode oracle: Send individual requests from the EVM side, and receive responses from the Phat Contract.

Architecture

(WIP)

  • Phat Contracts
  • Offchain Rollup clients
  • EVM contracts

Deploy

EVM contracts

The EVM contracts are at evm/ folder in this repo. It's developed with Hardhat. To start, you should initialize the repo first:

cd evm/
yarn

Compile the EVM contracts

npx hardhat compile

You can choose to the contracts on a local hardhat testnet:

# Run the commands below in two terminal windows
npx hardhat node
npx hardhat run --network localhost ./scripts/deploy-test.ts

Or alternatively, you can deploy it to a public EVM blockchain (e.g. Goerli or Astar) depending on the network you have configured. You may want to modify evm/hardhat.config.ts to add your network.

npx hardhat run --network <YOUR-NETWORK> ./scripts/deploy-test.ts

You will get an output like below. Please save it for later reference.

Deployed {
 anchor: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512',
 oracle: '0x5FbDB2315678afecb367f032d93F642f64180aa3'
}

Phat Contract

If you just want to run a unit test, now you can refer to the EvmPriceFeed unit test docs. Otherwise follow the instructions below if you would like to deploy a real Phat Contract on a live chain. Here let's assume the deployment target is the Phala PoC-5 live testnet.

PoC-5 Network parmeters:

  • Phat Contract UI: https://phat.phala.network
  • Substrate RPC: wss://poc5.phala.network/ws
  • PRuntime endpoint: https://poc5.phala.network/tee-api-1

You will need to deploy EvmPriceFeed contract on the testnet. Enter Phat UI. Get some test coin by Get Test-PHA if you don't have. Then you can click + Upload to deploy a contract. The precompiled contract can be found at:

./phat/artifacts/evm_price_feed/evm_price_feed.contract

If you want to build a fresh contract instead, you can follow the Phat Contract Development Guide.

After a successful deployment, the Phat UI should bring you to the contract page. You can find a more detailed guide in Sub0 2022 Workshop. Now you need to configure the contract by sending a config() transaction with the arguments below:

  • rpc: The EVM RPC for Phat Contract to send transaction. It must be a http endpoint.
  • anchor_addr: The anchor contract you deployed on EVM, with "0x".
  • submit_key: The secp256k1 private key you used to deploy the contracts on EVM, with "0x". (Note below)
  • token0: The first token in the trading pair. Must come from the CoinGecko simple price API. (e.g. bitcoin, or polkadot)
  • token1: The second currency in the trading pair. Must come from the the CoingEcko simple price API. (e.g. usd)
  • feed_id: By default, you can use 0 here because it's already configured by the deploy-test.ts script.

Notes on submit_key: The key is used for the EVM contracts to authenticate the data submissions are from the genuine EvmPriceFeed contract. The EVM rollup anchor ony accepts the transaction singed with the submit_key. So, in a secure setup, the submit key should be controlled and only controlled by a Phat Contract, without involving any human or 3rd party. In production deployment, the submit_key should be solely generated by a Phat Contract and never leaked to the outside. Once the key is generated, we can query its public key (or address) to set as the attestor in the EVM contracts.

However, in the test deployment, we can simply use the EVM deployer wallet as the submit key. DO NOT follow this setup on production.

Once configured, you can call the following query methods:

  • feed_price(): Fetch the latest price of your token0/token1 trading pair, and submit it to the EVM contracts. You will get FeedReceived message on EVM.
  • anser_price(): Read one request from the EVM side, and answer it with the price quote.
    • Opon a successful EVM deployment, there will be a polkadot/usd request in the queue.
    • Once a request is answered, you will receive a PriceReceived message. The request will be removed from the queue.
    • To add a new request, you can run npx hardhat run --network localhost ./scripts/testnet-push-request.ts
  • feed_custom_price(): A low level method that allows you to send any price data to a customized network and contract, not limited by the endpoint or contract address you have configured before.