← Back to list

Creating a Smart Contract for Tip and Donation Systems on Morph

A Tip and Donation System is a system that allows users to support their favorite creators in a decentralized way

Defiboy · 2024-11-30 08:20 · 0 claps · 2.1 min read
#decentralization #ethereum #morph
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3

Creating a Smart Contract for Tip and Donation Systems on Morph

A Tip and Donation System is a system that allows users to support their favorite creators, charities, or even communities in a decentralized manner.

With Morph’s low gas fees, it’s the perfect chain to build solutions like this. Users won’t have to worry about the fees they’d need to pay for gas.

In this article, we’ll learn how to build a smart contract for tipping and donations.

Key Features of the Contract

  1. Support for Multiple Beneficiaries: Enable tipping/donating to different recipients.
  2. Customizable Messages: Allow users to include messages with their donations.
  3. Withdraw Functionality: Secure withdrawal mechanism for beneficiaries.
  4. Event Logging: Log donations for transparency and analytics.

Prerequisites

  1. Basic knowledge of Solidity.
  2. A working environment with Node.js, Hardhat installed.
  3. Access to the Morph.

Step 1: Setting up Development Environment

  1. Initialize a Hardhat Project
mkdir morph-tip
cd morph-tip
npx hardhat init
  1. Install some necessary dependencies
npm install @openzeppelin/contracts dotenv hardhat-ethers ethers
  1. Configure hardhat for Morph Holesky
require("@nomiclabs/hardhat-ethers");
require("dotenv").config();
module.exports = {
  networks: {
    "morph-holesky": {
      url: "https://rpc-quicknode-holesky.morphl2.io/",
      accounts: [`0x${process.env.PRIVATE_KEY}`],
    },
  },
  solidity: "0.8.19",
};

Step 2: Writing of the Crowdfunding Contract

Start by creating a new file contracts/tips.sol

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

contract TipAndDonate {
    // Event to log donations
    event DonationReceived(
        address indexed donor,
        address indexed beneficiary,
        uint256 amount,
        string message
    );

    // Event to log withdrawals
    event Withdrawn(address indexed beneficiary, uint256 amount);

    // Mapping to track balances of beneficiaries
    mapping(address => uint256) public balances;

    // Function to donate to a beneficiary
    function donate(address beneficiary, string memory message) external payable {
        require(msg.value > 0, "Donation must be greater than zero");

        // Update the beneficiary's balance
        balances[beneficiary] += msg.value;

        // Emit the donation event
        emit DonationReceived(msg.sender, beneficiary, msg.value, message);
    }

    // Function for beneficiaries to withdraw their funds
    function withdraw() external {
        uint256 amount = balances[msg.sender];
        require(amount > 0, "No funds available to withdraw");

        // Reset the beneficiary's balance to zero
        balances[msg.sender] = 0;

        // Transfer the funds
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Withdrawal failed");

        // Emit the withdrawal event
        emit Withdrawn(msg.sender, amount);
    }

    // Function to check the contract's balance
    function getContractBalance() external view returns (uint256) {
        return address(this).balance;
    }
}

Step 3: Compile and Deploy the Contract

  1. Compile the Contract:
npx hardhat compile
  1. Deploy the Contract:
async function main() {
    const TipAndDonate = await ethers.getContractFactory("TipAndDonate");
    const tipanddonate = await TipAndDonate.deploy();
console.log("TipAndDonate deployed to:", tipanddonate.address);
}
main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});

Deploy to Morph Holesky:

npx hardhat run scripts/deploy.js --network morph-holesky

Step 4: Frontend Integration

You can use libraries like web3.js ethers.js or viem to integrate the deployed contract to your frontend.

You can add features like:

  • Donation Form: Input for beneficiary address, message, and amount.
  • Withdrawal Dashboard: Display balances and allow withdrawal for beneficiaries.

Conclusion

With the steps above, you’ve successfully deployed a tip and donation contract. Now, you can integrate it into your project to create a full-featured Tipping dApp.


메타데이터
post_id
3ea2d8ec93ab
slug
creating-a-smart-contract-for-tip-and-donation-systems-on-morph-3ea2d8ec93ab
url
https://medium.com/@defiboy/creating-a-smart-contract-for-tip-and-donation-systems-on-morph-3ea2d8ec93ab
canonical_url
https://medium.com/@defiboy/creating-a-smart-contract-for-tip-and-donation-systems-on-morph-3ea2d8ec93ab
author_url
https://medium.com/@defiboy
status
ok
fetched_at
2026-07-21 22:45:11