Skip to content

A repo of Phala's Phat Contracts and boiler plate implementations sorted into different branches

Notifications You must be signed in to change notification settings

HashWarlock/phat-contract-examples

Repository files navigation

Fat Contract Interact w/ OnFinality RPC Nodes

This workshop will demonstrate how to make an HTTPS request to OnFinality RPC nodes.

Introduction

Fat Contract is the programming model adopted by Phala Network. Fat Contract is NOT smart contract.

Instead, it aims to provide the rich features that ordinary smart contracts cannot offer, including:

  • CPU extensive computation: exclusive off-chain execution at the full CPU speed
  • Network access: the ability to send the HTTP requests
  • Low latency: non-consensus-sensitive operations may not hit the blockchain at all, removing the block latency
  • Strong consistency: consensus-sensitive operations remain globally consistent
  • Confidentiality: contract state is hidden by default unless you specifically expose it via the read call

Fat Contract is 100% compatible with Substrate's pallet-contracts. It fully supports the unmodified ink! smart contracts. Therefore you can still stick to your favorite toolchain including cargo-contract, @polkadot/contract-api, and the Polkadot.js Extension.

About this workshop

This workshop will demonstrate how to make an HTTPS request to OnFinality RPC nodes.

Environment Preparation

An operating system of macOS or Linux systems like Ubuntu 18.04/20.04 is recommended for the workshop.

  • For macOS users, we recommend to use the Homebrew package manager to install the dependencies
  • For other Linux distribution users, use the package manager with the system like Apt/Yum

The following toolchains are needed:

  • Rust toolchain
    • Install rustup, rustup is the "package manager" of different versions of Rust compilers: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • This will install rustup and cargo
  • Ink! Contract toolchain
    • Install binaryen with
      • Homebrew for macOS: brew install binaryen
      • Apt for Ubuntu: sudo apt install binaryen
      • or download the release and put it under your $PATH
    • Install contract toolchain: cargo install cargo-contract --force
  • Install frontend toolchain

Check your installation with

$ rustup toolchain list
# stable-x86_64-unknown-linux-gnu (default)
# nightly-x86_64-unknown-linux-gnu

$ cargo --version
# cargo 1.58.0 (f01b232bc 2022-01-19)

$ cargo contract --version
# cargo-contract 0.17.0-unknown-x86_64-linux-gnu

$ node --version
# v17.5.0

$ yarn --version
# 1.22.17

Create Polkadot Account to use Phala Testnet

  • Install Polkadot.js extension and import the Phala gas account following the tutorial
    • Gas account seed: misery blind turtle lottery random chalk flight fresh cute vanish elephant defy
  • Connect to Phala Testnet
  • Send some coins to your own account (limited, don't be evil);
    • Create your own account following tutorial
    • Send some coins.

[Deprecated] Play with our deployed version

Compile the contract

cargo +nightly contract build

Also test to ensure everything is fine

cargo +nightly contract test

You will find the compile result at ./target/ink:

$ ls -h target/ink
# fat_sample.wasm  metadata.json ...

Deploy

Collect the above two files and create the contract in Phala Testnet (PoC 5). The contract deployment can be divided into two steps: code upload and contract instantiation.

We recommend to keep a tab for explorer so you will not miss any historical events.

Code upload

Choose Developer - Extrinsics, and select the extrinsic phalaFatContracts and uploadCode, drag the fat_sample.wasm file and send the transaction.

A event of phalaFatContracts.CodeUploaded should be observed in the block explorer with the code hash, also you can go to Developer - Chain state and select the extrinsic phalaFatContracts and code to see the existing code.

Code upload could failed if the wasm code is already on chain.

Contract instantiation

Choose Developer - Extrinsics, and select the extrinsic phalaFatContracts and instantiateCode. We explain the arguments as follow:

  • codeIndex: the code to use, choose WasmCode and type in the hash of you uploaded code
  • data: the instantiation argument. We shall call the constructor function of the contract will the specific function selector. This can be found in the metadata.json (in this case, 0xed4b9d1b)
...
    "constructors": [
    {
        "args": [],
        "docs": [],
        "label": "default",
        "payable": false,
        "selector": "0xed4b9d1b"
    }
],
...
  • salt: some random bytes to prevent collision, like 0x0 or 0x1234
  • deployTo: we have prepared a cluster with 0x0000000000000000000000000000000000000000000000000000000000000001. In the future, customized cluster will be enabled.

There are three events to observe, all these events contain your contract ID

  • phalaFatContracts.Instantiating, the chain has receive your request and start instanting;
  • phalaFatContracts.PubkeyAvailable, the gatekeeper has generated the contract key to encrypt its state and input/output;
  • phalaFatContracts.Instantiated, your contract is successfully instantiated.

You can go to Developer - Chain state and select the extrinsic phalaFatContracts and contracts to see all the contracts.

Handle instantiation failure

For now, the contract execution log is not directly available to the developers. Join our Discord and we can help forward the Worker logs if necessary.

Interact with the contract

Phala provides js-sdk to simplified the frontend development. It already contains the frontend for the demo contract, check its example folder.

Follow the steps to run the frontend

  1. Download Phala-Network/js-sdk

    git clone --branch ethdenver-2022 https://github.com/Phala-Network/js-sdk.git
  2. Compile and run the frontend. By default it will serve the app at http://localhost:3000:

    cd js-sdk
    
    yarn
    yarn dev

You shall see the identical page as we have deployed.

Appendix

Resources

Endpoints

About

A repo of Phala's Phat Contracts and boiler plate implementations sorted into different branches

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published