Note: Feedback and contributions are welcome. Please add issues for any features or bugs found. Join our discord and jump in our #dev channel to speak with our devs.
Contract Build Requirements
Ensure your rust toolchain requirements are installed correctly to ensure your contracts can be built correctly.
Swanky Suite
Swanky Suite aims to be an "all-in-one" tool for Wasm smart contract developers. It is modeled on existing tools like cargo contract CLI and polkadot.js. The swanky-cli tool is built with the oclif (Open CLI Framework repo).
Building with oclif allows for the swanky-cli to be extended through the plugin-plugin feature. Now the swanky-plugin-phala plugin can be added to the swanky-cli (note: must install swanky-clifirst) with the command swanky plugins:link [PATH_TO_REPO]. Specific installation instructions and examples will be displayed later, so do not worry about getting this set up now.
DevPHAse
DevPHAse is a developer tool built and maintained by Phala community member 100k. DevPhase is a command line utility that enables a developer building Phat Contracts to:
Install binaries for phala-node, pherry and pruntime.
Install local testnet configuration files to allow for Phat Contracts to be deployed into a cluster with a systems contract to enable SideVM for logging.
Create testnet accounts.
Start the local testnet.
Configure the local testnet by:
Registering Workers and their endpoints
Register Gatekeeper
Upload systems contract code
Deploy a Cluster with the su account (Default: Alice)
Deploy driver contract code
Phat Contract Tokenomics
SideVm Deployer
Authorize Logger SideVM Contract
Upload SideVM Logger to cluster and Deploy SideVM Logger
Compile Phat contracts into a .wasm, metadata.json and .contract file(combines the .wasm and metadata.json file into one .contract file).
Upload and Instantiate a compiled Phat Contract to a cluster on the local testnet.
Call (queries and transactions) instantiated Phat Contracts directly.
Check local dev environment setup.
Getting Started
Installation
Currently swanky phala will only support Ubuntu Linux. Other Linux operating systems are support, but this requires downloading and compiling phala-node, pherry and pruntime binaries locally. Then copy the binaries to the stacks/ folder.
Install Swanky CLI
Follow the instructions from Astar Network wiki here.
Easily get started with a new Phat Contract project by executing swanky phala init [PROJECT_NAME]
swankyphalainittest-swanky-phala
This will initiate a round of questions to get your Phat Contract template configured. First question will ask:
? Which contract language should we use? (Usearrowkeys)β―pink
At this moment, pink is the only contract language option at this time. pink is the Phala ink library to support rust-based ink! language in Phalaβs pRuntime workers.
Next youβll be asked the what contract template to use (blank, http_client, or phat_hello more to come!).
? Which contract template should we use? (Usearrowkeys)blankβ―http_clientphat_hello
Now that you have a Phat Contract language and template picked, you will be asked to create a name for the PC. (default is the template name)
? What should we name your contract? (http_client)
After you select a name, youβll be asked for your git username (default is git config --get user.name) and email.
? What is your name? (HashWarlock)? What is your email?
An expected final output will display when the template files and project directory is created.
? Which contract language should we use? pink? Which contract template should we use? phat_hello? What should we name your contract? phat_hello? What is your name? hashwarlock? What is your email?βCheckingdependenciesOKInitializingβCopyingtemplatefilesOKβProcessingtemplatesOKβInitializinggitOKβInstallingdependenciesOKβ Copyingdevphaseconfigfiles[Initializer]Creatingdirectories[Initializer] Creating files[Initializer] devphase.config.ts[Initializer] accounts.json[Initializer] scripts[Initializer] Creating sample contract[ContractManager] Contract created in:[ContractManager] /home/hashwarlock/Projects/Phala/Swanky/demo/swanky-plugin-phala/test-swanky-phala/contracts/flipperβCopyingdevphaseconfigfilesOKβ Installingphalanode,pruntimeandpherrybinaries[StackBinaryDownloader]PreparingPhalastackrelease[StackBinaryDownloader] Creating stack directoryβCheckingreleasesdirectoryβCheckingtargetreleasebinariesβInstallingphalanode,pruntimeandpherrybinariesOKπPhatContractprojectsuccessfullyinitialised!π
Execute ls to list the new project directory called test-swanky-phala in the current working directory.
Created a project directory with a configured template Phat Contract.
Downloaded Phala binaries, drivers, system contracts and sideVM programs.
Started and set up a local testnet to start deploying Phat Contracts to a worker node in the deployed 0x01 cluster.
Letβs begin the process of deploying the Phat Contract, but first the PC must be compiled to get the contract.wasm, metadata.json and contract.contract files that will be used to upload and instantiate the PC to the local testnet cluster.
Note: Follow these installation steps before continuing to ensure you can compile your Phat Contract.
We can compile the PC with swanky phala contract compile -c [CONTRACT_NAME] and will look like the follow:
If you have compiled your contract already, the types will be auto-generated at the end of a successful compilation. In the case that the types were not generated for the contract, execute the swanky phala contract typegen [CONTRACT_NAME].
An example of the output will create typings/ folder with a TypeScript file of the contract's generated types.
Sometimes there are difficult to find errors that lie within a succesfully compiled wasm blob. The command swanky phala contract validate -c [CONTRACT_NAME] will do a preliminary check to verify if the compiled contract is valid for deployment.
Let's deploy the phat_hello contract to the local testnet in cluster 0x0000000000000000000000000000000000000000000000000000000000000001. Note if there is no account defined then alice will deploy the Phat Contract by default.
Now we can interact with our deployed contract by taking the Contract Id: 0x2e11166f9a623f7536434b5f4456b2311d3bb06717dd91a376380a61b8f9b0a8 returned from deploying phat_hello successfully. There is a function called get_eth_balance that takes a hex string of the account address. This is how the composed call would look like.
Note:
ETH address has to be converted to the Hex representation 0x307844306645333136423966303141336235666436373930463838433244353337333946383042343634 of the account opposed to using the Account ID 0xD0fE316B9f01A3b5fd6790F88C2D53739F80B464. This can be retrieved through the @polkadot/util method stringToHex(0xD0fE316B9f01A3b5fd6790F88C2D53739F80B464). Check the phat_hello.test.ts for the example.
To add a new contract there is the swanky phala contract new [CONTRACT_NAME] command. This can be done at the root of your new project folder and would look like the following.
swankyphalacontractnewnew_contract
Expected output:
CreatingnewPhatContract? Which contract language should we use? pink? Which contract template should we use? flipper? What should we name your contract? new_contract? What is your name? hashwarlock? What is your email?βCheckingdependenciesOKInitializingβCopyingtemplatefilesOKβProcessingtemplatesOKπSuccessfullycreatednewPhatContract!πββ[$]> lscontractstotal0drwxr-xr-x.1hashwarlockhashwarlock58Apr417:16.drwxr-xr-x.1hashwarlockhashwarlock362Apr417:16..drwxr-xr-x.1hashwarlockhashwarlock26Apr417:16new_contractdrwxr-xr-x.1hashwarlockhashwarlock58Apr416:45phat_hello
Create/List Accounts
There is a basic dev account generation that can be protected by a configured password if desired. By default, the list of accounts can be seen with swanky phala account list.