Building DApps for Celo (Part 2)
In Part 1, we went over the basics for building and testing smart contracts on the Celo platform. However, to really build anything useful on Celo, we need to have the ability to interact and test integrations with the core Celo contracts.
Core Contracts
celo-devchain
At high level, what we need to do is to compile, build and deploy all the Celo core smart contracts in our local ganache instance. This way, we can have a working version of a local blockchain that also has all the core code deployed.
To do all this, we will use:
celo-devchain: Simple package that contains pre-compiled and pre-built Celo core contracts. This package is based on the guide here, but is nicely packaged up to be used without introducing massive dependencies on the `celo-monorepo`.
> npm install --save-dev celo-devchain
Now instead of using `ganache-cli` directly, we will use `celo-devchain` to start instance of `ganache` locally that has all of the core contracts already deployed to it.
> npx celo-devchain --port 7545 --core v1
Once Ganache is initialized, it will also printout addresses for all of the core contracts.
OpenZeppelin
OpenZeppelin: Provides building block smart contracts and also interfaces for standardized token representations.
Using OpenZeppelin is definitely not necessary, but it does provide a lot of nice helpers and core building blocks to build production ready smart contracts.
> npm install --save-dev @openzeppelin/contracts
HelloContract: Part 2
We will now enhance our HelloContract to support following:
Allows any user to lock CELO through this contract.
Allows any other user to unlock and withdraw locked CELO held through this contract.
While this isn’t going to be very practical or useful smart contract, it will allow us to demonstrate how to integrate with the core Celo contracts.
From here on out, two most useful references for you should be:
Code
Instead of going through things step-by-step, it is probably easier to checkout the code from the example repository and to explore it on your own.
Code on Github: https://github.com/zviadm/celo-hellocontracts/tree/main
Code in `./contracts/interfaces` is copied from `celo-monorepo`. Copying code is never perfect, but adding dependency to `celo-monorepo` would be even worse.
Notice migration code in `./migrations-ts/2_deploy_hellocontract.ts`. Contract addresses will need to be different when deploying to test-nets like `alfajores` or `baklava`, or when deploying to main-net.
Tests
Code on Github: https://github.com/zviadm/celo-hellocontracts/blob/main/test-ts/test-hellocontract.ts
Notice `increaseTime` helper from `celo-devchain` package. This allows us to fast forward time on the blockchain to test time related functionality like unlocking CELO.
References
OpenZeppelin: https://docs.openzeppelin.com/contracts/3.x/
Core contracts: https://github.com/celo-org/celo-monorepo/tree/master/packages/protocol/contracts
celo-devchain: https://github.com/zviadm/celo-devchain
celo-hellocontracts: https://github.com/zviadm/celo-hellocontracts