code wiki / (root) / nx_fin_finance_gate.nx

nx_fin_finance_gate.nx source

↩ module page · 98 lines · 5937 B

1// nx_fin_finance_gate.nx -- R7 GATE: proves the financing-cost optimizer ranks REAL options by TRUE total 2// cost and SHIELDS against scams/predatory traps. Scenario: $20,000 high-interest debt, $600/month budget. 3// opt0 current credit cards 24% APR fee $0 licensed (the status quo to beat) 4// opt1 credit union loan 11% APR fee $100 licensed 5// opt2 CDFI / nonprofit loan 9% APR fee $50 licensed (the cheapest legit option) 6// opt3 balance-transfer card 15% APR fee $600 licensed 7// opt4 "international" offer 4% APR fee $0 UPFRONT-required + UNLICENSED (advance-fee SCAM bait) 8// opt5 payday / loan-shark 200% APR fee $0 licensed (predatory, above the 36% usury line) 9// The scam's advertised 4% computes the LOWEST raw cost -- it MUST NOT win. Exits 0 iff ALL pass. license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_money.nx" 12import "nx_debt_payoff.nx" 13import "nx_fin_finance.nx" 14 15func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16func g_putn(v: i64) -> i64 { 17 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 18 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 19 let d: *u8 = sys_mmap(24); var k: i64 = 0 20 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 21 var j: i64 = k - 1 22 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 23 return 0 24} 25func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 26 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 27 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 28 return 0 29} 30func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 } 31 32func main() -> i64 { 33 let st: *i64 = sys_mmap(16) as *i64 34 st[0] = 0; st[1] = 0 35 36 let usury: i64 = mny_rate_from_pct(36, 1) 37 let balance: i64 = 2000000 // $20,000.00 38 let payment: i64 = 60000 // $600.00/mo 39 40 let aprs: *i64 = sys_mmap(8 * 8) as *i64 41 let fees: *i64 = sys_mmap(8 * 8) as *i64 42 let upf: *i64 = sys_mmap(8 * 8) as *i64 43 let lic: *i64 = sys_mmap(8 * 8) as *i64 44 seti(aprs,0,mny_rate_from_pct(24,1)); seti(fees,0,0); seti(upf,0,0); seti(lic,0,1) 45 seti(aprs,1,mny_rate_from_pct(11,1)); seti(fees,1,10000); seti(upf,1,0); seti(lic,1,1) 46 seti(aprs,2,mny_rate_from_pct(9,1)); seti(fees,2,5000); seti(upf,2,0); seti(lic,2,1) 47 seti(aprs,3,mny_rate_from_pct(15,1)); seti(fees,3,60000); seti(upf,3,0); seti(lic,3,1) 48 seti(aprs,4,mny_rate_from_pct(4,1)); seti(fees,4,0); seti(upf,4,1); seti(lic,4,0) 49 seti(aprs,5,mny_rate_from_pct(200,1)); seti(fees,5,0); seti(upf,5,0); seti(lic,5,1) 50 let n: i64 = 6 51 52 // ---- exact anchor: 0% APR, $1200 at $100/mo, $30 fee -> 12 months, total cost = $30 ---- 53 let m0: *i64 = sys_mmap(16) as *i64 54 chk("exact: 0% $1200 @ $100/mo total cost = 3000c ($30 fee only)", fe_total_cost(120000, 10000, 0, 3000, m0), 3000, st) 55 chk(" payoff months = 12", m0[0], 12, st) 56 57 // ---- risk flags ---- 58 chk("opt0 (24% licensed) risk = clean", fe_risk(aprs[0], upf[0], lic[0], usury), 0, st) 59 chk("opt2 (9% CDFI) risk = clean", fe_risk(aprs[2], upf[2], lic[2], usury), 0, st) 60 chk("opt4 (intl upfront+unlicensed) risk = ADVANCE_FEE+UNLICENSED (5)", fe_risk(aprs[4], upf[4], lic[4], usury), 5, st) 61 chk("opt5 (200% payday) risk = PREDATORY (2)", fe_risk(aprs[5], upf[5], lic[5], usury), 2, st) 62 chk("opt4 is NOT safe (scam)", fe_is_safe(fe_risk(aprs[4], upf[4], lic[4], usury)), 0, st) 63 chk("opt2 IS safe", fe_is_safe(fe_risk(aprs[2], upf[2], lic[2], usury)), 1, st) 64 65 // ---- ranking ---- 66 let bi: *i64 = sys_mmap(16) as *i64 67 let bestcost: i64 = fe_rank_best(balance, payment, aprs, fees, upf, lic, n, usury, bi) 68 let mc: *i64 = sys_mmap(16) as *i64 69 let cur: i64 = fe_total_cost(balance, payment, aprs[0], fees[0], mc) 70 let scamraw: i64 = fe_total_cost(balance, payment, aprs[4], fees[4], mc) 71 g_puts(" [info] current(24%) true cost="); g_putn(cur); g_puts("c best(opt"); g_putn(bi[0]); g_puts(") true cost="); g_putn(bestcost); g_puts("c\n") 72 g_puts(" [info] scam advertised(4%) raw cost="); g_putn(scamraw); g_puts("c (would rank #1 -- EXCLUDED)\n") 73 g_puts(" [info] savings vs current="); g_putn(cur - bestcost); g_puts("c\n") 74 75 chk("best option = opt2 (CDFI 9%, cheapest legit)", bi[0], 2, st) 76 chk("best is NOT the scam opt4", fe_is_safe(fe_risk(aprs[bi[0]], upf[bi[0]], lic[bi[0]], usury)), 1, st) 77 // SCAM-SHIELD liar-kill: the scam's raw cost is LOWER than the chosen best, yet it did NOT win. 78 var scam_would_win: i64 = 0 79 if scamraw < bestcost { scam_would_win = 1 } 80 chk("scam's bait rate WOULD win on raw cost...", scam_would_win, 1, st) 81 chk("...but ranker EXCLUDED it (best != 4)", bi[0], 2, st) 82 // switching to the best legit option saves money vs the current high rate 83 var saves: i64 = 0 84 if (cur - bestcost) > 0 { saves = 1 } 85 chk("switching to best legit option SAVES vs current", saves, 1, st) 86 87 // SURVIVAL RUNWAY (job-loss resilience): how long savings cover bills if income stops 88 chk("runway: $500 savings / $2350 bills = 0 months (sub-month)", fe_runway_months(50000, 235000), 0, st) 89 chk("runway: $500 savings / $2350 bills = 6 days", fe_runway_days(50000, 235000), 6, st) 90 chk("runway: $7,050 savings / $2350 bills = 3 months", fe_runway_months(705000, 235000), 3, st) 91 chk("runway: $7,050 savings / $2350 bills = 90 days", fe_runway_days(705000, 235000), 90, st) 92 chk("runway: no obligations -> -1 (indefinite)", fe_runway_months(50000, 0), 0 - 1, st) 93 94 g_puts("nx_fin_finance_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 95 if st[1] == 0 { g_puts("R7 nx_fin_finance: GREEN\n"); return 0 } 96 g_puts("R7 nx_fin_finance: RED\n") 97 return 1 98}