Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup anchor for ink! smart contracts #30

Merged
merged 67 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
105edad
Merge pull request #1 from Phala-Network/main
GuiGou12358 Jun 20, 2023
a9cc209
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
87dc18e
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
94f34c6
Merge remote-tracking branch 'origin/main'
GuiGou12358 Jul 14, 2023
8a6966b
rollup anchor for ink smart contract
GuiGou12358 Jul 14, 2023
385ace8
Add artifacts
GuiGou12358 Jul 15, 2023
2e350ab
Uodsate documentation
GuiGou12358 Jul 15, 2023
5fb1408
remove useless digit in the version number
GuiGou12358 Jul 21, 2023
d56c302
Refactoring:
GuiGou12358 Jul 21, 2023
860e654
reduce the visibility of 'Internal Traits' to only the crate
GuiGou12358 Jul 21, 2023
7491c97
allow by default clippy::inline_fn_without_body required in the attri…
GuiGou12358 Jul 21, 2023
8a598a7
refactor to use stable rust
GuiGou12358 Jul 21, 2023
78db124
refactor to use stable rust
GuiGou12358 Jul 28, 2023
8a4146f
refactor to use stable rust
GuiGou12358 Jul 28, 2023
d798b75
refactor to use stable rust
GuiGou12358 Jul 28, 2023
92cc283
move the integration tests in the tests directory
GuiGou12358 Jul 28, 2023
b3ee38b
update readme
GuiGou12358 Jul 28, 2023
fc67f1f
improvements
GuiGou12358 Jul 28, 2023
54d6449
improve the separation between rollup and meta transaction
GuiGou12358 Jul 28, 2023
73874a4
rename meta transaction
GuiGou12358 Jul 28, 2023
e049df1
improvement
GuiGou12358 Jul 28, 2023
10dd288
cargo fmt
GuiGou12358 Jul 31, 2023
efe5872
Update readme after the changes
GuiGou12358 Jul 31, 2023
92306ef
Update contract test after the changes
GuiGou12358 Jul 31, 2023
61016e7
Update crate for the phat contract after the changes (stable rust ver…
GuiGou12358 Jul 31, 2023
48f54d3
Update readme after the changes (stable rust version for ink! smart c…
GuiGou12358 Jul 31, 2023
d883605
Update the tests after the changes (stable rust version for ink! smar…
GuiGou12358 Jul 31, 2023
39ab8a3
Update the readme after the changes (stable rust version for ink! sma…
GuiGou12358 Jul 31, 2023
4a808f8
Update the artifacts after the changes (stable rust version for ink! …
GuiGou12358 Jul 31, 2023
9c1aa57
Merge pull request #2 from GuiGou12358/stable-rust
GuiGou12358 Aug 1, 2023
6aed3b3
Improve the readme
GuiGou12358 Aug 2, 2023
ef8e601
Check the tests
GuiGou12358 Aug 2, 2023
f1a4791
Delete .env file committed by mistake
GuiGou12358 Aug 7, 2023
a7d71e5
Update readme
GuiGou12358 Aug 7, 2023
ad88ecb
Use cargo contract 3.2.0 and rust toolchain 1.72.0
GuiGou12358 Sep 5, 2023
1a32aab
Use cargo contract 3.2.0 and rust toolchain 1.72.0
GuiGou12358 Sep 5, 2023
f6ede45
Use last version of ink!
GuiGou12358 Sep 5, 2023
3d9309c
Use last version of ink! and update dependency
GuiGou12358 Sep 5, 2023
e7a7e45
Update artifacts with last version of rust toolchain and ink!
GuiGou12358 Sep 5, 2023
6575d29
Update artifacts with last version of rust toolchain and ink!
GuiGou12358 Sep 5, 2023
280d342
Use last version of ink! and update dependencies
GuiGou12358 Sep 5, 2023
6d63ff8
Update pallet id for local substrate contract node
GuiGou12358 Sep 5, 2023
c2d8088
Update dependencies
GuiGou12358 Sep 5, 2023
d958620
fix cargo clippy issue
GuiGou12358 Sep 5, 2023
01ed1b1
fix typo
GuiGou12358 Sep 14, 2023
d6e51c3
Improve readme
GuiGou12358 Sep 14, 2023
9785772
reuse the encoded value
GuiGou12358 Sep 14, 2023
d3d8fe1
Remove the useless boolean in the return of some methods
GuiGou12358 Sep 14, 2023
105db1a
Fix a typo in some names containing 'rollup' (missing l)
GuiGou12358 Sep 14, 2023
3277426
remove useless field
GuiGou12358 Sep 14, 2023
8c6705b
Improve the error handling of method get_ecdsa_public_key
GuiGou12358 Sep 14, 2023
5c8892d
Use an enum instead of struct for implementing the actions
GuiGou12358 Sep 14, 2023
97c8396
Fix tests after the different updates
GuiGou12358 Sep 14, 2023
7acd72a
Updated artifacts after the different changes
GuiGou12358 Sep 14, 2023
e680f39
rename use_meta_tx by ensure_meta_tx_valid
GuiGou12358 Sep 15, 2023
fbc5889
rename use_meta_tx by ensure_meta_tx_valid
GuiGou12358 Sep 15, 2023
3a18b8b
refactor a little bit the test
GuiGou12358 Sep 15, 2023
1707c44
refactor a little bit the code
GuiGou12358 Sep 15, 2023
c33cff5
strict comparison between nonces in meta transaction
GuiGou12358 Sep 16, 2023
2ed56fd
update artifacts
GuiGou12358 Sep 16, 2023
4fd7174
use the last released pink-subrpc version
GuiGou12358 Sep 18, 2023
3494f55
remove commented dependency
GuiGou12358 Sep 18, 2023
670be39
Merge the traits KvStore, MessageQueue and RollupAnchor for simplific…
GuiGou12358 Sep 18, 2023
56b09c6
Merge the traits KvStore, MessageQueue and RollupAnchor for simplific…
GuiGou12358 Sep 18, 2023
1d7ebfa
Add the consumer contract address to add a domain separation and repl…
GuiGou12358 Sep 21, 2023
9f6db49
Get the attestor address used in direct, without using meta transaction
GuiGou12358 Sep 21, 2023
c244a2e
rename the method pub_key_to_ss58 to get_ecdsa_account_id
GuiGou12358 Sep 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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