@anuragchvn-blip/mandatekit
Version:
Production-ready Web3 autopay SDK for crypto-based recurring payments using EIP-712 mandates
242 lines (179 loc) ⢠8.15 kB
Markdown
# š MandateKit
**Production-ready Web3 autopay SDK for crypto-based recurring payments on Polygon**
MandateKit enables developers to easily create and manage recurring payments using EIP-712 mandates, relayer automation, pull-based billing, cadence enforcement, and secure non-custodial payments.
[](https://github.com/anuragchvn-blip/MandateKit/packages)
[](https://opensource.org/licenses/MIT)
## š Live Deployment
**Smart Contract Address (Polygon Mainnet):**
```
0xd9145CCE52D386f254917e481eB44e9943F39138
```
View on [PolygonScan](https://polygonscan.com/address/0xd9145CCE52D386f254917e481eB44e9943F39138)
## ⨠Features
- š **EIP-712 Typed Signatures** - Secure, human-readable mandate signing (no phishing)
- š¤ **Relayer Automation** - Automated pull-based payment execution with fee incentives
- ā° **Cadence Enforcement** - Flexible scheduling (daily, weekly, monthly, yearly, custom)
- š **Replay Protection** - Built-in nonce and timestamp validation
- š”ļø **Security Hardened** - Checks-Effects-Interactions pattern, ReentrancyGuard
- š° **Non-Custodial** - Users always maintain control of their funds
- š§© **Modular & Tree-shakeable** - Import only what you need
- šŖ **TypeScript First** - Full type safety and IntelliSense
- š **Adapter Support** - Permit2, vaults, account abstraction ready
- š¦ **Modern Stack** - Built with Viem v2, ES2020+, OpenZeppelin v5
## š¦ Installation
### From GitHub Packages
```bash
# Configure npm to use GitHub Packages
echo "@anuragchvn-blip:registry=https://npm.pkg.github.com" >> .npmrc
# Install the package
npm install @anuragchvn-blip/mandatekit viem
```
### From npm (Coming Soon)
```bash
npm install @anuragchvn-blip/mandatekit viem
```
## š Quick Start
### Backend (Node.js)
```typescript
import { createMandateClient } from '@anuragchvn-blip/mandatekit/client';
import { privateKeyToAccount } from 'viem/accounts';
import { polygon } from 'viem/chains';
const account = privateKeyToAccount('0x...');
const client = createMandateClient({
account,
chain: polygon
});
// Create a mandate for recurring USDC payments
const mandate = await client.signMandate({
subscriber: '0x123...', // Payer address
token: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC on Polygon
amount: '10000000', // 10 USDC (6 decimals)
cadence: { interval: 'monthly', count: 1 },
recipient: '0x456...', // Recipient address
validAfter: Math.floor(Date.now() / 1000),
validBefore: Math.floor(Date.now() / 1000) + 31536000, // 1 year
maxPayments: 12, // Optional: auto-cancel after 12 payments
metadata: 'Netflix subscription', // Optional
});
console.log('Mandate ID:', mandate.mandateId);
console.log('Signature:', mandate.signature);
```
### Frontend (Browser with MetaMask)
```typescript
import { createMandateClient } from '@anuragchvn-blip/mandatekit/client';
import { createWalletClient, custom } from 'viem';
import { polygon } from 'viem/chains';
const walletClient = createWalletClient({
chain: polygon,
transport: custom(window.ethereum),
});
const [address] = await walletClient.getAddresses();
const client = createMandateClient({
walletClient,
address
});
// User signs mandate in MetaMask
const mandate = await client.signMandate({
subscriber: address,
token: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC
amount: '10000000', // 10 USDC
cadence: { interval: 'monthly', count: 1 },
recipient: '0x456...',
validAfter: Math.floor(Date.now() / 1000),
validBefore: Math.floor(Date.now() / 1000) + 31536000,
});
// Register mandate on-chain
const txHash = await client.registerMandate(mandate);
console.log('Registered:', txHash);
```
### Relayer (Automated Execution)
```typescript
import { createRelayerClient } from '@anuragchvn-blip/mandatekit/relayer';
import { privateKeyToAccount } from 'viem/accounts';
import { polygon } from 'viem/chains';
const relayerAccount = privateKeyToAccount('0x...');
const relayer = createRelayerClient({
account: relayerAccount,
chain: polygon,
registryAddress: '0xd9145CCE52D386f254917e481eB44e9943F39138',
pollInterval: 60000, // Check every minute
});
// Schedule mandate for automated execution
await relayer.scheduleMandate(mandate);
// Relayer earns 0.5% fee for executing payments
console.log('Relayer monitoring active mandates...');
```
## š Documentation
- **[API Reference](./docs/API.md)** - Complete API documentation
- **[Examples](./examples/)** - Full working examples
- **[Smart Contract](./contracts/)** - Solidity source code
- **[Security](./SECURITY.md)** - Security practices and audit info
## šÆ Use Cases
- š³ **Subscription Services** - Netflix-style recurring billing in crypto
- š¼ **Payroll** - Pay employees/contractors on a schedule
- š **Dollar-Cost Averaging (DCA)** - Automated periodic token purchases
- š **Rent/Bills** - Recurring payments for utilities, rent, etc.
- š® **Gaming** - Season passes and recurring in-game purchases
- š± **SaaS** - Decentralized software subscriptions
## šļø Architecture
```text
MandateKit
āāā client/ ā Mandate signing & verification (EIP-712)
āāā relayer/ ā Automated execution engine
āāā contracts/ ā Smart contract ABIs & addresses
āāā utils/ ā Cryptographic & validation utilities
āāā adapters/ ā Protocol integrations (Permit2, vaults, AA)
```
## š Security
### Smart Contract Security
- ā
**Checks-Effects-Interactions Pattern** - Prevents reentrancy attacks
- ā
**ReentrancyGuard** - OpenZeppelin's reentrancy protection
- ā
**SafeERC20** - Secure token transfers with proper error handling
- ā
**Nonce-based Replay Protection** - Each mandate uses unique nonce
- ā
**Timestamp Validation** - Enforces validAfter/validBefore windows
- ā
**Cadence Enforcement** - Contract-level payment scheduling
- ā
**Signature Verification** - EIP-712 typed data signatures
- ā
**Owner Access Control** - Only owner can modify critical parameters
### SDK Security
- ā
**EIP-712 Typed Data** - Human-readable signatures prevent phishing
- ā
**Input Validation** - Comprehensive validation before signing
- ā
**TypeScript Type Safety** - Compile-time error prevention
- ā
**No Private Key Storage** - Uses viem's secure account handling
### Deployed Contract
**Polygon Mainnet:** `0xd9145CCE52D386f254917e481eB44e9943F39138`
- Owner: `0x5B38Da6a701c568545dCfcB03FcB875f56beddC4`
- Fee Collector: `0x2913411D27f5d6716590F952b50088779Ae4a699`
- Relayer Fee: 0.5% (50 basis points)
- Max Fee Cap: 5% (500 basis points)
## š ļø Development
### Setup
```bash
# Clone repository
git clone https://github.com/anuragchvn-blip/MandateKit.git
cd MandateKit
# Install dependencies
npm install
# Build SDK
npm run build
# Run examples
npm run example:backend
npm run example:relayer
```
### Publishing to GitHub Packages
```bash
# Authenticate with GitHub
npm login --registry=https://npm.pkg.github.com
# Build and publish
npm run build
npm publish
```
## š¤ Contributing
Contributions are welcome! Please read our [Contributing Guide](./CONTRIBUTING.md) first.
## š License
MIT Ā© 2025 MandateKit
## š Links
- **Repository:** [github.com/anuragchvn-blip/MandateKit](https://github.com/anuragchvn-blip/MandateKit)
- **Issues:** [github.com/anuragchvn-blip/MandateKit/issues](https://github.com/anuragchvn-blip/MandateKit/issues)
- **Polygon Contract:** [0xd9145CCE52D386f254917e481eB44e9943F39138](https://polygonscan.com/address/0xd9145CCE52D386f254917e481eB44e9943F39138)
## ā ļø Disclaimer
This software is provided "as is", without warranty of any kind. Use at your own risk. Always test thoroughly before using in production with real funds.