1- An Introduction to write Phat Contracts in Rust and Ink! v4
Prerequisites
Cargo contract installed: https://polkaverse.com/11178/technical-guide-install-cargo-contract-ink-v4-37994
This tutorial uses Ubuntu 22.04, cargo 1.68 and cargo contract 2.0
Clone the phat-hello template
In order to initialize a new Phat Contract project you can use the phat hello world template:
git clone https://github.com/Phala-Network/phat-hello
This rust project contains two files:
- Cargo.toml : cargo manifest file
- lib.rs : source code file
In the cargo manifest file we can see the dependency on the crate ink
And the dependency on the extension pinkIn the source code file (lib.rs) we have :
The macro #[pink :contract]
Applied to the module, this macro is responsible for generating all of the code necessary for interracting with the Contracts pallet
The macro #[ink(storage)]
Applied on struct
types in order to flag them for being the contract's storage definition. There can only be one ink! storage definition per contract.
More information here: https://use.ink/macros-attributes/storage
The macro #[ink(constructor)]
Flags a method (or multiple methods) for the ink! storage struct as constructor making it available to the API for instantiating the contract.
More information here: https://use.ink/macros-attributes/constructor
The macro #[ink(message)]
Flags a method for the ink! storage struct as message making it available to the API for calling the contract.
More information here: https://use.ink/macros-attributes/message
Use the command “cargo contract build” to compile the project
cargo contract build
After building, you will have three artifacts :
- phat_hello.wasm: the contract’s WebAssembly blob (the contract code)
- phat_hello.json: a JSON file that contains just the contract’s metadata, used by the tools to interact with your contract
- phat_hello.contract: bundle with the combination of the metadata and the wasm code
Note: you can see the size of the contract . The default is to build the contract in debug which is usefull for developement because you can use the debug messages but you can see the size of size is 19.0K
If you build with the release mode, the size will be significantly lower
cargo contract build --release
Contract size is really important from a cost perspective. Smaller is the contract, cheaper is the interaction with the contract.
Run the Unit Tests
Use the command “cargo test -- --nocapture“ to run the Unit Tests and display the debug messages
cargo test -- --nocapture
Update the contract
In order to test easily this contract via the Phat Contracts UI, we will update the source code a bit to automatically add the 0x prefix to the address when it is missing.
Note: we use the macro “ink::env::debug_println” to display a message in the console logs.And update the unit test to test it:
cargo contract build
cargo test -- --nocapture
Deploy the contract via Phat Contracts UI
You can use the app https://phat.phala.network/ or https://phat-cb.phala.network/ to instantiate new contracts and interact with them.
You need to have some PHA in this testnet to do it.
Upload and Instantiate a new contract
Upload the .contract file (bundle with the combination of the metadata and the wasm code)
Submit two transactions to upload the contract code and instantiate a new instance of this contract.
Congrats! You first phat contract is instantiated.
Now you can interact with it. Click on the query: “get_eth_balance“
Put an address without the prefix 0x (you can use etherscan to find it) and click on the button “Run“ and sign the query
Display the result via the toolbar at the bottom
In the “Result” tab, you can see the output of the query
Git repository
https://github.com/GuiGou12358/phala-tutorials/tree/main/tuto1
Crypto-enthusiast, Defi & NFT believer, Dotsam Fam Astar Tech Amb & Phala Amb Web2 builder gradually migrating to Web3
Tutorials to write Phat Contracts in Rust and Ink!
0 comments