Phala Turbo Repo: Kickstart Your Phala Network Development

Developing production-grade applications within a mono repository can be a daunting task, especially without proper package management. As a developer focused on agility and innovation, you need a build system, that not only matches your pace but also ensures stability.

Monorepositories are gaining substantial traction in the world of project management, especially within large enterprises. This methodology, originally pioneered by Google, is now widely adopted due to its exceptional ability to facilitate package sharing, promote code reusability, foster collaboration, and streamline the bug-fixing process.

In this article, we will introduce you to a powerful build system called Turbo Repo. Developed by the Vercel team, Turbo Repo is designed to enhance the developer experience. It boasts an incremental bundler optimized for JavaScript and TypeScript, and it's written in Rust. We'll explore how you can leverage this monorepository setup to create robust, internet-aware DApps for the Phala Network.

Setting up Phala Turbo Repo

Let's dive right in and set up your Phala Turbo Repo environment. Follow these steps to get started:

  • Create a new Phala Turbo Starter from this GitHub repository using the following commands:
npx degit markkos89/turbo-monorepo-template my-monorepo
cd my-monorepo
pnpm install

This will initialize a new project called my-monorepo for you, with a folder structure similar to the one below:

πŸ“¦ root
 ┣ πŸ“‚ .github
 ┣ πŸ“‚ .husky
 ┣ πŸ“‚ .vscode
 ┣ πŸ“‚ apps
 ┃  ┣ πŸ“‚ lens-api-code-examples
 ┃  ┣ πŸ“‚ lens-oracle
 ┃  ┣ πŸ“‚ website-ssr
 ┃  β”— πŸ“‚ website
 ┣ πŸ“‚ packages
 ┃  ┣ πŸ“‚ eslint-config-custom
 ┃  ┣ πŸ“‚ jest-config
 ┃  ┣ πŸ“‚ lighthouse-config
 ┃  ┣ πŸ“‚ next-config
 ┃  ┣ πŸ“‚ playwright-config
 ┃  ┣ πŸ“‚ storybook-config
 ┃  ┣ πŸ“‚ tailwindcss-config
 ┃  ┣ πŸ“‚ typescript-config
 ┃  ┣ πŸ“‚ ui
 ┃  β”— πŸ“‚ utils
 ┣ πŸ“œ package.json
 β”— πŸ“œ turbo.json

In the apps directory, you'll find templates to kickstart your projects, including lens-api oracle templates, server-side rendered website templates, and lens oracle templates.

Developing with Next.js

If you wish to develop with Next.js and want to start apps/website-ssr and apps/website in development mode while watching for changes in packages/ui, run the following command at the root:

pnpm dev

Building Next.js for Production

To build apps/website-ssr and apps/website for production, execute the following command at the root:

pnpm build

If you want to analyze the generated bundles, specify the ANALYZE environment variable:

ANALYZE=true pnpm build

Previewing Next.js Production Builds

To preview production builds of apps/website-ssr and apps/website, run the following command at the root:

pnpm start

Developing with Storybook

For developing with Storybook, if you wish to build all Storybook projects, execute this command at the root:

# run in development mode
pnpm storybook:dev 

# build for production
pnpm storybook:build 

Running Tests

To run unit/e2e tests for all projects, use this command at the root:

# to run unit tests
pnpm test:unit

# to run e2e tests
pnpm test:e2e

Linting

For linting across all projects, run the following command at the root:

pnpm lint

Collaborative Development with Phala Turbo Repo

The Phala Turbo Repo is designed to foster collaboration among developers. It provides a shared workspace where developers can work together on projects, review each other’s code, and contribute to the development of applications that leverage Phala Network decentralized computing. This collaborative environment not only speeds up the development process but also ensures the production of high-quality, robust applications.

Streamlined Bug Fixing

One of the key features of the Phala Turbo Repo is its streamlined bug-fixing process. Developers can easily track, report, and fix bugs in their applications. The repo provides tools for bug tracking and version control, making it easier for developers to identify and resolve issues. This leads to more stable and reliable applications.

Code Reusability and Package Sharing

The Phala Turbo Repo promotes code reusability and package sharing. Developers can share their code packages with others, allowing them to reuse code snippets and avoid reinventing the wheel. This not only saves time but also leads to more consistent and efficient code.

Leveraging Phala-Turbo-Repo for Phala Development

Now that you've familiarized yourself with Phala Turbo Repo and set up your development environment, let's delve into leveraging this powerful tool for Phala Network development. Building applications that utilize Phat contracts and interact with the Phala Network's off-chain Workers can open up a world of possibilities. Here's how you can take advantage of the Phala Turbo Repo for your Phala development journey:

Phat Contracts

Phat contracts serve as a bridge between off-chain and on-chain worlds within the Phala Network ecosystem. These contracts run on Phala Network's off-chain Workers and enable various capabilities, including internet connectivity, off-chain computation, and verifiable computation for smart contracts.

Creating a Phat Bricks Profile

To work with Phala Network and Phat contracts, you need a Polkadot account. Follow these steps to create your Bricks Profile account on the Phala PoC5 Testnet or Phala Mainnet:

  • Visit the Phala PoC5 Testnet or Phala Mainnet.

  • Create your Bricks Profile account by following the provided instructions. You may refer to this YouTube video on setting up your account from scratch.

  • Once your Bricks Profile account is created, you will have access to an overview of your account information.

Deploying a simple Phat function

The Phala monorepository comes prepacked with all you need to work with Phala already set up and can help you deploy your contract at a go. Let's see how we can deploy a simple function. First cd into apps/lens-oracle.

Step 1: Install dependencies and compile contract

# install dependencies
$ pnpm install --filter lens-oracle

Step 2: Build your phala function

# build phala function
pnpm build-function

Step 3: Running local tests

# test your function locally
pnpm hardhat test

Step 4: Compile the consumer contract

# run compile
pnpm compile

Step 5: Deploy the consumer contract on localhost

# Run a hardhat node
pnpm hardhat node

# deploy to local node
pnpm localhost-deploy 

Step 6: Test your function locally

# start watching your node
pnpm localhost-watch <deployed_contract_address> artifacts/contracts/TestLensApiConsumerContract.sol/TestLensApiConsumerContract.json dist/index.js -a https://api-mumbai.lens.dev/

# run a test push message
LOCALHOST_CONSUMER_CONTRACT_ADDRESS=<deployed_contract_address> pnpm localhost-push-request

Congrats, you have just accessed the internet from a smart contract. You can learn more about Phala network by going to their docs. You can also take a look at this example to get started.

Subscribe to Anyaogu
Receive the latest updates directly to yourΒ inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.