Giter Site home page Giter Site logo

donable-solidity's Introduction

Donable-Solidity

When writing a solidity contract that provides a service you might want to add a donation feature that would allow your users to donate a certain amount of Eth to express their gratitude for your effort. This repository contains a Solidity smart contract (Donable.sol) that you can inherit from to provide your contract with such feature. A DemoDonable.sol as an example usage contract is also provided. An Ownable.sol contract that provides ownership functionality is inherited by Donable.sol. There is also a Truffle test folder with files for testing the contracts in a Truffle project.

Contracts

Donable uml

Ownable.sol

This contract provides basic ownership functionality. It allows you to set and change the owner of a contract. The owner has certain privileges, and only the owner can perform certain actions within the contract.

Functions and Events

  • constructor(): Initializes the contract with the deployer as the initial owner.
  • changeOwner(address newOwner): Allows the current owner to change the owner to a new address.
  • getOwner(): Retrieves the current owner's address.
  • requireOwner modifier: Used to restrict access to certain functions to the contract owner.
  • OwnerSet event: Emitted when ownership is changed.

Donable.sol

This contract extends the functionality of Ownable.sol and introduces the ability to receive and track donations. Users can donate Ether to the contract, either directly or by sending more Eth than needed to a child contract. Donable.sol will keep track of the donated amount.

Functions and Events

  • donate(): Allows users to donate Ether directly to the contract.
  • withdrawDonations(): Allows the owner to withdraw all stored donations.
  • donateAmount(uint donation): Internal function to allow a child contract to keep track of a donation. The child contract specifies the donated amount.
  • keepTheChange(uint spentAmount): Internal function to allow a child contract to keep track of a donation. The child contract specifies the cost of the service and Donable.sol calculate what’s been donated (the rest).
  • DonationMade event: Emitted when a donation is made.
  • donationPot variable: Stores the total amount of Ether donated to the contract.

DemoDonable.sol

This contract is a demonstration of how to use Donable.sol. It inherits from Donable.sol and showcases two functions that simulate charging users and handling donations.

Functions

  • doSomethingFor1mEtherKeepTheChange(): Simulates charging the user for 1 milliEther and keeping the rest as a donation. It uses the keepTheChange(uint spentAmount) function of Donable.sol.
  • doSomethingFor1mEtherAndDonateTheRest(): Simulates charging the user for 1 milliEther and donating the rest. It uses the donateAmount(uint donation) function of Donable.sol.
  • withdrawAllFunds(): Allows the owner to withdraw contract funds and the donation pot. This would not update the donationPot variable and is an example of misuse. However, Donable.sol should adapt gracefully.
  • withdrawContractFunds(): Allows the owner to withdraw contract funds, without affecting the donation pot.
  • contractFunds variable: Tracks the amount of Ether charged to users.

Running Tests

To run the tests for these contracts you need a Truffle project set up. Please read the README.md file of the Truffle test folder for further details.

License

These smart contracts are provided under the MIT License.

donable-solidity's People

Contributors

salvadorjesus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.