Skip to content

🧾 OnChainInvoice – Minimal, gas-efficient invoice management smart contract in Solidity. Includes secure ETH payments, invoice cancellation, and withdrawals with full Foundry test coverage (100%) and fuzz testing. Clean CEI pattern, access control, and defensive receive/fallback logic.

License

Notifications You must be signed in to change notification settings

ralphmcralph/OnChainInvoice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧾 OnChainInvoice – Simple Invoice Management System in Solidity

Solidity License Tested Coverage


πŸ“Œ Description

OnChainInvoice is a minimal and gas-efficient Solidity smart contract that enables users to:

  • Create invoices with amount, client, and description
  • Accept payments securely via ETH
  • Cancel invoices (by issuer or admin)
  • Withdraw collected funds safely

Built with Foundry, the project showcases clean separation of logic, CEI pattern, and robust access control β€” with full 100% test coverage (lines, statements, functions, branches) and fuzzing included.


πŸ“ Structure

β”œβ”€β”€ src/
β”‚   └── OnChainInvoice.sol         # Main contract
β”‚   └── RejectETH.sol              # Mock contract to force transfer failure
β”œβ”€β”€ test/
β”‚   └── OnChainInvoiceTest.t.sol   # Full test suite using Foundry

🧱 Contract Overview

Enum: Status

enum Status { Pending, Paid, Cancelled }

Struct: Invoice

struct Invoice {
    address issuer;
    address client;
    uint256 amount;
    string description;
    Status status;
}

πŸ” Modifiers

  • onlyClient(invoiceId) – Only the client can pay
  • onlyIssuerOrAdmin(invoiceId) – Only issuer or admin can cancel

πŸš€ Functions

βœ… Create Invoice

function createInvoice(address client, uint256 amount, string calldata description)

Creates a new invoice assigned to a client.


πŸ’Έ Pay Invoice

function payInvoice(uint256 invoiceId) external payable

Allows the invoice's client to pay the exact amount in ETH.


❌ Cancel Invoice

function cancelInvoice(uint256 invoiceId) external

Only issuer or admin can cancel invoices in Pending state.


πŸ’° Withdraw Funds

function withdrawEther(uint256 amount) external

Allows issuers to withdraw collected ETH using the CEI pattern. Handles edge cases like transfer failure using call.


⚠️ Rejection Handlers

receive() external payable {
    revert("Use payInvoice");
}

fallback() external payable {
    revert("Invalid function");
}

Prevents accidental ETH transfers or undefined calls.


πŸ§ͺ Testing

  • Built with Foundry
  • Covers all paths, including:
    • Valid/invalid payments
    • Cancellation logic (issuer/admin)
    • Transfer failure simulation using RejectETH contract
    • Fallback and receive handling
    • Fuzz testing of core logic (e.g. invoice creation) using random inputs
  • βœ… 100% line, function, and branch coverage

πŸ“„ License

Licensed under the GNU Lesser General Public License v3.0 – see the LICENSE file.


πŸ™‹β€β™‚οΈ Author & Contributions

Open to contributions, PRs, and improvements. This project serves as a showcase of best practices in minimal on-chain billing systems.

About

🧾 OnChainInvoice – Minimal, gas-efficient invoice management smart contract in Solidity. Includes secure ETH payments, invoice cancellation, and withdrawals with full Foundry test coverage (100%) and fuzz testing. Clean CEI pattern, access control, and defensive receive/fallback logic.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published