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.
βββ src/
β βββ OnChainInvoice.sol # Main contract
β βββ RejectETH.sol # Mock contract to force transfer failure
βββ test/
β βββ OnChainInvoiceTest.t.sol # Full test suite using Foundry
enum Status { Pending, Paid, Cancelled }struct Invoice {
address issuer;
address client;
uint256 amount;
string description;
Status status;
}onlyClient(invoiceId)β Only the client can payonlyIssuerOrAdmin(invoiceId)β Only issuer or admin can cancel
function createInvoice(address client, uint256 amount, string calldata description)Creates a new invoice assigned to a client.
function payInvoice(uint256 invoiceId) external payableAllows the invoice's client to pay the exact amount in ETH.
function cancelInvoice(uint256 invoiceId) externalOnly issuer or admin can cancel invoices in Pending state.
function withdrawEther(uint256 amount) externalAllows issuers to withdraw collected ETH using the CEI pattern.
Handles edge cases like transfer failure using call.
receive() external payable {
revert("Use payInvoice");
}
fallback() external payable {
revert("Invalid function");
}Prevents accidental ETH transfers or undefined calls.
- Built with Foundry
- Covers all paths, including:
- Valid/invalid payments
- Cancellation logic (issuer/admin)
- Transfer failure simulation using
RejectETHcontract - Fallback and receive handling
- Fuzz testing of core logic (e.g. invoice creation) using random inputs
- β 100% line, function, and branch coverage
Licensed under the GNU Lesser General Public License v3.0 β see the LICENSE file.
Open to contributions, PRs, and improvements. This project serves as a showcase of best practices in minimal on-chain billing systems.