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.
Let's dive right in and set up your Phala Turbo Repo environment. Follow these steps to get started:
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.
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
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
To preview production builds of apps/website-ssr
and apps/website
, run the following command at the root:
pnpm start
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
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
For linting across all projects, run the following command at the root:
pnpm lint
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.
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.
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.
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 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.
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.
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.