diff options
| author | Your Name <you@example.com> | 2026-05-17 04:37:15 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 04:37:15 +0530 |
| commit | cb4bd7d7c10cadcb43f82c09b13ffed744e541f7 (patch) | |
| tree | 1f01c31083e9252b7f41e89ba201373d6606a47d /tests/unit/test_lightning_payout.c | |
| parent | 78dd599277b8e8b2ddc39a4ae710ec91d737272e (diff) | |
Phase 5: Lightning auto-payout with LNURL-pay and NUT-05 melt
- New lnurl_pay.c/h: LNURL-pay protocol (GET .well-known/lnurlp + callback)
- New lightning_payout.c/h: threshold-based auto-payout with multi-recipient split
- Extended nucula_wallet bridge with nucula_wallet_melt() (NUT-05)
- Config: payout section with multi-mint, multi-recipient, fee_tolerance
- Default: enabled, TollGate@coinos.io, min_payout=128, min_balance=64
- 18 new unit tests (all passing), 134 total
Diffstat (limited to 'tests/unit/test_lightning_payout.c')
| -rw-r--r-- | tests/unit/test_lightning_payout.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/unit/test_lightning_payout.c b/tests/unit/test_lightning_payout.c new file mode 100644 index 0000000..8501eb9 --- /dev/null +++ b/tests/unit/test_lightning_payout.c | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/lightning_payout.h" | ||
| 3 | #include "../../main/config.h" | ||
| 4 | #include <string.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <math.h> | ||
| 7 | |||
| 8 | static void test_payout_calculation(void) | ||
| 9 | { | ||
| 10 | printf("\n--- Payout pool calculation ---\n"); | ||
| 11 | { | ||
| 12 | uint64_t balance = 500; | ||
| 13 | uint64_t min_balance = 64; | ||
| 14 | uint64_t min_payout_amount = 128; | ||
| 15 | |||
| 16 | ASSERT(balance >= min_payout_amount, "500 >= 128 triggers payout"); | ||
| 17 | |||
| 18 | uint64_t pool = balance - min_balance; | ||
| 19 | ASSERT_EQ_INT(436, (int)pool, "pool = 500 - 64 = 436"); | ||
| 20 | } | ||
| 21 | |||
| 22 | printf("\n--- Payout below threshold ---\n"); | ||
| 23 | { | ||
| 24 | uint64_t balance = 100; | ||
| 25 | uint64_t min_payout_amount = 128; | ||
| 26 | |||
| 27 | ASSERT(balance < min_payout_amount, "100 < 128, no payout"); | ||
| 28 | } | ||
| 29 | |||
| 30 | printf("\n--- Multi-recipient split ---\n"); | ||
| 31 | { | ||
| 32 | uint64_t pool = 436; | ||
| 33 | double factors[] = {0.79, 0.21}; | ||
| 34 | const char *names[] = {"owner", "developer"}; | ||
| 35 | |||
| 36 | uint64_t total = 0; | ||
| 37 | for (int i = 0; i < 2; i++) { | ||
| 38 | uint64_t share = (uint64_t)round((double)pool * factors[i]); | ||
| 39 | printf(" %s: factor=%.2f share=%llu\n", names[i], factors[i], (unsigned long long)share); | ||
| 40 | total += share; | ||
| 41 | } | ||
| 42 | ASSERT_EQ_INT(436, (int)total, "79/21 split sums to pool"); | ||
| 43 | } | ||
| 44 | |||
| 45 | printf("\n--- Single recipient 100%% ---\n"); | ||
| 46 | { | ||
| 47 | uint64_t pool = 436; | ||
| 48 | double factor = 1.0; | ||
| 49 | uint64_t share = (uint64_t)round((double)pool * factor); | ||
| 50 | ASSERT_EQ_INT(436, (int)share, "1.0 factor = full pool"); | ||
| 51 | } | ||
| 52 | |||
| 53 | printf("\n--- Fee tolerance calculation ---\n"); | ||
| 54 | { | ||
| 55 | uint64_t share = 344; | ||
| 56 | uint64_t fee_pct = 10; | ||
| 57 | uint64_t max_cost = share + (share * fee_pct / 100); | ||
| 58 | ASSERT_EQ_INT(378, (int)max_cost, "344 + 10% = 378"); | ||
| 59 | } | ||
| 60 | |||
| 61 | printf("\n--- Zero pool (balance == reserve) ---\n"); | ||
| 62 | { | ||
| 63 | uint64_t balance = 64; | ||
| 64 | uint64_t min_balance = 64; | ||
| 65 | uint64_t pool = balance - min_balance; | ||
| 66 | ASSERT_EQ_INT(0, (int)pool, "no payout when balance == reserve"); | ||
| 67 | } | ||
| 68 | |||
| 69 | printf("\n--- Payout config defaults ---\n"); | ||
| 70 | { | ||
| 71 | payout_config_t cfg; | ||
| 72 | memset(&cfg, 0, sizeof(cfg)); | ||
| 73 | cfg.enabled = true; | ||
| 74 | cfg.mint_count = 1; | ||
| 75 | strncpy(cfg.mints[0].url, "https://testnut.cashu.space", sizeof(cfg.mints[0].url) - 1); | ||
| 76 | cfg.mints[0].min_balance = 64; | ||
| 77 | cfg.mints[0].min_payout_amount = 128; | ||
| 78 | cfg.recipient_count = 1; | ||
| 79 | strncpy(cfg.recipients[0].lightning_address, "TollGate@coinos.io", | ||
| 80 | sizeof(cfg.recipients[0].lightning_address) - 1); | ||
| 81 | cfg.recipients[0].factor = 1.0; | ||
| 82 | cfg.fee_tolerance_pct = 10; | ||
| 83 | cfg.check_interval_s = 60; | ||
| 84 | |||
| 85 | ASSERT(cfg.enabled, "payout enabled"); | ||
| 86 | ASSERT_EQ_INT(1, cfg.mint_count, "1 mint"); | ||
| 87 | ASSERT_EQ_INT(1, cfg.recipient_count, "1 recipient"); | ||
| 88 | ASSERT_EQ_STR("TollGate@coinos.io", cfg.recipients[0].lightning_address, "default LNURL"); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | int main(void) | ||
| 93 | { | ||
| 94 | printf("=== test_lightning_payout ===\n"); | ||
| 95 | test_payout_calculation(); | ||
| 96 | TEST_SUMMARY(); | ||
| 97 | } | ||