Quick Start
Here you will find instructions on how to get started with your dApp project. Rather than delving deep into extensive documentation, you can use this quick start guide to up and running in a few steps.
Work Environment
As previously explained, we will use Truffle as our environment in this step-by-step tutorial. Make sure to have it correctly installed in your workspace.
Initialization
Create a new directory and navigate into it. Execute the following commands in this new (and empty) directory:
npm init
truffle init
You then need to adapt the project dependencies in package.json to enable the deployment of the compiled smart contract:
"dependencies": {
"@truffle/hdwallet-provider": "^1.7.0",
"truffle": "^5.4.30"
},
Install the dependencies using the following command:
npm install
Configuration
After installing your preferred development framework, the next step is to configure it to work with ZENIQ Smart Chain. Here's an example configuration for Truffle's truffle-config.js file:
const HDWalletProvider = require('@truffle/hdwallet-provider');
const MNEMONIC = "12 word phrase"
...
module.exports = {
...
networks: {
...
zeniq: {
provider: () => new HDWalletProvider(MNEMONIC, 'https://api.zeniq.network'),
network_id: "*"
}
},
};
Writing Smart Contract
Edit the smart contract in the contracts directory. Here's an example using a counter contract Counter.sol:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Counter {
uint public count;
constructor() {
count = 0;
}
function get() public returns (uint) {
return count;
}
function inc() public {
count += 1;
}
function dec() public {
require(count > 0, "Can not decrement further");
count -= 1;
}
}
Testing
You can launch unit tests with Truffle. To do this, you need to write some tests in the test directory. Use TestCounter.js as the test file:
const counter = artifacts.require("Counter");
contract("Testing Counter contract for test purposes", async accounts => {
it("checking initial state", async () => {
const instance = await counter.deployed();
const start_value = await instance.get.call();
assert.equal(start_value, 0, 'Increment failed');
await instance.inc();
var current_value = await instance.get.call();
assert.equal(current_value, 1, 'Increment failed');
await instance.dec();
current_value = await instance.get.call();
assert.equal(current_value, 0, 'Decrement failed');
});
});
Compiling
To deploy (or migrate) the smart contract to the blockchain, Truffle uses some source code to control the order and functionality. The first step is to add the following content to a file named 2_deploy_contracts.js in the migrations directory:
var Counter = artifacts.require("Counter");
module.exports = function(deployer) {
deployer.deploy(Counter);
};
You can then compile the smart contract with this command:
truffle compile
Deployment
If the smart contract successfully compiles, you can upload it to the Blockchain:
truffle migrate --network zeniq
Or alternatively, force the upload:
truffle deploy --network zeniq --reset
After deploying the smart contract to the blockchain, you can run your unit tests:
truffle test --network zeniq
Interacting with Your Contract
To test individual aspects of the smart contracts, you can use the Truffle console:
truffle console --network zeniq
And then, perform various operations:
let contract = await Counter.deployed()
contract.get.call()
contract.inc()
contract.dec()
contract.dec()
For further experiments with smart contracts, consider exploring the OpenZeppelin framework, specifically the Contracts Wizard.