Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MicroHD/web3-subscriptions-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

1 Commit

Repository files navigation

Web3 Subscriptions - Modular Smart Contract Subscription System

A fully open-source smart contract system that lets ANY developer add subscription payments to their dApps — like "Stripe Subscriptions for Web3."

Overview

This project provides a production-ready, modular subscription payment system for Web3 applications. It supports both native ETH and ERC20 tokens, with features like gasless meta-transactions, L2 optimization, and comprehensive analytics.

Features

  • Core Subscription Management: Create, charge, cancel, and pause subscriptions
  • Multi-Token Support: Native ETH and ERC20 tokens
  • Pull Payment Model: Gas-efficient subscription charging
  • Admin Controls: Pause/resume subscriptions and contract operations
  • Event Logging: Comprehensive events for off-chain tracking
  • Gasless Transactions: Meta-transaction support via EIP-712
  • L2 Optimization: Gas-optimized contract for Layer 2 networks
  • Analytics: On-chain and off-chain analytics tracking
  • Example Frontend: React application with modern UI
  • Example Backend: Bun-based API with event listener

Architecture

┌─────────────────────────────────────────────────────────────┐
│ Web3 Subscriptions │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ Subscription │ │ L2Subscription │ │
│ │ Manager │ │ (Optimized) │ │
│ │ │ │ │ │
│ │ - Create │ │ - Batch Charges │ │
│ │ - Charge │ │ - Packed Storage │ │
│ │ - Cancel │ │ │ │
│ └──────────────────┘ └──────────────────┘ │
│ │ │ │
│ └────────────┬───────────────┘ │
│ │ │
│ ┌────────────▼────────────┐ │
│ │ MetaTransactions │ │
│ │ (Gasless) │ │
│ └─────────────────────────┘ │
│ │ │
│ ┌────────────▼────────────┐ │
│ │ SubscriptionAnalytics │ │
│ │ (Metrics) │ │
│ └─────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
 │ │
 │ │
┌────────▼────────┐ ┌────────▼────────┐
│ Frontend │ │ Backend │
│ (React) │ │ (Bun) │
│ │ │ │
│ - UI Components │ │ - REST API │
│ - Web3 Hooks │ │ - Event Listener│
│ - Wallet Connect│ │ - Analytics │
└─────────────────┘ └─────────────────┘

Quick Start

Prerequisites

  • Node.js 18+ or Bun
  • Hardhat
  • MetaMask or compatible wallet

Installation

  1. Clone and navigate to the project:
cd web3-subscriptions
  1. Install Hardhat dependencies:
cd hardhat
bun install
  1. Install Frontend dependencies:
cd ../frontend
bun install
  1. Install Backend dependencies:
cd ../backend
bun install

Configuration

Create a .env file in the project root:

# Network RPC URLs
GOERLI_RPC_URL=https://eth-goerli.g.alchemy.com/v2/YOUR_API_KEY
POLYGON_RPC_URL=https://polygon-mainnet.g.alchemy.com/v2/YOUR_API_KEY
# Deployer
PRIVATE_KEY=your_private_key_here
RECIPIENT_ADDRESS=0x... # Address to receive subscription payments
# Contract Addresses (set after deployment)
SUBSCRIPTION_MANAGER_ADDRESS=
META_TRANSACTIONS_ADDRESS=
L2_SUBSCRIPTION_ADDRESS=
ANALYTICS_ADDRESS=
# Etherscan API Keys
ETHERSCAN_API_KEY=your_etherscan_api_key
POLYGONSCAN_API_KEY=your_polygonscan_api_key

Deployment

  1. Deploy contracts:
cd hardhat
npx hardhat run scripts/deploy.ts --network goerli
  1. Verify contracts:
npx hardhat run scripts/verify.ts --network goerli

Or use the one-click deploy script:

./scripts/deploy-all.sh

Usage

Creating a Subscription

// Native ETH subscription
subscriptionManager.createSubscription(
 address(0), // Native ETH
 1 ether, // Amount
 3600 // Interval (1 hour)
);
// ERC20 token subscription
subscriptionManager.createSubscription(
 tokenAddress, // ERC20 token
 100 * 10**18, // Amount
 86400 // Interval (1 day)
);

Charging a Subscription

Anyone can charge a subscription when the interval has elapsed:

subscriptionManager.chargeSubscription(subscriptionId);

Cancelling a Subscription

subscriptionManager.cancelSubscription(subscriptionId);

Project Structure

web3-subscriptions/
├── contracts/ # Solidity smart contracts
│ ├── core/ # Core subscription contract
│ ├── extensions/ # Meta-transactions, L2, Analytics
│ └── mocks/ # Mock contracts for testing
├── hardhat/ # Hardhat configuration and scripts
│ ├── scripts/ # Deployment scripts
│ └── test/ # Contract tests
├── frontend/ # React frontend example
│ └── src/
│ ├── components/ # UI components
│ ├── hooks/ # React hooks
│ └── utils/ # Utilities
├── backend/ # Bun backend example
│ └── src/
│ ├── services/ # Business logic
│ └── api/ # REST API
├── docs/ # Documentation
└── scripts/ # Deployment scripts

Testing

Run contract tests:

cd hardhat
npx hardhat test

Security

See SECURITY.md for security considerations and best practices.

Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests.

License

MIT License - see LICENSE file for details.

Support

  • Open an issue on GitHub
  • Check the documentation in docs/
  • Review ARCHITECTURE.md for detailed architecture

Acknowledgments

  • OpenZeppelin for security libraries
  • Hardhat team for the development framework
  • Bun team for the runtime

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle によって変換されたページ (->オリジナル) /