Deploying smart contracts to the Qan private Chain

Y.N.
CoinsBench
Published in
4 min readJan 15, 2024

--

This guide will show you step by step how to deploy a solidity smart contract to the new Qan Chain. Please note that this chain is very new, and therefore this guide is in no way exhaustive but rather just an example to help developers get onboard of this new exciting ecosystem. There might be additional changes to this guide or to the Qan ecosystem in the future, and therefore this guide might be subject to change too.

Step 1 : Get a x-link signed address and private key

Qan is a quantum resistant ethereum compatible blockchain, and this process is achieved through x-link signed addresses. Just install x-link through docker and generate an address and private key following this guide. This is a short process that shouldn’t take more than 5 mins if you are used to docker. Once started on your machine, Xlink will validate your transactions to bypass the qantum security check for 24 hours even if you close it.

https://learn.qanplatform.com/developers/qan-testnet/setting-up-qan-testnet/2.-install-qan-xlink

Note: use the following command to run docker and generate the key.

docker run -d --name=xlink --restart=always --volume=xlink:/xlink qanplatform/xlink 0 http://rpc.qanx.live:8545

Once you’re done with the process you will be able to find your mnemonic and privatekey clicking on Volumes and then xlink. Just click privkey file to show the pkey.

Step 2 : Create a npm folder and install necessary dependencies

Simply clone this git https://github.com/zorglob21/qan-solidity-deployment or create a npm repository and install the packages yourself.

git clone https://github.com/zorglob21/qan-solidity-deployment

Then open a terminal window into the cloned directory and build the dependencies

npm i

Step 3 : create and compile a smart contract

Create your smart contract in the contracts folder, and save it with a .sol extension.

Now we will compile it. To make sure we have the correct compiler version for our contract, install the appropriate solc version with:

npm install --save-dev solc@0.8.21 //whatever version you need!

then we will compile it with this solc command :

npx solcjs --bin --abi --include-path node_modules/ --base-path . --output-dir ./bin ./contracts/<REPLACEWITHYOURCONTRACTNAME>.sol

The compiled files will appear in the bin folder.

Step 4 : Deploying

Now that our binary files and ABI are generated, we just have to deploy to the qan live chain.
For this, we will use the last version of ethers library. This is important as older versions might not work.

But first, we must input the private key we generated in step one in a .env file. So create a .env file in the root of the folder and insert the private key you generated with X-link.

PRIVATE_KEY=XXXXXXXXXXXXXXXXXXXXXXXX

We already have this deployment script ready from the git folder. Just replace the abi and binary path with the appropriate filenames generated by solc for your contract, and paste your actual deployment address as an argument in the getTransactionCount() function.

  wallet = await wallet.connect(provider);
const abi = fs.readFileSync("./bin/<SET YOUR FILE NAME HERE>.abi", "utf8");
const binary = fs.readFileSync("./bin/<SET YOUR FILE NAME HERE>.bin", "utf8");

const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("Start deployment!");

const nonce = await provider.getTransactionCount("PASTE YOUR ADDRESS HERE");
console.log(nonce)

// Set optimization settings
const overrides = {
gasPrice: '90000000000', // Adjust the gas price accordingly
gasLimit: '10000000', // Adjust the gas limit accordingly
// Enable Solidity optimizer
nonce: nonce,
optimizer: {
enabled: true,
runs: 200, // Number of runs for the optimizer (adjust as needed)
},
};

Notice the gasPrice and gasLimit settings, and also the nonce count. At the moment this article was written, those values were correct and necessary but they might be subject to change in the near future. Provide any additional arguments for your contract constructor in the deploy function, before passing overrides.

Once all of this is done, we just need some testneet qan :

https://learn.qanplatform.com/developers/qan-testnet/setting-up-qan-testnet/3.-request-tokens-from-the-faucet

and then we’re good to go:

node deploy.js

If you don’t get an error message, you should this printed on the terminal:

Start deployment!
0

The number is the nonce (the number of transactions emitted by your address). Wait a few seconds and your transaction should appear on the qanlive explorer

Et voilà!

--

--