dubs-server 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (304) hide show
  1. package/.claude/settings.local.json +280 -0
  2. package/CLAUDE.md +46 -0
  3. package/CONNECT4_PRODUCTION_DEPLOY.md +155 -0
  4. package/CURRENT_SESSION.md +171 -0
  5. package/CURRENT_SESSION_DRAW.md +516 -0
  6. package/MARCH_MADNESS_SURVIVOR.md +254 -0
  7. package/PANDA.md +166 -0
  8. package/Procfile +4 -0
  9. package/README.md +476 -0
  10. package/controllers/livescoresController.js +376 -0
  11. package/controllers/pickemController.js +554 -0
  12. package/controllers/survivorAdminController.js +887 -0
  13. package/controllers/survivorController.js +623 -0
  14. package/cron/oracleMonitor.js +77 -0
  15. package/cron/pickemOracleMonitor.js +73 -0
  16. package/data/jackpot-history.json +952 -0
  17. package/data/ncaaTeams.js +406 -0
  18. package/documentation/API_SECURITY_GUIDE.md +327 -0
  19. package/documentation/ARCADE_API.md +593 -0
  20. package/documentation/ARCADE_IMPLEMENTATION_SUMMARY.md +399 -0
  21. package/documentation/ARCADE_QUICKSTART.md +242 -0
  22. package/documentation/AUTOMATIC_MODE_ORACLE.md +321 -0
  23. package/documentation/BUG_FIX_COHORT_DATE_DISPLAY.md +171 -0
  24. package/documentation/CLAIM_MIGRATION_INSTRUCTIONS.md +52 -0
  25. package/documentation/CLAIM_STATUS_FIX.md +67 -0
  26. package/documentation/CLI_TOOL_GUIDE.md +372 -0
  27. package/documentation/COHORT_RETENTION_ANALYSIS.md +295 -0
  28. package/documentation/COHORT_RETENTION_IMPLEMENTATION_COMPLETE.md +461 -0
  29. package/documentation/COHORT_RETENTION_SUMMARY.md +204 -0
  30. package/documentation/COMPLETE_PROJECT_SUMMARY.md +490 -0
  31. package/documentation/DATABASE_QUERIES.md +269 -0
  32. package/documentation/DATABASE_RETENTION_POLICY.md +390 -0
  33. package/documentation/DATABASE_SETUP_GUIDE.md +361 -0
  34. package/documentation/DATABASE_SETUP_SUMMARY.md +247 -0
  35. package/documentation/DEMO_API_CURL_COMMANDS.md +656 -0
  36. package/documentation/DEPLOYMENT_SUMMARY.txt +100 -0
  37. package/documentation/DUPLICATE_NOTIFICATIONS_FIXED.md +201 -0
  38. package/documentation/EXCHANGE_RATES_INTEGRATION.md +371 -0
  39. package/documentation/FINAL_API_PROTECTION_TABLE.md +175 -0
  40. package/documentation/GAME_START_NOTIFICATIONS_DEPLOYMENT.md +256 -0
  41. package/documentation/GAME_START_NOTIFICATIONS_INTEGRATION.md +275 -0
  42. package/documentation/HEROKU_DEPLOYMENT.md +134 -0
  43. package/documentation/HEROKU_SCHEDULER_SETUP.md +271 -0
  44. package/documentation/JACKPOT_API.md +521 -0
  45. package/documentation/JACKPOT_DEPLOYMENT_GUIDE.md +362 -0
  46. package/documentation/JWT_IMPLEMENTATION_SUMMARY.md +373 -0
  47. package/documentation/JWT_QUICK_SETUP.md +268 -0
  48. package/documentation/JWT_TESTING_GUIDE.md +404 -0
  49. package/documentation/KEEPER_RECOVERY_GUIDE.md +381 -0
  50. package/documentation/KEEPER_SETUP.md +206 -0
  51. package/documentation/KEEPER_STATE_MACHINE.md +423 -0
  52. package/documentation/LATEST_PRODUCTION_SETUP.md +387 -0
  53. package/documentation/LOCAL_VOTING_TEST.md +279 -0
  54. package/documentation/ORACLE_FIXES_SUMMARY.md +188 -0
  55. package/documentation/ORACLE_POSTGRESQL_UPDATE.md +202 -0
  56. package/documentation/PAYMENT_DEPLOYMENT.md +209 -0
  57. package/documentation/PNL_TRACKING_SETUP.md +189 -0
  58. package/documentation/PREVENTING_LOCKUP_ERRORS.md +472 -0
  59. package/documentation/PRODUCTION_READY_SUMMARY.md +227 -0
  60. package/documentation/PUBLIC_VS_PRIVATE_ENDPOINTS.md +278 -0
  61. package/documentation/QUICK_AUTH_SETUP.md +99 -0
  62. package/documentation/QUICK_DEPLOY.md +224 -0
  63. package/documentation/QUICK_FIX.md +114 -0
  64. package/documentation/QUICK_START.md +152 -0
  65. package/documentation/REFEREE_MODE_GUIDE.md +392 -0
  66. package/documentation/RETENTION_CORE_ACTION_UPDATE.md +313 -0
  67. package/documentation/RETENTION_UPDATE_SUMMARY.md +108 -0
  68. package/documentation/RUN_MIGRATION_NOW.md +39 -0
  69. package/documentation/SCRIPTS_UPDATE_SUMMARY.md +251 -0
  70. package/documentation/SETUP_GUIDE.md +184 -0
  71. package/documentation/STATE_MACHINE_IMPLEMENTATION.md +250 -0
  72. package/documentation/TELEGRAM_NOTIFICATIONS_DIAGNOSIS.md +361 -0
  73. package/documentation/UNIFIED_ARCHITECTURE.md +231 -0
  74. package/documentation/VOTING_DEPLOYMENT_SUMMARY.md +392 -0
  75. package/documentation/WEBSOCKET_ARCHITECTURE.md +881 -0
  76. package/documentation/WHAT_WE_BUILT_TODAY.md +369 -0
  77. package/documentation/latest/LATEST_PRODUCTION_SETUP.md +865 -0
  78. package/ecosystem.config.js +65 -0
  79. package/env.template +125 -0
  80. package/middleware/apiKeyAuth.js +136 -0
  81. package/middleware/authenticate.js +214 -0
  82. package/middleware/developerUserAuth.js +76 -0
  83. package/middleware/socketAuth.js +69 -0
  84. package/package.json +49 -0
  85. package/postman/Dubs-API-v1-With-Voting.postman_collection.json +555 -0
  86. package/postman/Dubs-API-v1.postman_collection.json +205 -0
  87. package/postman/Dubs_Developer_API.postman_collection.json +662 -0
  88. package/postman/QUICKSTART.md +118 -0
  89. package/postman/QUICK_REFERENCE.md +246 -0
  90. package/postman/README.md +71 -0
  91. package/postman/VOTING_API_GUIDE.md +426 -0
  92. package/refactor/Animations.md +148 -0
  93. package/refactor/Chat.md +252 -0
  94. package/routes/actionsRoutes.js +699 -0
  95. package/routes/adminRoutes.js +370 -0
  96. package/routes/analyticsRoutes.js +1262 -0
  97. package/routes/arcadeRoutes.js +557 -0
  98. package/routes/authRoutes.js +2310 -0
  99. package/routes/avatarRoutes.js +85 -0
  100. package/routes/botRoutes.js +211 -0
  101. package/routes/chatRoutes.js +377 -0
  102. package/routes/cryptoPriceRoutes.js +105 -0
  103. package/routes/developerRoutes.js +4201 -0
  104. package/routes/deviceRoutes.js +214 -0
  105. package/routes/dmRoutes.js +167 -0
  106. package/routes/esportsRoutes.js +806 -0
  107. package/routes/exchangeRateRoutes.js +233 -0
  108. package/routes/gamesRoutes.js +3028 -0
  109. package/routes/jackpotRoutes.js +754 -0
  110. package/routes/keeperMonitoringRoutes.js +156 -0
  111. package/routes/keeperWebhookRoutes.js +466 -0
  112. package/routes/livescoresRoutes.js +31 -0
  113. package/routes/pickemAdminRoutes.js +199 -0
  114. package/routes/pickemRoutes.js +231 -0
  115. package/routes/playerStatsRoutes.js +147 -0
  116. package/routes/portfolioRoutes.js +217 -0
  117. package/routes/promoRoutes.js +418 -0
  118. package/routes/referralEarningsRoutes.js +392 -0
  119. package/routes/socialRoutes.js +459 -0
  120. package/routes/sportsRoutes.js +1271 -0
  121. package/routes/survivorAdminRoutes.js +345 -0
  122. package/routes/survivorRoutes.js +756 -0
  123. package/routes/uploadRoutes.js +256 -0
  124. package/routes/userProfileRoutes.js +244 -0
  125. package/routes/whatsNewRoutes.js +331 -0
  126. package/scripts/.claude/settings.local.json +15 -0
  127. package/scripts/README.md +170 -0
  128. package/scripts/RESTART_EVERYTHING.sh +104 -0
  129. package/scripts/add-claim-columns.sql +48 -0
  130. package/scripts/add-crypto-prices-cache.sql +27 -0
  131. package/scripts/add-exchange-rates-cache.sql +40 -0
  132. package/scripts/add-game-invite-column.sql +23 -0
  133. package/scripts/add-game-invite-notification.sql +33 -0
  134. package/scripts/add-game-invite-telegram-pref.sql +16 -0
  135. package/scripts/add-game-joined-notification.sql +16 -0
  136. package/scripts/add-game-joined-pref.js +40 -0
  137. package/scripts/add-game-joined-preference.sql +6 -0
  138. package/scripts/add-game-start-notifications.sql +41 -0
  139. package/scripts/add-notification-flags-to-games.sql +55 -0
  140. package/scripts/add-pending-game-dismissals.sql +19 -0
  141. package/scripts/add-preferred-currency.sql +34 -0
  142. package/scripts/add-winner-columns.js +61 -0
  143. package/scripts/add_mention_system.sql +53 -0
  144. package/scripts/add_payment_system.sql +96 -0
  145. package/scripts/add_sports_event_id_column.sql +22 -0
  146. package/scripts/analyze-cohort-data-heroku.js +276 -0
  147. package/scripts/analyze-cohort-data.js +295 -0
  148. package/scripts/analyze-prod-cohorts.sh +10 -0
  149. package/scripts/backfill-matchup-images.js +245 -0
  150. package/scripts/backfill-missing-signatures.js +175 -0
  151. package/scripts/backfill-referral-earnings.js +202 -0
  152. package/scripts/check-chat-schema.js +130 -0
  153. package/scripts/check-db.sh +14 -0
  154. package/scripts/check_oracle_in_game.js +54 -0
  155. package/scripts/cleanup-database.js +193 -0
  156. package/scripts/clear-notification-cache.js +85 -0
  157. package/scripts/convert-mnemonic.js +50 -0
  158. package/scripts/create-users-table.sql +44 -0
  159. package/scripts/debug-cohort-counts.js +248 -0
  160. package/scripts/debug-winner-calc.js +84 -0
  161. package/scripts/deploy-payment-system.sh +118 -0
  162. package/scripts/deploy-to-heroku.sh +63 -0
  163. package/scripts/diagnose-locked-round.js +143 -0
  164. package/scripts/dubs-cli.js +720 -0
  165. package/scripts/dump-account.js +65 -0
  166. package/scripts/find-vrf-offset.js +48 -0
  167. package/scripts/fix-chat-notifications-constraint.sql +122 -0
  168. package/scripts/fix-claim-columns.js +124 -0
  169. package/scripts/fix-constraint-now.js +44 -0
  170. package/scripts/fix-lock-timestamps.js +96 -0
  171. package/scripts/fix-locked-round.sh +126 -0
  172. package/scripts/fix-missing-badges.sql +91 -0
  173. package/scripts/fix-payment-notifications.sql +41 -0
  174. package/scripts/force-new-round.js +55 -0
  175. package/scripts/force-resolve-and-claim.js +278 -0
  176. package/scripts/important/README.md +115 -0
  177. package/scripts/important/authority-force-lock.js +197 -0
  178. package/scripts/important/authority-resolve-game.js +267 -0
  179. package/scripts/important/check-game-status.js +373 -0
  180. package/scripts/important/list-pending-games-by-version.js +270 -0
  181. package/scripts/important/reconcile-v1-v2-payouts.js +270 -0
  182. package/scripts/initialize-jackpot.js +111 -0
  183. package/scripts/jackpot/.claude/settings.local.json +10 -0
  184. package/scripts/jackpot/force-reset.js +84 -0
  185. package/scripts/jackpot/initialize-mainnet.js +100 -0
  186. package/scripts/jackpot/keeper.js +742 -0
  187. package/scripts/jackpot/status.js +107 -0
  188. package/scripts/jackpot/update-round-duration.js +143 -0
  189. package/scripts/keeper-bot.js +112 -0
  190. package/scripts/list-pending-games.js +131 -0
  191. package/scripts/migrate-chat-v2.js +127 -0
  192. package/scripts/migrate-chat-winners.js +84 -0
  193. package/scripts/migrate-chat.sh +17 -0
  194. package/scripts/migrate-game-invite.js +83 -0
  195. package/scripts/migrate-heroku-game-notifications.sh +159 -0
  196. package/scripts/migrations/001_analytics_tables.sql +422 -0
  197. package/scripts/migrations/002_add_matchup_image_url.sql +14 -0
  198. package/scripts/migrations/003_referral_earnings.sql +208 -0
  199. package/scripts/migrations/004_add_whats_new_notification_type.sql +62 -0
  200. package/scripts/migrations/005_add_connect4_your_turn_notification.sql +61 -0
  201. package/scripts/migrations/005_push_notifications.sql +55 -0
  202. package/scripts/migrations/006_add_draw_team_players.sql +28 -0
  203. package/scripts/migrations/006_add_game_cancelled_notification.sql +62 -0
  204. package/scripts/migrations/007_add_gif_url.sql +8 -0
  205. package/scripts/migrations/008_add_connect4_columns.sql +139 -0
  206. package/scripts/migrations/008_add_pool_tracking.sql +22 -0
  207. package/scripts/migrations/009_create_survivor_pool_tables.sql +174 -0
  208. package/scripts/migrations/010_add_survivor_pool_outcome.sql +28 -0
  209. package/scripts/migrations/011_create_developer_tables.sql +67 -0
  210. package/scripts/migrations/011_fix_keeper_tables.sql +85 -0
  211. package/scripts/migrations/012_create_developer_webhooks.sql +31 -0
  212. package/scripts/migrations/013_add_network_mode.sql +18 -0
  213. package/scripts/migrations/014_create_developer_app_users.sql +19 -0
  214. package/scripts/migrations/015_add_ui_config.sql +4 -0
  215. package/scripts/migrations/016_add_resolution_secret.sql +4 -0
  216. package/scripts/migrations/017_add_external_game_id.sql +3 -0
  217. package/scripts/migrations/018_create_pickem_tables.sql +115 -0
  218. package/scripts/migrations/019_expo_push_tokens.sql +19 -0
  219. package/scripts/migrations/create_whats_new_tables.sql +88 -0
  220. package/scripts/migrations/drop_live_games_tables.sql +34 -0
  221. package/scripts/open-jackpot-round.js +85 -0
  222. package/scripts/purge-all-data.sh +329 -0
  223. package/scripts/purge-all-data.sql +142 -0
  224. package/scripts/purge-heroku-data.sh +149 -0
  225. package/scripts/purge-heroku-data.sql +62 -0
  226. package/scripts/rebuild-heroku-database.sh +113 -0
  227. package/scripts/recover-funds.js +357 -0
  228. package/scripts/regenerate-epl-images.js +278 -0
  229. package/scripts/resize-s3-matchup-images.js +374 -0
  230. package/scripts/resolve-direct.js +88 -0
  231. package/scripts/resolve-mock-game.js +124 -0
  232. package/scripts/resolve-pickem-game.js +55 -0
  233. package/scripts/resolve-round-manual.js +83 -0
  234. package/scripts/resolve-stuck-game.js +382 -0
  235. package/scripts/resolve-stuck-round.js +42 -0
  236. package/scripts/run-connect4-migration.sh +16 -0
  237. package/scripts/run-mention-migration.sh +32 -0
  238. package/scripts/run-payment-migration.sh +51 -0
  239. package/scripts/run-preferred-currency-migration.sh +31 -0
  240. package/scripts/run-referral-earnings-migration.sh +32 -0
  241. package/scripts/run-survivor-outcome-migration.sh +16 -0
  242. package/scripts/seed-test-users.js +346 -0
  243. package/scripts/setup-auth-tables.js +78 -0
  244. package/scripts/setup-complete-database.sql +992 -0
  245. package/scripts/setup-database-fresh.sh +359 -0
  246. package/scripts/setup-heroku-keeper.sh +48 -0
  247. package/scripts/setup-keeper-database.js +83 -0
  248. package/scripts/setup-keeper-state-db.sql +110 -0
  249. package/scripts/setup-oracle.sh +39 -0
  250. package/scripts/setup-pnl-tracking.js +111 -0
  251. package/scripts/start-devnet.sh +14 -0
  252. package/scripts/test-arcade-devnet.sh +160 -0
  253. package/scripts/test-arcade-match.sh +109 -0
  254. package/scripts/test-automatic-mode.sh +239 -0
  255. package/scripts/test-connect4-cancel-claim.js +370 -0
  256. package/scripts/test-connect4-e2e.js +369 -0
  257. package/scripts/test-connect4-resolve.js +369 -0
  258. package/scripts/test-game-state-endpoint.js +136 -0
  259. package/scripts/test-invite-notification.js +86 -0
  260. package/scripts/test-jackpot-api.sh +71 -0
  261. package/scripts/test-poll-confirmation.js +267 -0
  262. package/scripts/test-resolve-game.js +271 -0
  263. package/scripts/test-resolve-signature.js +223 -0
  264. package/scripts/test-signature-preservation.js +124 -0
  265. package/scripts/test-state-machine.js +291 -0
  266. package/scripts/test-webhook-receiver.js +60 -0
  267. package/scripts/update-notification-constraint.js +52 -0
  268. package/scripts/verify-account-layout.js +145 -0
  269. package/scripts/verify-winner-algorithm.js +278 -0
  270. package/server.js +5259 -0
  271. package/services/arcadeMatchService.js +763 -0
  272. package/services/automaticGameOracle.js +1596 -0
  273. package/services/chatService.js +1612 -0
  274. package/services/connect4GameService.js +1049 -0
  275. package/services/connect4NotificationService.js +374 -0
  276. package/services/cryptoPriceService.js +223 -0
  277. package/services/customGameResolver.js +260 -0
  278. package/services/db.js +79 -0
  279. package/services/directMessageService.js +389 -0
  280. package/services/discordNotifications.js +160 -0
  281. package/services/exchangeRateService.js +289 -0
  282. package/services/expoPushService.js +314 -0
  283. package/services/gamesCacheService.js +539 -0
  284. package/services/jackpotHistory.js +331 -0
  285. package/services/jackpotService.js +856 -0
  286. package/services/keeperStateService.js +355 -0
  287. package/services/matchupImageService.js +591 -0
  288. package/services/notificationCacheService.js +407 -0
  289. package/services/pickemOracle.js +440 -0
  290. package/services/playerStatsService.js +389 -0
  291. package/services/portfolioService.js +555 -0
  292. package/services/promoService.js +757 -0
  293. package/services/promoTreasuryService.js +239 -0
  294. package/services/pushNotifications.js +353 -0
  295. package/services/redisService.js +422 -0
  296. package/services/referralEarningsService.js +728 -0
  297. package/services/s3Service.js +396 -0
  298. package/services/socialService.js +1202 -0
  299. package/services/survivorOracle.js +469 -0
  300. package/services/survivorSimulator.js +475 -0
  301. package/services/telegramNotifications.js +461 -0
  302. package/services/userProfileStatsService.js +1185 -0
  303. package/services/whatsNewService.js +388 -0
  304. package/utils/urlHelper.js +95 -0
