DEV Community

Cover image for How I Built an On-Chain Web3 Zakat Calculator using ethers.js
Adrian
Adrian

Posted on

How I Built an On-Chain Web3 Zakat Calculator using ethers.js

If you hold cryptocurrency, calculating your Zakat (the mandatory 2.5% charitable wealth tax in Islam) can be incredibly tedious.

You have to figure out your exact balance, check the current live price of the token, look up the current Nisab threshold (approx 85g of gold), and then do the math.

I got tired of doing this manually, so I built a 100% Client-Side Web3 Zakat Calculator.

Here’s a breakdown of how it works under the hood and how you can use it right now.


The Architecture (Zero-Trust)

Financial tools should be trustless. I didn't want to build a backend that logs user wallet addresses. So, everything runs in the browser using ethers.js and public APIs.

1. Fetching Live Prices (CoinGecko)

To know if you hit the Nisab threshold, we need the exact USD value of Ethereum. The CoinGecko API is perfect for this:

const cgResponse = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd');
const cgData = await cgResponse.json();
const ethPrice = cgData.ethereum.usd;
Enter fullscreen mode Exit fullscreen mode

2. Fetching the On-Chain Balance

Instead of requiring users to connect their MetaMask (which introduces security anxiety), I used Cloudflare's Public RPC to fetch the balance of any inputted address:

const provider = new ethers.providers.JsonRpcProvider('https://cloudflare-eth.com');
const balanceWei = await provider.getBalance(walletAddress);
const balanceEth = ethers.utils.formatEther(balanceWei);
const balanceUsd = parseFloat(balanceEth) * ethPrice;
Enter fullscreen mode Exit fullscreen mode

3. The Zakat Logic

If the USD value of the wallet is greater than or equal to the Nisab threshold (~$6,500), the app calculates the exact 2.5% owed in both USD and ETH.

const NISAB_USD = 6500;
if (balanceUsd >= NISAB_USD) {
    const zakatUsd = balanceUsd * 0.025;
    const zakatEth = zakatUsd / ethPrice;
    console.log(`Zakat Owed: ${zakatEth} ETH`);
}
Enter fullscreen mode Exit fullscreen mode

πŸš€ Use the Tool Live

I've deployed the tool for free on GitHub Pages. You can test it out with your own EVM address or any whale's address to see how much Zakat they owe!

πŸ‘‰ Live App: Web3 Zakat Calculator

πŸ’‘ Support the Project

If this tool saved you time calculating your obligations, consider supporting my open-source work:

LTC Tip Address: ltc1q82zr48l4sy2w5hax5hu0cdcxa5x0ak3a83xfle

Let me know if you want me to add support for Binance Smart Chain (BNB) or Polygon (MATIC) in the next update!

Top comments (0)