How to deploy a smart contract to the XDC Network using CoralX
Overview
CoralX is a tool released by Securrency for Ethereum smart contract deployment and testing. Besides Ethereum, it also supports the EVM-compatible XDC network, where we are going to deploy a smart contract in this tutorial.
The smart contract we will create is a "Hello, World!" program, which simply outputs a constant string.
Setting up the environment
You will need to install Node.js on your system. Download and install it from https://nodejs.org/en/, and type these commands in a terminal.
If you see version numbers printed after both commands, your Node.js environment is ready.
Now that you have Node.js installed, let's install CoralX:
CoralX does not have its own init script yet, so we will borrow the scaffolding from Truffle. Install Truffle with:
If both npm install complete without errors, you are almost ready for smart contract development. There is only one thing left: Ganache, which provides a local development blockchain.
Install it from here. Open it and choose Ethereum Quickstart to start a local blockchain server then click the keys icon on the right of the first address to reveal its private key.
Finally, keep Ganache running in the background and save the private key for later use in this tutorial.
Setting up a smart contract project
Follow these steps to create your first smart contract project!
First, create a new folder and open a terminal there. This folder will be your project folder.
Second, run truffle init
in the terminal to initialize the directory structure, and remove .gitkeep
files that are not compatible with CoralX.
Third, scaffold (i.e. create from template) a contract with the following command.
Fourth, create CoralX configuration files. You need to create three files under your project folder, package.json
, coralX-config.js
and coralX-scenarios.js
as follows (skip the first line starting with //
).
Last, paste the private key from Ganache to privateKey
in the project folder and change http://127.0.0.1:7545
to the RPC server URL in Ganache. Make sure there are no newline characters in the file. We also need to create privateKey.apothem
because it's referenced in coralX-config.js
.
Writing the smart contract
In this tutorial, we will use the test-driven development (TDD) method, which means to write failing tests first, and then implement the functionality described by the tests.
Open the project folder in your favorite code editor. I'm using Visual Studio Code for this demo.
Now create a greeting.test.js
file in the test
folder:
Try to run this test, which only tests if Greeting is properly deployed to the development
network. In your project folder, run the following command:
You will see the following error, but don't panic. It is the spirit of TDD to write tests that fail at first and implement the missing pieces later on.
Now create the migration script required in the migrations
folder:
Try coralX test
again.
Almost there! You now have a working smart contract - though, as of right now, it does nothing. We do expect it to do something once you write a test case to describe the expected behavior:
Add this test in the describe("Greeting")
block and run coralX test
again.
A function is missing, so you should add it in the contract. Open contracts/Greeting.sol
and implement the function greet()
:
Run the tests again:
You now have a working contract ready to be deployed. But before you attempt to deploy, you must create a wallet on chain.
Creating an XDC wallet on Apothem Network (TestNet)
In coralX-config.js
, private keys must be configured to deploy smart contracts on networks. Let's generate a wallet on the Apothem Network, a test network for XDC.
First, open https://wallet.apothem.network/ and click "Create a new wallet". Follow the instructions to generate a software wallet with a keystore file.
Do not use this wallet in production ("MainNet"). We will put private keys on disk without encryption, which is not safe.
In step 3, click "Access Wallet" and choose software keystore. Select the keystore file you just downloaded and enter your password to log in.
Then, click "View paper wallet" as shown below to reveal address and private key of the wallet.
Save the private key verbatim to privateKey.apothem
in the project folder. If the private key starts with 0x
, only copy the string after it. Beware not to paste any newline characters into the file!
We also need some XDCt (XDC on Apothem Network) to pay the gas fee. Open https://faucet.apothem.network/, type in the wallet address from the paper wallet page, finish the CAPTCHA and click "REQUEST 1000 XDC".
If you can see a faucet transaction by searching your wallet address from https://explorer.apothem.network/, we are ready for deploying a smart contract.
Deploy the contract
Time for the final steps! Let's deploy your smart contract to a public network. Run the following command:
If you see no errors, open https://explorer.apothem.network/ and search for your wallet address:
Click the transaction you've just created and navigate to the contract address?
Next, verify the contract source code to enable interactions on the explorer. Choose the compiler version that was printed in your terminal. If you are not sure which version it is, just remove the build
folder in your project and run coralX compile
to get it.
After verification, the contract tab should show up and we can see the string hello
in the return value of the function greet()
.
Congratulations! Now that you have successfully deployed a smart contract on the Apothem Network with CoralX, you can start programming and deploying more complex contracts to the XDC Network.
Last updated