https://github.com/faremeter/x-solana-settlement
https://github.com/faremeter/x-solana-settlement
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/faremeter/x-solana-settlement
- Owner: faremeter
- License: gpl-3.0
- Created: 2025-07-18T22:55:56.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-11-07T23:26:53.000Z (8 months ago)
- Last Synced: 2025-11-08T01:16:15.099Z (8 months ago)
- Language: TypeScript
- Size: 480 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Solana Payment-Gated API System
## Concept
This system creates a **Solana program** that manages micropayments for API access. Here's how it works:
1. **Payment Creation**: When a user wants to access a protected endpoint, they create a payment transaction that:
- Transfers SOL to the API provider
- Creates a **Payment account** (PDA) that stores payment metadata
- Uses a unique nonce to prevent duplicate payments
2. **Settlement with Race Condition Protection**: When the server processes the payment, it immediately settles it using an admin transaction that:
- **Uses a unique settle nonce** - this is crucial for preventing race conditions
- Multiple settlement attempts will fail because only one can successfully close the account
- **Returns the rent** from the Payment account back to the original payer
- Ensures each payment can only be settled once
3. **Rent Recovery**: The Payment account requires rent (~0.002 SOL) to exist on-chain. When settled, this rent is automatically returned to the original payer, so they only pay the actual service fee.
## 🔒 Race Condition Prevention
The **settle nonce** is key to preventing race conditions:
```rust
// Each settlement transaction uses a unique nonce
pub fn settle_payment(
ctx: Context,
original_payer: Pubkey,
payment_nonce: [u8; 32], // Original payment nonce
settle_nonce: [u8; 32], // UNIQUE settle nonce - prevents race
) -> Result<()>
```
Example createPayment tx: https://solscan.io/tx/4F1kPoz3L6JqWcUM53cGMZjvYb41TuzqZbAXrXDYQmF6HWNQB2M9WFBbXzdE56GFCNudieAKH5tfJzk6u51EMUF?cluster=devnet
Example settlePayment tx: https://solscan.io/tx/3UZNTyG7QsceTCBmJmTHE8Fiah6TeJpzopKzCcLNciXHrtTKUrZQub9322AWBuuxba2q1zsN4NXp6Pja6podNfrH?cluster=devnet