Loading...

Warning: Undefined array key "post_id" in /home/u795416191/domains/speqto.com/public_html/wp-content/themes/specto-fresh/single.php on line 22

Getting Started with DeFi – A Step-by-Step Guide to Building a Simple Staking DApp

Afzal Khan

19 August, 2025

DeFi Logo


Decentralized Finance (DeFi) has revolutionized how people interact with money by removing intermediaries like banks. In this guide, we’ll build a simple staking DApp where users can stake tokens and earn rewards—one of the core mechanisms of DeFi.

Why Learn DeFi?

DeFi allows developers to create open financial systems where users have full control over their assets. By learning how to build a staking smart contract, you’ll understand a core concept used in yield farming, liquidity mining, and decentralized savings applications.

Step-by-Step Guide to Building a Simple Staking DApp

1. Set Up the Project

Start by creating a project and installing the required dependencies:

mkdir my-staking-dapp
cd my-staking-dapp
npm init -y
npm install --save-dev hardhat @openzeppelin/contracts

Initialize Hardhat:

npx hardhat

and select “Create a basic sample project”.

2. Write the Staking Smart Contract

Inside the contracts folder, create Staking.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Staking {
    IERC20 public token;
    mapping(address => uint256) public balances;
    mapping(address => uint256) public reward;

    uint256 public rewardRate = 10; // 10% reward

    constructor(address _token) {
        token = IERC20(_token);
    }

    function stake(uint256 amount) public {
        require(amount > 0, "Cannot stake 0");
        token.transferFrom(msg.sender, address(this), amount);
        balances[msg.sender] += amount;
        reward[msg.sender] += (amount * rewardRate) / 100;
    }

    function withdraw() public {
        uint256 amount = balances[msg.sender];
        uint256 rewards = reward[msg.sender];
        require(amount > 0, "Nothing to withdraw");
        
        balances[msg.sender] = 0;
        reward[msg.sender] = 0;
        
        token.transfer(msg.sender, amount + rewards);
    }
}

This smart contract lets users stake ERC-20 tokens and earn simple rewards.

3. Compile and Deploy

Compile the contract:

npx hardhat compile

Deploy it by updating scripts/deploy.js:

async function main() {
  const [deployer] = await ethers.getSigners();

  const Token = await ethers.getContractFactory("Token"); // Your ERC20 token
  const token = await Token.deploy();
  await token.deployed();

  const Staking = await ethers.getContractFactory("Staking");
  const staking = await Staking.deploy(token.address);
  await staking.deployed();

  console.log("Token deployed at:", token.address);
  console.log("Staking contract deployed at:", staking.address);
}
 
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

4. Stake and Earn Rewards

Once deployed, you can interact with the contract in the Hardhat console:

npx hardhat console --network localhost

Inside the console:

const staking = await ethers.getContractAt("Staking", "STAKING_CONTRACT_ADDRESS");
await token.approve(staking.address, 1000);
await staking.stake(1000);
await staking.withdraw();

You’ll see rewards credited when withdrawing your tokens.

How This Helps You

By building this simple staking contract, you’ve learned how token staking and rewards work—a key component of DeFi. With these basics, you can extend your DApp to support liquidity pools, governance tokens, and complex reward mechanisms like those used in popular protocols.

Conclusion

DeFi is reshaping finance, and staking is one of its most fundamental building blocks. In just a few steps, you created a staking contract where users can lock tokens and earn rewards. This foundation prepares you to build advanced DeFi protocols like yield farming, lending platforms, and decentralized exchanges. The future of finance is decentralized—your journey starts here.

RECENT POSTS

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications

Socket.IO Security Unveiled: Mastering Authentication & Authorization for Robust Real-time Applications Divya Pal 4 February, 2026 In the dynamic landscape of modern web development, real-time applications have become indispensable, powering everything from chat platforms to collaborative editing tools. At the heart of many of these interactive experiences lies Socket.IO, a powerful library enabling low-latency, bidirectional […]

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability

Prisma ORM in Production: Architecting for Elite Performance and Seamless Scalability Shubham Anand 16 February 2026 In the rapidly evolving landscape of web development, database interaction stands as a critical pillar. For many modern applications, Prisma ORM has emerged as a powerful, type-safe, and intuitive tool for interacting with databases. However, transitioning from development to […]

Streamlining DevOps: The Essential Guide to Gatling Integration in Your CI/CD Pipeline

Streamlining DevOps: The Essential Guide to Gatling Integration in Your CI/CD Pipeline Megha Srivastava 04 February 2026 In the dynamic landscape of modern software development, the quest for efficiency and reliability is paramount. DevOps practices have emerged as the cornerstone for achieving these goals, fostering seamless collaboration and rapid delivery. Yet, even the most robust […]

Fortifying Your Enterprise: Playwright Best Practices for Unbreakable Test Resilience

Fortifying Your Enterprise: Playwright Best Practices for Unbreakable Test Resilience Megha Srivastava 04 February 2026 In the dynamic landscape of enterprise software development, the quest for robust, reliable, and efficient testing is paramount. As systems grow in complexity, the challenge of maintaining an ironclad testing suite that withstands constant evolution becomes a critical differentiator. This […]

The TanStack Query Revolution: Elevating Your Data Fetching Paradigm from Basic to Brilliant

The TanStack Query Revolution: Elevating Your Data Fetching Paradigm from Basic to Brilliant GAURAV GARG 04 February 2026 In the dynamic landscape of web development, managing server state and data fetching often presents a labyrinth of challenges. From stale data and intricate caching mechanisms to race conditions and manual error handling, developers frequently grapple with […]

POPULAR TAG

POPULAR CATEGORIES