@@ -0,0 +1,188 @@
1
+ # Oracle Fixes Summary
2
+
3
+ ## Problems Fixed
4
+
5
+ ### 1. โŒ Duplicate Notifications
6
+ **Issue:** Users were receiving multiple duplicate "starting soon" notifications (2m, 3m, 4m, etc.)
7
+
8
+ **Root Cause:**
9
+ - Race condition: notification flag check and update were not atomic
10
+ - Silent error catching: `.catch()` on flag updates swallowed errors
11
+ - When database update failed (missing columns), oracle would retry every cycle
12
+
13
+ **Fix:**
14
+ - **Mark notification flag FIRST** before sending any notifications
15
+ - Made flag update errors throw exceptions instead of silent catch
16
+ - Added critical logging to track flag updates
17
+ - If flag update fails, notification won't be sent (prevents infinite duplicates)
18
+
19
+ **Files Changed:**
20
+ - `services/automaticGameOracle.js` - Lines 184-237 (sendGameStartingSoonNotification)
21
+ - `services/automaticGameOracle.js` - Lines 239-273 (sendGameStartingNowNotification)
22
+
23
+ ---
24
+
25
+ ### 2. โŒ Games Not Resolving
26
+ **Issue:** Oracle couldn't find games or resolve them properly
27
+
28
+ **Root Cause:**
29
+ - Oracle was pointing to REMOTE (Heroku) database instead of LOCAL PostgreSQL during development
30
+ - Configuration fallback: `process.env.DUBS_SERVER_URL || process.env.DUBS_GAMES_API_URL`
31
+ - If `DUBS_SERVER_URL` was set in `.env` (pointing to production), it would query remote database
32
+ - Missing detailed logging made debugging impossible
33
+
34
+ **Fix:**
35
+ - Updated configuration to use PORT-based local URL: `http://localhost:${PORT}`
36
+ - Added extensive logging to `checkSportsGameResult()` method:
37
+ - Logs all available games from ESPN API
38
+ - Shows team name matching attempts
39
+ - Displays why games aren't matching (date/team mismatches)
40
+ - Tracks final scores when found
41
+ - Better error handling with clear console output
42
+
43
+ **Files Changed:**
44
+ - `cron/oracleMonitor.js` - Line 44 (fixed configuration)
45
+ - `services/automaticGameOracle.js` - Lines 385-461 (enhanced logging in checkSportsGameResult)
46
+
47
+ ---
48
+
49
+ ### 3. โŒ Dead Firebase/dubs-games-api References
50
+ **Issue:** Code still had fallback logic for Firebase (dubs-games-api), which is dead
51
+
52
+ **Fix:** Removed ALL Firebase references:
53
+ - Removed `dubsGamesApiUrl` from constructor
54
+ - Removed Firebase fallback in `checkPendingGames()`
55
+ - Removed `updateGameInFirebase()` method entirely
56
+ - Simplified `sendTelegramNotifications()` (removed Firestore storage)
57
+ - Removed all `|| this.dubsGamesApiUrl` fallbacks (8 locations)
58
+ - Updated `cron/oracleMonitor.js` to not pass dubsGamesApiUrl
59
+
60
+ **Files Changed:**
61
+ - `services/automaticGameOracle.js` - Multiple locations (constructor, checkPendingGames, resolveGame, etc.)
62
+ - `cron/oracleMonitor.js` - Removed dubsGamesApiUrl configuration
63
+
64
+ ---
65
+
66
+ ## Before vs After
67
+
68
+ ### Before (Broken):
69
+ ```javascript
70
+ // Race condition - notification sent BEFORE flag updated
71
+ await this.sendWebAppNotifications(...);
72
+ await axios.post('.../update-notification-flags', ...)
73
+ .catch(err => console.log('Failed')); // SILENT FAILURE!
74
+ ```
75
+
76
+ ### After (Fixed):
77
+ ```javascript
78
+ // Flag updated FIRST - prevents race condition
79
+ await axios.post('.../update-notification-flags', ...);
80
+ // If above fails, throw error - DON'T send notification
81
+ await this.sendWebAppNotifications(...);
82
+ ```
83
+
84
+ ---
85
+
86
+ ### Before (Wrong Database):
87
+ ```javascript
88
+ dubsServerUrl: process.env.DUBS_SERVER_URL || process.env.DUBS_GAMES_API_URL || 'http://localhost:3001'
89
+ // If DUBS_SERVER_URL=https://dubs-server-dev.herokuapp.com in .env
90
+ // Oracle queries PRODUCTION database even when developing locally!
91
+ ```
92
+
93
+ ### After (Fixed):
94
+ ```javascript
95
+ dubsServerUrl: process.env.DUBS_SERVER_URL || `http://localhost:${process.env.PORT || 3001}`
96
+ // In development: uses http://localhost:3001 (local PostgreSQL)
97
+ // In production: uses explicit DUBS_SERVER_URL from Heroku config
98
+ ```
99
+
100
+ ---
101
+
102
+ ## Testing Checklist
103
+
104
+ - [ ] Start local PostgreSQL server
105
+ - [ ] Start dubs-server: `npm start`
106
+ - [ ] Start oracle: `npm run oracle:start`
107
+ - [ ] Create a test game in local database
108
+ - [ ] Verify oracle finds the game: `๐Ÿ“ก Querying: http://localhost:3001/api/games`
109
+ - [ ] Verify notification sent only ONCE (no duplicates)
110
+ - [ ] Check logs show flag was updated BEFORE notification sent
111
+ - [ ] Verify game resolution works when ESPN data available
112
+
113
+ ---
114
+
115
+ ## Environment Variables
116
+
117
+ ### For Local Development:
118
+ ```bash
119
+ # .env file
120
+ PORT=3001
121
+ DATABASE_URL=postgresql://adamdahan@localhost:5432/dubs_db
122
+ LIVE_SCORES_API_URL=http://localhost:3000
123
+ SOLANA_NETWORK=http://127.0.0.1:8899 # or devnet
124
+ PROGRAM_ID=your_program_id
125
+ ```
126
+
127
+ **IMPORTANT:** Do NOT set `DUBS_SERVER_URL` in local `.env`! Let it default to localhost.
128
+
129
+ ### For Production (Heroku):
130
+ ```bash
131
+ # Heroku config vars
132
+ DUBS_SERVER_URL=https://your-app.herokuapp.com
133
+ DATABASE_URL=postgresql://... (auto-set by Heroku)
134
+ LIVE_SCORES_API_URL=https://your-live-scores-api.com
135
+ ```
136
+
137
+ ---
138
+
139
+ ## Debugging Tips
140
+
141
+ ### Check Oracle Configuration:
142
+ ```bash
143
+ npm run oracle:start
144
+ ```
145
+
146
+ Look for:
147
+ ```
148
+ ๐Ÿค– Initializing Oracle Monitor...
149
+ PostgreSQL: http://localhost:3001 # Should be LOCAL
150
+ Live Scores API: http://localhost:3000
151
+ ```
152
+
153
+ ### Check Notification Flags:
154
+ ```sql
155
+ SELECT game_id, lock_notification_sent_10min, lock_notification_sent_now
156
+ FROM games
157
+ WHERE game_mode = 4
158
+ AND is_resolved = false;
159
+ ```
160
+
161
+ ### Watch Oracle Logs:
162
+ ```bash
163
+ # Local
164
+ npm run oracle:start
165
+
166
+ # Heroku
167
+ heroku logs --tail --app dubs-server-dev --dyno oracle
168
+ ```
169
+
170
+ ---
171
+
172
+ ## Related Documentation
173
+
174
+ - `/scripts/add-notification-flags-to-games.sql` - Migration that adds missing columns
175
+ - `TELEGRAM_NOTIFICATIONS_DIAGNOSIS.md` - Original diagnosis document
176
+ - `ORACLE_POSTGRESQL_UPDATE.md` - PostgreSQL integration notes
177
+
178
+ ---
179
+
180
+ **Fixed:** December 3, 2025
181
+ **Issues:** Duplicate notifications, games not resolving, wrong database
182
+ **Status:** โœ… Ready for testing
183
+
184
+
185
+
186
+
187
+
188
+
@@ -0,0 +1,202 @@
1
+ # Oracle PostgreSQL Database Update - Implementation Summary
2
+
3
+ ## Problem
4
+ The game oracle was only updating Firebase (old database) when resolving sports games, but not updating PostgreSQL (new database on Heroku). This caused the `automaticStatus` field and `finalScore` data to remain stale in the new database, showing games as "In Progress" even when they were finished.
5
+
6
+ ## Solution
7
+ Updated the oracle to write game results to **both** databases (Firebase AND PostgreSQL) when resolving games.
8
+
9
+ ---
10
+
11
+ ## Changes Made
12
+
13
+ ### 1. Added PostgreSQL Resolve Endpoint (`routes/gamesRoutes.js`)
14
+
15
+ **New Route:** `POST /api/games/:gameId/resolve`
16
+
17
+ This endpoint updates the game's resolution status in PostgreSQL:
18
+ - Sets `is_resolved = true`
19
+ - Sets `automatic_status = 'resolved'`
20
+ - Stores `finalScore` data (winner, homeScore, awayScore) in the `sports_event` JSONB field
21
+ - Updates `updated_at` timestamp
22
+
23
+ ```javascript
24
+ router.post('/:gameId/resolve', async (req, res) => {
25
+ const { winner, homeScore, awayScore, resolvedAt, resolvedBy } = req.body;
26
+
27
+ // Updates PostgreSQL with resolution data
28
+ await pool.query(`
29
+ UPDATE games
30
+ SET
31
+ is_resolved = true,
32
+ automatic_status = 'resolved',
33
+ sports_event = COALESCE(sports_event, '{}'::jsonb) || $1::jsonb,
34
+ updated_at = NOW()
35
+ WHERE game_id = $2
36
+ `, [JSON.stringify({ finalScore: {...} }), gameId]);
37
+ });
38
+ ```
39
+
40
+ ### 2. Updated Oracle Service (`services/automaticGameOracle.js`)
41
+
42
+ #### Added `updateGameInPostgreSQL()` method
43
+ - Calls the new resolve endpoint
44
+ - Uses environment variable `DUBS_SERVER_URL` or constructs URL from `PORT`
45
+ - Includes proper error handling and logging
46
+
47
+ #### Modified `resolveGame()` method
48
+ - Now calls **both** `updateGameInFirebase()` AND `updateGameInPostgreSQL()` in parallel
49
+ - Uses `Promise.all()` for concurrent database updates
50
+
51
+ ```javascript
52
+ await Promise.all([
53
+ this.updateGameInFirebase(gameId, result),
54
+ this.updateGameInPostgreSQL(gameId, result)
55
+ ]);
56
+ ```
57
+
58
+ ### 3. Enhanced GET User Games Endpoint (`routes/gamesRoutes.js`)
59
+
60
+ Updated `GET /api/auth/games/user/:walletAddress` to:
61
+ - Extract `finalScore` from the `sports_event` JSONB field
62
+ - Return it as a top-level field for easier access by the frontend
63
+
64
+ ```javascript
65
+ const finalScore = row.sports_event?.finalScore || null;
66
+ return {
67
+ // ... other fields
68
+ finalScore: finalScore, // Now accessible at top level
69
+ automaticStatus: row.automatic_status
70
+ };
71
+ ```
72
+
73
+ ### 4. Updated Environment Configuration (`env.template`)
74
+
75
+ Added new environment variable:
76
+ ```bash
77
+ DUBS_SERVER_URL=http://localhost:3001
78
+ # Your dubs-server endpoint (PostgreSQL database)
79
+ # For production (Heroku): https://dubs-server-production.herokuapp.com
80
+ # For local dev: http://localhost:3001
81
+ ```
82
+
83
+ ### 5. Fixed Frontend Game Status Logic (`dubs-jackpot/app/v2/features/sports/components/MyGamesWidget.tsx`)
84
+
85
+ Updated `getGameStatus()` to:
86
+ - Check `automaticStatus` field ('pending', 'locked', 'in_progress', 'resolved')
87
+ - Check `finalScore.winner` to determine if user won or lost
88
+ - Added `hasGameStarted()` helper to check if timestamp has passed
89
+ - Added `hasGameFinished()` helper to detect games that should be done (5+ hours after start)
90
+
91
+ ---
92
+
93
+ ## How It Works Now
94
+
95
+ 1. **Oracle detects finished game** via live scores API
96
+ 2. **Resolves on-chain** via Solana smart contract
97
+ 3. **Updates Firebase** (old database) โ† Already working
98
+ 4. **Updates PostgreSQL** (new database) โ† **NEW!**
99
+ - Sets `automaticStatus = 'resolved'`
100
+ - Stores `finalScore` in `sports_event` JSON
101
+ 5. **Sends notifications** via Telegram
102
+ 6. **Frontend refreshes** and shows correct status:
103
+ - "Claimable" for winners
104
+ - "Lost" for losers
105
+ - "In Progress" for ongoing games
106
+
107
+ ---
108
+
109
+ ## Database Schema Changes
110
+
111
+ No schema changes required! We're using the existing `sports_event` JSONB field to store `finalScore`:
112
+
113
+ ```json
114
+ {
115
+ "strEvent": "Team A vs Team B",
116
+ "strLeague": "NFL",
117
+ "finalScore": {
118
+ "winner": "home",
119
+ "homeScore": 24,
120
+ "awayScore": 17,
121
+ "resolvedAt": "2024-01-15T20:30:00.000Z",
122
+ "resolvedBy": "oracle"
123
+ }
124
+ }
125
+ ```
126
+
127
+ ---
128
+
129
+ ## Environment Setup
130
+
131
+ ### Local Development
132
+ ```bash
133
+ DUBS_SERVER_URL=http://localhost:3001
134
+ ```
135
+
136
+ ### Production (Heroku)
137
+ ```bash
138
+ DUBS_SERVER_URL=https://dubs-server-production.herokuapp.com
139
+ ```
140
+
141
+ The oracle will automatically construct the URL if `DUBS_SERVER_URL` is not set, using the `PORT` environment variable.
142
+
143
+ ---
144
+
145
+ ## Testing
146
+
147
+ To test the oracle resolution:
148
+
149
+ ```bash
150
+ # Run the oracle monitor
151
+ node cron/oracleMonitor.js
152
+
153
+ # Or manually resolve a game
154
+ node scripts/test-resolve-game.js GAME_ID --winner=home
155
+ ```
156
+
157
+ The oracle will now update both databases when resolving games!
158
+
159
+ ---
160
+
161
+ ## API Endpoints
162
+
163
+ ### New Endpoint
164
+ - `POST /api/games/:gameId/resolve` - Resolve game in PostgreSQL
165
+
166
+ ### Updated Endpoints
167
+ - `GET /api/auth/games/user/:walletAddress` - Now returns `finalScore` at top level
168
+
169
+ ---
170
+
171
+ ## Benefits
172
+
173
+ โœ… Games now show correct status ("Claimable", "Lost", "In Progress")
174
+ โœ… No more stale "In Progress" games in the UI
175
+ โœ… Users can claim winnings immediately after game finishes
176
+ โœ… Both databases stay in sync
177
+ โœ… Backwards compatible - Firebase still works
178
+ โœ… No database schema changes required
179
+
180
+ ---
181
+
182
+ ## Next Steps
183
+
184
+ 1. Deploy updated code to Heroku
185
+ 2. Set `DUBS_SERVER_URL` environment variable on Heroku
186
+ 3. Restart oracle monitor (if running as separate dyno)
187
+ 4. Verify game resolutions update PostgreSQL correctly
188
+
189
+ ---
190
+
191
+ **Created:** 2025-01-XX
192
+ **Author:** AI Assistant
193
+ **Status:** โœ… Ready for deployment
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
@@ -0,0 +1,209 @@
1
+ # Payment System Deployment to Heroku
2
+
3
+ ## โœ… Pre-Deployment Checklist
4
+
5
+ ### 1. **Database Migration** (CRITICAL)
6
+ The payment system added new notification types to the database. This migration runs automatically on server startup.
7
+
8
+ **What it does:**
9
+ - Drops old `chat_notifications_notification_type_check` constraint
10
+ - Adds new constraint including `'payment_received'` and `'payment_sent'`
11
+
12
+ **Verify it worked:**
13
+ ```bash
14
+ # After deployment, check Heroku logs
15
+ heroku logs --tail --app your-app-name
16
+
17
+ # Look for:
18
+ โœ… Chat notification types updated with payment types
19
+ ```
20
+
21
+ ### 2. **Environment Variables**
22
+ No new environment variables needed! Payment system uses existing:
23
+ - `DATABASE_URL` - PostgreSQL connection
24
+ - `SOLANA_RPC_URL` - Already configured
25
+ - `JWT_SECRET` - Already configured
26
+
27
+ ### 3. **Code Changes Summary**
28
+
29
+ #### **Server (`dubs-server`):**
30
+ - โœ… `server.js` - Passes `paymentSignature` to message handler
31
+ - โœ… `services/chatService.js` - Skips mention notifications for payments, creates payment notifications
32
+ - โœ… Database constraint updated to allow `payment_received` and `payment_sent` notification types
33
+
34
+ #### **Client (`dubs-jackpot`):**
35
+ - โœ… Payment service uses wallet adapter connection (no new RPC calls)
36
+ - โœ… Payment received animation component
37
+ - โœ… Payment animation overlay
38
+ - โœ… Green tinted payment message rows
39
+ - โœ… Green payment notifications in dropdown
40
+ - โœ… No confirmation wait - instant transaction broadcast
41
+
42
+ ## ๐Ÿš€ Deployment Steps
43
+
44
+ ### Server Deployment (dubs-server)
45
+
46
+ ```bash
47
+ # Navigate to server directory
48
+ cd /path/to/dubs-server
49
+
50
+ # Make sure all changes are committed
51
+ git status
52
+ git add .
53
+ git commit -m "feat: add payment system with notifications and animations"
54
+
55
+ # Deploy to Heroku
56
+ git push heroku main
57
+
58
+ # Watch the deployment
59
+ heroku logs --tail
60
+
61
+ # Verify database migration ran
62
+ # Look for: "โœ… Chat notification types updated with payment types"
63
+ ```
64
+
65
+ ### Client Deployment (dubs-jackpot)
66
+
67
+ ```bash
68
+ # Navigate to client directory
69
+ cd /path/to/dubs-jackpot
70
+
71
+ # Build the app
72
+ npm run build
73
+
74
+ # Deploy to Netlify (or your hosting platform)
75
+ # If using Netlify CLI:
76
+ netlify deploy --prod
77
+
78
+ # Or push to git if using auto-deploy:
79
+ git push origin main
80
+ ```
81
+
82
+ ## ๐Ÿงช Testing After Deployment
83
+
84
+ ### Test 1: Send Payment
85
+ 1. Open app in two browsers (different users, must be friends)
86
+ 2. Browser 1: Type `@username $0.01`
87
+ 3. Approve in wallet
88
+ 4. Check console logs for: `โœ… [ChatInput] Transaction broadcast!`
89
+
90
+ ### Test 2: Verify Payment Received
91
+ **In Browser 2 (recipient):**
92
+ 1. Should see **full-screen animation** ๐Ÿ’ฐ
93
+ 2. Should see **green notification** in bell
94
+ 3. Should see **green message row** in chat
95
+ 4. Should see **payment badge** with Verify link
96
+
97
+ ### Test 3: Verify Database
98
+ ```bash
99
+ # Connect to Heroku PostgreSQL
100
+ heroku pg:psql --app your-app-name
101
+
102
+ # Check payments table
103
+ SELECT * FROM chat_payments ORDER BY created_at DESC LIMIT 5;
104
+
105
+ # Check notifications
106
+ SELECT * FROM chat_notifications
107
+ WHERE notification_type = 'payment_received'
108
+ ORDER BY created_at DESC LIMIT 5;
109
+ ```
110
+
111
+ ## ๐Ÿ“Š What to Monitor
112
+
113
+ ### Server Logs
114
+ ```bash
115
+ heroku logs --tail --app your-app-name | grep -i payment
116
+ ```
117
+
118
+ **Look for:**
119
+ - `๐Ÿ’ฐ [Chat] Saving payment`
120
+ - `๐Ÿ’ฐ [Chat] Payment saved as CONFIRMED`
121
+ - `๐Ÿ’ฐ [Chat] Sent payment notification to @username`
122
+
123
+ ### Client Console
124
+ **Sender side:**
125
+ - `๐Ÿ’ฐ [ChatInput] Sending โ—ŽX SOL to @username...`
126
+ - `โœ… [ChatInput] Transaction broadcast! Signature: abc...`
127
+ - `๐Ÿ”— [ChatInput] Verify: https://explorer.solana.com/tx/...`
128
+
129
+ **Recipient side:**
130
+ - `๐Ÿ’ฐ [Chat] Message has PAYMENT: { amount: X, status: 'confirmed', signature: 'abc...' }`
131
+ - `๐Ÿ’ฐ [PaymentAnimation] Payment received!`
132
+
133
+ ## ๐Ÿ› Troubleshooting
134
+
135
+ ### Issue: "chat_notifications_notification_type_check" error
136
+ **Solution:** Database migration didn't run. Restart the dyno:
137
+ ```bash
138
+ heroku restart --app your-app-name
139
+ ```
140
+
141
+ ### Issue: Payments not showing in chat
142
+ **Check:**
143
+ 1. Is wallet connected? (Check wallet adapter)
144
+ 2. Are users friends? (Mentions only work for friends)
145
+ 3. Is payment signature being sent? (Check browser console)
146
+
147
+ ### Issue: Animation not showing
148
+ **Check:**
149
+ 1. Is `PaymentAnimationOverlay` mounted? (Check React DevTools)
150
+ 2. Is notification event firing? (Check console for `payment:received`)
151
+ 3. Is notification type correct? (Should be `payment_received`)
152
+
153
+ ### Issue: 429 Rate Limit Errors
154
+ **Solution:** Using devnet RPC with no API key can rate limit. Options:
155
+ 1. Use Helius free tier: `https://devnet.helius-rpc.com/?api-key=YOUR_KEY`
156
+ 2. Use QuickNode free tier
157
+ 3. We're already using cached connection from wallet adapter - should be fine!
158
+
159
+ ## ๐ŸŽฏ Success Criteria
160
+
161
+ After deployment, you should be able to:
162
+ - โœ… Send SOL payments via chat (`@user $amount`)
163
+ - โœ… Payments appear with green background in chat
164
+ - โœ… Recipients get green notification with ๐Ÿ’ต icon
165
+ - โœ… Recipients see full-screen celebration animation
166
+ - โœ… Payment badge shows with "Verify" link to Solana Explorer
167
+ - โœ… All payments stored in database as 'confirmed'
168
+ - โœ… No mention notifications for payment messages
169
+
170
+ ## ๐Ÿ“ Rollback Plan
171
+
172
+ If something goes wrong:
173
+
174
+ ```bash
175
+ # Revert to previous deployment
176
+ heroku releases --app your-app-name
177
+ heroku rollback v123 --app your-app-name # Use previous version number
178
+
179
+ # Or revert git commit and redeploy
180
+ git revert HEAD
181
+ git push heroku main
182
+ ```
183
+
184
+ ## ๐Ÿ” Security Notes
185
+
186
+ - โœ… No private keys stored on server
187
+ - โœ… Payments verified via transaction signature
188
+ - โœ… Wallet adapter handles all signing client-side
189
+ - โœ… Server only stores public information (signatures, amounts)
190
+ - โœ… Users can verify all transactions on Solana Explorer
191
+
192
+ ## ๐Ÿ“š Related Documentation
193
+
194
+ - Payment system architecture: `app/v2/PAYMENT_SYSTEM_DEPLOYED.md`
195
+ - Database setup: `DATABASE_SETUP_SUMMARY.md`
196
+ - API endpoints: `postman/` collection
197
+
198
+ ---
199
+
200
+ **Ready to deploy!** ๐Ÿš€
201
+
202
+ The payment system is fully functional and tested. The database migration will run automatically on first startup after deployment.
203
+
204
+
205
+
206
+
207
+
208
+
209
+