In this guide, we are going to write a Node.js script to introduce some of the basic concepts that are important to understand how Celo works. This will get us started with connecting to the Celo network and learning how to develop more advanced applications.
At the end of this guide, you will be able to:
- Connect to the Celo test network, called Alfajores
- Get test CELO and Celo Dollars (cUSD) from the faucet
- Read account and contract information from the test network
- Transferring CELO and cUSD on the test network
To start, clone this GitHub repo. This is a Node.js application.
git clone https://github.com/critesjosh/helloCelo.git
We will be using the Celo ContractKit SDK to interact with the Celo test network (Alfajores). Let's install it. It is already defined in the package.json, so we can get it with
We will be writing our Node.js app in the
Import the contract kit into our script with
Now we can use the ContractKit to connect to the test network.
ContractKit contains a
contractsproperty that we can use to access certain information about deployed Celo contracts.
Let's read some token balances from the blockchain. Add the following line in the
We can get the CELO balance of an account using the token wrappers like
goldtoken.balanceOf(address). Let's check the balance of this address
balanceOf(address)function also returns a Promise, so we wait for the promise to resolve then we print the result.
To view the balances, run the script from the termainal with
Reading all account balances is a powerful feature of blockchains. Next, let's see how we can send value to each other on the testnet.
In order to do transfers (aka transactions), we need to:
- Create an account (by creating a private key)
- Fund it with test CELO and cUSDs
- Sign and send transactions to the network
We are accessing the Celo network via a remote node via HTTP requests at
Because we are accessing the network remotely, we need to generate an account to sign transactions and fund that account with test CELO.
There is a short script in
getAccount.jsto either get a Celo account from a mnemonic in the
.secretfile, or create a random account if the file is empty. In the script, we use
We can now use this
accountto get account information (ie the private key and account address) and to send transactions from
account.address. Add the following code to read the account balance. Continue adding to
Run this script again with
node helloCelo.js. This will print
0, as we have not funded the associated account yet.
We can get free test CELO and cUSDs on the test network for development via the Celo Alfajores faucet.
Copy your randomly generated account address from the console output mentioned above, and paste it into the faucet.
Once your account has been funded, run the command
node helloCelo.jsagain to see your updated balance.
We have an account with CELO and cUSD in it, now how do we send tokens to another account? Remember the token wrappers we used to read account balances earlier? We can use the same wrappers to send tokens, you just need to add the private key associated with your account to ContractKit (see line 10).
The token wrappers have a method called
transfer(address, amount)that allows you to send value to the specified address (line 14).
You need to
send()the transaction to the network after you construct it. The
send()methods accepts an option that allows you to specify the
feeCurrency, which allows the sender to pay transaction fees in CELO or cUSD. The default
feeCurrencyis CELO. In the following example, let's pay transaction fees in CELO when we transfer CELO and pay with cUSD when we transfer cUSD.
send()method returns a transaction object. We will wait for the transaction receipt (which will be returned when the transaction has been included in the blockchain) and print it when we get it. This receipt contains information about the transaction.
After we read the receipt, we check the balance of our account again, using the
balanceOf()function. The logs print our updated balance!
You may notice that the account balance is a bit smaller than the amount of tokens that we sent. This is because you have to pay for every update to the network.
Add the following code to the
helloCelo.jsto send a transaction.
node helloCelo.jsagain to send the transactions and see the printed output in the console.
The above instructions apply to building NodeJS applications. If you want to build an integration with a web application, you can still use the ContractKit by following slightly modified instructions.
You will need to have the following
Then, you can create a new instance of the ContractKit with the following code:
Once you have successfully created the ContractKit, you can use the various Celo contracts to sign transactions with a connected Ledger device. For example, here's how to transfer gold tokens (just like above in the NodeJS example):
This is the basic setup to integrate the Celo Ledger App with a web application. You can also view the Celo Ledger App example codebase for some other examples of connecting to a Ledger Device from a web application.
Congratulations! You have accomplished a lot in this short introduction to developing on Celo.
- Installing and setting up ContractKit
- Connecting to the Celo Alfajores network
- Getting the CELO contract wrapper
- Reading account balances using the CELO wrapper
- Generating a new account in Celo
- Funding an account using the Celo Alfajores Faucet
- Sending CELO
If you had any difficulties following this tutorial or simply want to discuss Celo tech with us you can join our community today!