Unlock the Power of Blockchain: A Step-by-Step Guide to Creating an Escrow Smart Contract
What is Solidity?
Solidity is the programming language used to create smart contracts on the Ethereum blockchain. It’s an object-oriented language that’s similar to C++, Python, and JavaScript. Solidity compiles your smart contract into bytecode, which is then deployed on the Ethereum Virtual Machine (EVM).
Introducing Hardhat
Hardhat is a Solidity development environment built using Node.js. It’s a popular alternative to Truffle and Remix, offering a plug-and-play environment for testing and deploying smart contracts. With Hardhat, you can use JavaScript testing libraries like Chai and connect to your smart contract using Ethers.js.
Installing Hardhat
Installing Hardhat is straightforward. First, install npm and Node.js v12. Then, create a new directory and initiate your Node.js project.
npm init -y
Install Hardhat as a dev dependency, and autogenerate a hardhat.config.js file.
npm install --save-dev hardhat
Finally, install Ethers.js and Chai as dependencies.
npm install ethers.js chai
Creating Your First Solidity Smart Contract
In this tutorial, we’ll create a simple escrow smart contract using Solidity and Hardhat. The contract will allow users to deposit tokens and withdraw them using a unique hash.
Bootstrapping Your Smart Contract
We’ll use Open Zeppelin’s library of secure smart contracts to create our escrow contract. First, install Open Zeppelin’s library using npm.
npm install @openzeppelin/contracts
Then, create a new contracts directory and create two files: MockDaiToken.sol and Escrow.sol.
Escrow Deposit Function
The deposit function will transfer funds from the user’s wallet to the smart contract’s wallet. We’ll use a mapping to store the deposits and generate a unique hash based on the sender’s address, deposit amount, and deposit count.
pragma solidity ^0.8.0;
contract Escrow {
mapping (address => uint256) public deposits;
function deposit(uint256 _amount) public {
// Generate a unique hash
bytes32 hash = keccak256(abi.encodePacked(msg.sender, _amount, deposits[msg.sender]));
// Store the deposit
deposits[msg.sender] = _amount;
}
}
Withdrawing from the Escrow
To withdraw funds, we’ll create a separate function that accepts a transaction hash parameter. We’ll validate the transaction hash and ensure that the user has enough funds to withdraw.
function withdraw(bytes32 _hash) public {
// Validate the transaction hash
require(deposits[msg.sender] > 0, "Insufficient funds");
// Withdraw funds
deposits[msg.sender] = 0;
}
Testing Your Smart Contract
Hardhat makes testing easy with its built-in testing suite. We’ll configure our tests, plan happy and unhappy paths, and write unit tests using Chai and Ethers.js.
describe("Escrow contract", function() {
it("should deposit funds", async function() {
// Deposit funds
await escrow.deposit(10);
// Verify the deposit
expect(await escrow.deposits.call(accounts[0])).to.equal(10);
});
it("should withdraw funds", async function() {
// Withdraw funds
await escrow.withdraw(hash);
// Verify the withdrawal
expect(await escrow.deposits.call(accounts[0])).to.equal(0);
});
});
Deploying Your Smart Contract
Finally, we’ll deploy our smart contract to a Testnet using Hardhat. We’ll configure our deployments, create a deployment script, and deploy our contract to a local Testnet and the Rinkeby Testnet.
module.exports = {
solidity: "0.8.0",
networks: {
local: {
url: "http://localhost:8545",
},
rinkeby: {
url: "undefinedkeby.infura.io/v3/YOUR_PROJECT_ID",
accounts: ["YOUR_ACCOUNT_PRIVATE_KEY"],
},
},
};
npx hardhat run scripts/deploy.js --network local
npx hardhat run scripts/deploy.js --network rinkeby
Get Started with Blockchain Development
In this tutorial, we’ve covered the basics of Solidity, Hardhat, and creating an escrow smart contract. With this knowledge, you can start building your own blockchain applications and deploying them to the Ethereum network. Learn more about blockchain development and explore the possibilities.