VECHAIN Docs
Search
⌃K

Create a Hardhat Sample Project (Web3)

In this section the process of creating your own sample project using our hardhat plugins is shown. We start from the default hardhat project and modify it by adding our plugins to enable hardhat to connect to Thor.

Create a default hardhat project

mkdir my-project
cd my-project
npm init --yes
npm install --save-dev hardhat

Install the Vechain hardhat plugin and either the web3-providers-connex or the etherjs Vechain plugin

npm i --save-dev @vechain/web3-providers-connex
npm i --save-dev @vechain/hardhat-vechain
npm i --save-dev @vechain/hardhat-web3

Start Hardhat

Follow the steps and create a default Hardhat Javascript project
npx hardhat

Modify Default project

Hardhat Config File

Modify the hardhat.config.js file such that we can use the Vechain plugin
require("@nomicfoundation/hardhat-toolbox");
require("@vechain/hardhat-vechain");
require("@vechain/hardhat-web3");
require('@nomiclabs/hardhat-truffle5');
module.exports = {
solidity: {
version: "0.8.17",
},
networks: {
vechain: {
url: "http://127.0.0.1:8669",
accounts: {
mnemonic: "denial kitchen pet squirrel other broom bar gas better priority spoil cross",
count: 10,
},
restful: true,
gas: 10000000,
},
},
};
The options in the sample hardhat.config.js indicate which Thor node we will use as well as the default seed phrase which is initialized when Thor is in solo mode for development purposes. Here, by default the node that our plugin will connect to is on localhost. If you are running Thor on a different ip address then you can replace http://127.0.0.1:8669 with the IP address of your Thor node.

Default Contracts and Tests

Next we need to change the default contracts and tests since those provided by default require some hardhat-specific features that do not exist on a real blockchain like the one provided by Thor.
Remove everything under the contracts/ directory and create a new contract named Storage.sol with the following contents:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
* @custom:dev-run-script ./scripts/deploy_with_ethers.ts
*/
contract Storage {
uint256 number;
// Declaring an event
event StoreEvent(address owner);
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
emit StoreEvent(msg.sender);
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retrieve() public view returns (uint256){
return number;
}
}
This is a simple storage contract where it can store a uint256 on the blockchain using the store(uint256) function that can be retrieved later using the retrieve() function.
Also remove everything under the tests/ directory and create a new test named Storage.test.js with the following contents:
const Storage = artifacts.require('Storage');
const { expect } = require('chai');
contract('Storage', function (accounts) {
beforeEach(async function () {
this.Storage = await Storage.new({ from: accounts[0] });
});
it('default value is 0', async function () {
var ret = await this.Storage.retrieve();
expect(String(ret)).to.eql('0');
});
it('store/retrieve is working', async function () {
await this.Storage.store(15);
var ret = await this.Storage.retrieve();
expect(String(ret)).to.eql('15');
});
it('emits a storage event', async function () {
const ret = await this.Storage.store(15)
expect(ret.logs[0].event).to.be.equal("StoreEvent")
});
});
This is a simple Javascript test file where it uses the hardhat framework, alongside the Vechain plugins to
  • Deploy Storage.sol on Thor solo
  • Test the store and retrieve functionalities

Start Thor

To be able to run the tests on Vechain you need Thor installed, which is VeChain's node implementation. We have a detailed guide on how to do that here.

Running the tests

Finally, to run the test you just created simply use the following command:
npx hardhat test --network vechain