- Overview
- Contract Details
- Functions
- Constants and Error Codes
- Data Structures
- Usage Examples
- Security Considerations
- Testing
- Deployment
- Contributing
- License
This Clarity smart contract implements an invoicing system on the Stacks blockchain. It allows users to create and manage two types of invoices:
- Standard Invoice: Can be paid in full only by a specific user.
- Flexible Invoice: Can be paid in parts by multiple users.
The contract provides functionality for creating invoices, paying invoices, and querying invoice details.
- Name: Invoicing Contract
- Version: 1.0.0
- Description: Smart contract to create and manage invoices
- Contract Owner:
ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
Creates a new invoice.
Parameters:
payer: Optional principal (required for standard invoices)amount: uint (invoice amount)invoice-type: string-ascii 20 ("standard" or "flexible")
Returns:
- OK response with invoice details or an error
Pays an existing invoice.
Parameters:
invoice-id: uintpayment-amount: Optional uint (required for flexible invoices)
Returns:
- OK response with payment details or an error
Retrieves invoice details.
Parameters:
invoice-id: uint
Returns:
- Invoice details or none if not found
get-principal-balance: Gets the STX balance of an accountget-current-time: Gets the current block timegenerate-invoice-id: Generates a unique invoice ID
MAX_INVOICE_AMOUNT: u10000000000- Error codes (u1001 to u1009) for various error conditions
(define-map invoices
{ invoice-id: uint }
{
issuer: principal,
payer: (optional principal),
paid-amount: uint,
amount: uint,
paid: bool,
invoice-type: (string-ascii 20)
}
)last-invoice-id: Stores the last generated invoice IDinvoice-counter: Counts the number of invoices created
(contract-call? .invoice create-invoice (some 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM) u100 "standard")(contract-call? .invoice pay-invoice u1 none)(contract-call? .invoice create-invoice none u200 "flexible")(contract-call? .invoice pay-invoice u2 (some u50))- Only the contract owner can create invoices
- Standard invoices can only be paid by the specified payer
- Flexible invoices can be paid by anyone, but the total paid amount cannot exceed the invoice amount
- The contract prevents self-payment for standard invoices
To ensure the reliability and correctness of the contract, implement comprehensive unit tests covering all functions and edge cases. Use the Clarinet testing framework for Clarity smart contracts.
- Ensure you have the Stacks CLI and Clarinet installed
- Deploy the contract to the desired Stacks network (testnet or mainnet) using the Stacks CLI
- Verify the contract deployment by checking the transaction on the Stacks Explorer
Contributions to improve the contract are welcome. Please follow these steps:
- Fork the repository
- Create a new branch for your feature
- Commit your changes
- Push to the branch
- Create a new Pull Request