Blockchain isn’t just for trading tokens. Developers are finding new ways to bring everyday applications onto decentralized infrastructure. One example is payroll. Instead of using opaque systems, you can build a decentralized app (DApp) that calculates paychecks, handles deductions, and logs results transparently on-chain.
In this tutorial, we’ll walk through how to build and deploy a simple Paycheck Calculator DApp on the XDC Network. By the end, you’ll understand how to write the smart contract, deploy it using developer tools, and interact with it from a frontend.
Why XDC for a Payroll DApp?
The XDC Network is an EVM-compatible blockchain with features that make it attractive for enterprise and real-world applications:
- Ultra-low fees: Transactions cost a fraction of a cent, which matters if you’re calculating salaries for dozens or hundreds of employees.
- High throughput: With 2-second block times, paycheck operations confirm quickly.
- EVM compatibility: If you know Solidity, Hardhat, or Truffle, you’re already halfway there.
- Real-world focus: XDC positions itself as an enterprise blockchain, which aligns perfectly with payroll and finance apps.
These advantages make it an excellent choice for a DApp that needs to handle frequent calculations without racking up costs.
Step 1: Setting Up Your Environment
Since XDC is EVM-compatible, you can use the same development stack you’d use for Ethereum.
Prerequisites
Node.js and npm installed
MetaMask (with XDC testnet added)
Hardhat or Truffle
Some XDC testnet tokens (available via faucet at apothem.network)
For this guide, we’ll use Hardhat.
mkdir paycheck-dapp
cd paycheck-dapp
npm init -y
npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers
Initialize Hardhat:
npx hardhat
Choose “Create a basic sample project”.
Step 2: Writing the Smart Contract
Our contract will let employers calculate employee net salary after deductions like tax or insurance. This is simplified but demonstrates the principle.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PaycheckCalculator {
struct Employee {
uint256 baseSalary;
uint256 taxRate; // percentage (e.g., 10 = 10%)
uint256 insurance;
}
mapping(address => Employee) public employees;
function setEmployee(
address _employee,
uint256 _baseSalary,
uint256 _taxRate,
uint256 _insurance
) public {
employees[_employee] = Employee(_baseSalary, _taxRate, _insurance);
}
function calculatePaycheck(address _employee) public view returns (uint256) {
Employee memory emp = employees[_employee];
uint256 tax = (emp.baseSalary * emp.taxRate) / 100;
uint256 netSalary = emp.baseSalary - tax - emp.insurance;
return netSalary;
}
}
Key Points:
- setEmployee() stores employee details on-chain.
- calculatePaycheck() computes the net salary dynamically.
- The data is transparent and immutable—great for auditability.
Step 3: Deploying on XDC Testnet
Add XDC’s Apothem Testnet to your Hardhat config. In hardhat.config.js:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.0",
networks: {
apothem: {
url: "https://rpc.apothem.network",
accounts: ["YOUR_PRIVATE_KEY_HERE"]
}
}
};
Replace YOUR_PRIVATE_KEY_HERE
with your MetaMask private key (never use your mainnet key for testing).
Now create a deployment script in scripts/deploy.js:
async function main() {
const PaycheckCalculator = await ethers.getContractFactory("PaycheckCalculator");
const calculator = await PaycheckCalculator.deploy();
await calculator.deployed();
console.log("Contract deployed to:", calculator.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
You should see your contract address printed. That’s your live Paycheck Calculator on XDC testnet.
Step 4: Interacting With the Contract
You can test it with Hardhat’s console or through a frontend app.
Example with Hardhat Console
npx hardhat console --network apothem
Inside the console:
const Contract = await ethers.getContractFactory("PaycheckCalculator");
const contract = await Contract.attach("YOUR_DEPLOYED_CONTRACT_ADDRESS");
// Set employee data
await contract.setEmployee("0xEmployeeWallet", 5000, 10, 200);
// Calculate paycheck
const net = await contract.calculatePaycheck("0xEmployeeWallet");
console.log("Net Salary:", net.toString());
You should see the calculated net salary returned.
Step 5: Adding a Simple Frontend
Most payroll DApps will have a UI. For a simple React frontend:
Install dependencies:
npx create-react-app paycheck-frontend
cd paycheck-frontend
npm install ethers
Example component (App.js):
import { useState } from "react";
import { ethers } from "ethers";
const contractAddress = "YOUR_DEPLOYED_CONTRACT_ADDRESS";
const abi = [
"function setEmployee(address _employee, uint256 _baseSalary, uint256 _taxRate, uint256 _insurance) public",
"function calculatePaycheck(address _employee) public view returns (uint256)"
];
function App() {
const [netPay, setNetPay] = useState("");
async function getPaycheck(employee) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(contractAddress, abi, provider);
const result = await contract.calculatePaycheck(employee);
setNetPay(result.toString());
}
return (
<div>
<h1>XDC Paycheck Calculator</h1>
<button onClick={() => getPaycheck("0xEmployeeWallet")}>
Get Net Salary
</button>
<p>Net Pay: {netPay}</p>
</div>
);
}
export default App;
Connect MetaMask to XDC Testnet, and your app can now query paycheck results.
Step 6: Next Steps and Improvements
This is just the beginning. A real-world payroll DApp could add:
- Automated payments: Integrate stablecoins or tokenized assets on XDC.
- Role-based access: Limit setEmployee() to employer accounts.
- Batch payroll: Calculate and distribute payments for multiple employees in one transaction.
- Gasless transactions: Explore XDC’s subnet staking for fee-less user experience.
- Integration with off-chain logic: Calculate complex payroll scenarios off-chain and only log results on-chain.
Why This Matters
Payroll is a classic use case where transparency and efficiency matter. By building it on XDC:
- Employers cut costs on processing fees.
- Employees gain trust in transparent, immutable salary calculations.
- Auditors can verify paycheck records without intermediaries.
This aligns with XDC’s broader mission of bringing blockchain to real-world finance and enterprise use cases.
Conclusion
We’ve walked through how to build and deploy a simple Paycheck Calculator DApp on the XDC Network using Solidity and Hardhat. With ultra-low fees, fast confirmations, and EVM compatibility, XDC is an ideal platform for payroll and other enterprise apps.
Whether you expand this into a full payroll management system or keep it as a demo, the core idea is clear: blockchain isn’t just for trading—it can power practical, everyday business processes.
Discussion (0)