Skip to content

Commit

Permalink
Merge pull request #30 from GuiGou12358/main
Browse files Browse the repository at this point in the history
  • Loading branch information
h4x3rotab committed Sep 30, 2023
2 parents 6d76832 + c244a2e commit 001ecf0
Show file tree
Hide file tree
Showing 36 changed files with 9,164 additions and 673 deletions.
112 changes: 112 additions & 0 deletions InkRollup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Deploy a Phat-ink! Oracle with Offchain Rollup

This repo has implemented a Phat Contract serving as a data source of an Ink! smart-contract oracle. It can:

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

## Architecture

(WIP)

- Phat Contracts
- Offchain Rollup clients
- Ink! smart-contracts

## Deploy

### Ink! contracts

The precompiled contract can be found at:
```
./ink/artifacts/test_oracle/test_oracle.contract
```

> If you want to build a fresh contract instead, you can compile it.
The source can be found at `ink/contracts/test_oracle` folder in this repo.
```bash
cd ink/contracts/test_oracle
```

Compile the Ink! smart contract
```bash
cargo contract build
```

You can choose to deploy the contract on a local node.
In this case you can install :
- [Swanky node](https://github.com/AstarNetwork/swanky-node). The easiest method of installation is by downloading and executing a precompiled binary from the [Release Page](https://github.com/AstarNetwork/swanky-node/releases)
- [Substrate node](https://github.com/paritytech/substrate-contracts-node.git) with pallet-contracts.

Or alternatively, you can deploy it to a public blockchain (e.g. Shibuya/Shiden/Astar) depending on
the network you have configured.


### Phat Contract

If you just want to run a unit test, now you can refer to the [InkPriceFeed unit test docs](./phat/contracts/ink_price_feed/README.md).
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 `InkPriceFeed` contract on the testnet. Enter [Phat UI](https://phat.phala.network).
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/ink_price_feed/ink_price_feed.contract
```

> If you want to build a fresh contract instead:
The phat contract is at `phat/contracts/ink_price_feed` folder in this repo.
```bash
cd phat/contracts/ink_price_feed
```

Compile the Phat contract
```bash
cargo contract build
```


After a successful deployment, the Phat UI should bring you to the contract page. Now you need to configure
the contract by sending a `config()` transaction with the arguments below:

- `rpc`: The Substrate RPC for Phat Contract to send transaction. It must be a http endpoint.
- `pallet_id`: The pallet id for Phat Contract to send transaction. 70 for Shibuya, 7 for swanky node.
- `call_id`: The call id for Phat Contract to send transaction. 6 in many cases.
- `contract id`: The anchor Ink! contract you deployed on substrate node, with "0x".
- `sender_key`: The sr25519 private key you used to pay the transaction fees, with "0x".

>Next you will have to authorise the phat contract to send the messages to ink! smart contract
Call the method `get_attest_address` and `get_ecdsa_public_key` to get the public keys used by the phat contract.

In the Ink! smart contract side, use the Contracts-UI or Polkadot.js to grant the phat contract as attestor
- Use the method `registerAttestor` to set the attest_address and the ecdsa_public_key
- Use the method `accessControl::grantRole` to set only the attest_address
- Use the method `metaTransaction::registerEcdsaPublicKey` to set only the ecdsa_public_key

Once configured, you can call the following query methods in ink! smart contract:
- `createTradingPair`: Create a trading to get the price between two tokens
- `requestPrice`: Send a message to receive the latest price from the Phat Contract
- `getTradingPair`: Display the trading pair with the latest price received

You can call the following query methods in phat contract:

- `feed_price_from_coingecko()`: Fetch the latest price of your token0/token1 trading pair, and submit it to the
Ink! smart contract contracts. You will get `FeedReceived` message on Ink smart contract side.
- `anser_price()`: Read one request from the Ink smart contract side, and answer it with the price quote.
You will get `FeedReceived` message on Ink smart contract side.
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The `features` attribute allows you to control the rollup target chain activatio

- `evm`: enables the client to connect to the EVM rollup anchor contracts.
- `substrate`: allows the client to connect to the Substrate rollup anchor pallet.
- `ink`: (currently a work in progress).
- `ink`: enables the client to connect to the Ink! rollup anchor contracts.

Additionally, the `logging` feature can be utilized to display internal logs, including internet access, to the logger. This can be helpful for debugging purposes.

Expand Down Expand Up @@ -178,12 +178,20 @@ Note that the error handling in the sample code above is simplified. In real-wor

Finally, the consumer contract can be configured to receive responses as shown below.

Solidity side:
```solidity
function _onMessageReceived(bytes calldata message) internal override {
emit MsgReceived(message);
}
```

Ink! side
```rust
fn _on_message_received(&mut self, action: Vec<u8>) -> Result<(), RollupAnchorError> {
// do you business logic
}
```

## Integration

To build an end-to-end project with offchain rollup, follow these steps to deploy the **Offchain Rollup Anchor** contract or pallet to the target blockchain and integrate it with the **Consumer Contract**. The rollup anchor is provided in this repository, while the consumer contract refers to the dApp that communicates with the Phat Contract.
Expand All @@ -204,22 +212,38 @@ To deploy the EVM rollup anchor, follow these steps:

Find a reference script [here](./evm/scripts/deploy-test.ts).

The Substrate pallet and ink! anchor deployment docs are currently under development (TODO).
To deploy the Ink rollup anchor, follow these steps:

1. Deploy the Phat Contract
- Sample code: [InkPriceFeed](./phat/contracts/ink_price_feed/lib.rs)
- During the instantiation, a sr2519 key pair is generated (called attestor key). This key is used to sign the messages sent to Ink! smart contract
2. Deploy the Ink! smart contract:
- Sample code: [TestOracle](./ink/contracts/test_oracle/lib.rs)
- register the phat contract as `attestor`
3. Configure the Phat Contract by sending a `config()` transaction with the arguments below:
- `rpc`: The Substrate RPC for Phat Contract to send transaction. It must be a http endpoint.
- `pallet_id`: The pallet id for Phat Contract to send transaction.
- `call_id`: The call id for Phat Contract to send transaction. 6 in many cases.
- `contract id`: The anchor Ink! contract you deployed on substrate node, with "0x".
- `sender_key`: The sr25519 private key you used to pay the transaction fees, with "0x".
Find a more detailed documentation [here](./InkRollup.md).

The Substrate pallet anchor deployment docs are currently under development (TODO).

### Integrate with Your Contract

Detailed instructions for consumer contract integration are coming soon (TODO). In the meantime, please refer to provided examples:

- For EVM: Sample consumer contract [TestOracle](./evm/contracts/TestOracle.sol)
- For ink! (WIP)
- For Ink!: Sample consumer contract [TestOracle](./ink/contracts/test_oracle/lib.rs)
- For Substrate: Sample consumer pallet [phat-oracle-pallet](https://github.com/Phala-Network/phala-blockchain/blob/master/pallets/offchain-rollup/src/oracle.rs)

### Integration Resources

- EVM
- [Phat-EVM Oracle Sample](./phat/contracts/evm_price_feed/README.md)
- [pink-web3](https://docs.rs/pink-web3): A web3 client for calling EVM chain JSON-RPC and handling EVM ABI codec
- ink! (WIP)
- Ink! [Phat-Ink! Oracle Sample](./phat/contracts/ink_price_feed/README.md)
- Substrate
- [Phat-Substrate Oracle Sample](./phat/contracts/sub_price_feed)
- [pink-subrpc](https://docs.rs/pink-subrpc/): A Substrate JSON-RPC client similar to Subxt, supporting HTTP(s)-only
Expand All @@ -235,7 +259,7 @@ For an in-depth explanation of the project's technical aspects, please refer to
Explore various examples and use cases of Phat Offchain Rollup in action:

- [Phat-EVM Oracle on Offchain Rollup](./EvmRollup.md)
- Phat-ink Oracle on Offchain Rollup (WIP)
- [Phat-Ink Oracle on Offchain Rollup](./InkRollup.md)
- Phat-Substrate Oracle on Offchain Rollup (Documentation WIP)

## API Reference
Expand All @@ -245,6 +269,8 @@ Find API documentation for key components of the Phat Offchain Rollup SDK below:
- Phat Offchain Rollup API (WIP)
- [Pink-KV-Session](https://docs.rs/pink-kv-session/)
- EVM [PhatRollupAnchor](./evm/contracts/PhatRollupAnchor.sol)
- Ink! [PhatRollupAnchor](./ink/crates/phat_rollup_anchor_ink/README.md)
- Example of [Ink! contract](./ink/contracts/test_oracle/README.md) implementing Ink! [PhatRollupAnchor](./ink/crates/phat_rollup_anchor_ink/README.md)
- ink! Anchor Contract (WIP)
- Substrate [Offchain Rollup Anchor Pallet](https://github.com/Phala-Network/phala-blockchain/blob/master/pallets/offchain-rollup/src/anchor.rs)

Expand Down
6 changes: 6 additions & 0 deletions ink/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
resolver = "2"
members = [
"crates/phat_rollup_anchor_ink",
"contracts/test_oracle",
]
1 change: 1 addition & 0 deletions ink/artifacts/test_oracle/test_oracle.contract

Large diffs are not rendered by default.

Loading

0 comments on commit 001ecf0

Please sign in to comment.