Development Tools: Solidity, Hardhat, Truffle
1.1 Overview of Development Tools
Solidity
Hardhat
Truffle
1.2 Setup and Configuration
Prerequisites
Node.js: Install Node.js (v12 or later is recommended).
NPM: Comes with Node.js and is used for managing dependencies.
1.2.1 Hardhat Setup
1. Install Hardhat
npm install --save-dev hardhat
2. Initialize Hardhat Project
npx hardhat
3. Install Essential Plugins
npm install --save-dev @nomiclabs/hardhat-ethers ethers
4. Configure Hardhat (Optional)
Modify hardhat.config.js
to specify custom network settings, compiler versions, etc.
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.4",
networks: {
rinkeby: {
url: "https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID",
accounts: [`0x${YOUR_PRIVATE_KEY}`]
}
}
};
1.2.2 Truffle Setup
1. Install Truffle
npm install -g truffle
2. Initialize Truffle Project
truffle init
3. Configure Truffle
Open truffle-config.js
to specify network details and compiler options.
module.exports = {
networks: {
rinkeby: {
provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID`),
network_id: 4, // Rinkeby's network ID
gas: 4500000, // Gas limit
gasPrice: 10000000000 // 10 gwei
}
},
compilers: {
solc: {
version: "0.8.4" // Specify compiler version
}
}
};
4. Ganache for Local Testing
Start Ganache and configure it in truffle-config.js
as the development
network.
networks: {
development: {
host: "127.0.0.1", // Localhost
port: 7545, // Ganache default port
network_id: "*", // Any network (default)
}
}
1.3 Testing Smart Contracts
Testing is crucial for identifying bugs and vulnerabilities in smart contracts. Both Hardhat and Truffle offer tools to create, run, and manage tests.
1.3.1 Writing Tests with Hardhat
Create Test File: Place test files in the
test/
directory.Write Tests: Use Mocha and Chai (JavaScript libraries for testing and assertions).
const { expect } = require("chai");
describe("MyToken", function () {
let MyToken, myToken, owner, addr1, addr2;
beforeEach(async function () {
MyToken = await ethers.getContractFactory("MyToken");
myToken = await MyToken.deploy(1000);
await myToken.deployed();
[owner, addr1, addr2, _] = await ethers.getSigners();
});
it("Should assign the total supply to the owner", async function () {
const ownerBalance = await myToken.balanceOf(owner.address);
expect(await myToken.totalSupply()).to.equal(ownerBalance);
});
});
Run Tests
npx hardhat test
1.3.2 Writing Tests with Truffle
Create Test File: Place test files in the
test/
directory.Write Tests: Truffle tests use JavaScript or Solidity.
const MyToken = artifacts.require("MyToken");
contract("MyToken", accounts => {
it("should assign initial supply to the deployer", async () => {
const instance = await MyToken.deployed();
const balance = await instance.balanceOf(accounts[0]);
assert.equal(balance.toString(), web3.utils.toWei("1000", "ether"));
});
});
Run Tests:
truffle test
1.4 Deploying Contracts to Testnets and Mainnet
1.4.1 Deploying with Hardhat
Create Deployment Script: Place scripts in the
scripts/
directory.
async function main() {
const MyToken = await ethers.getContractFactory("MyToken");
const myToken = await MyToken.deploy(1000);
await myToken.deployed();
console.log("MyToken deployed to:", myToken.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Deploy to Network
npx hardhat run scripts/deploy.js --network rinkeby
1.4.2 Deploying with Truffle
Write Migration Script: Place scripts in the
migrations/
folder
const MyToken = artifacts.require("MyToken");
module.exports = function (deployer) {
deployer.deploy(MyToken, 1000);
};
Deploy to Network:
truffle migrate --network rinkeby
Last updated