Commit Graph
14 Commits
Author SHA1 Message Date
SatoshiQaziMuhammed 4f00d3f696 feat(check-deposits): check TRC20 addresses of recently active users only (#41)
TronGrid's free plan allows 10,000 requests a day. Checking every user's
TRC20 address on every five-minute run needs ~29,000 a day at 101 users,
so the key ran out at 03:00 UTC and every later run was refused.

get-deposit-info now records when a user opened the deposit screen.
check-deposits checks users seen in the last 24 hours on every run and
everyone in one run every six hours (00, 06, 12, 18 UTC): about 3,300
requests a day at 10 active users. A deposit made without opening the
screen within the past day waits for the next full scan.

The user-list query now reports its error instead of ending the TRC20
check as if there were no users.
2026-10-19 12:17:13 +04:00
SatoshiQaziMuhammed a781e456e3 fix(check-deposits): request at most 25 Subscan rows (#39)
Subscan's free plan answers 403 row_limit_exceeded to a page of 50, so
every Polkadot check failed once a key was configured. 25 is the largest
page it accepts (measured: 25 succeeds, 30 is rejected).
2026-10-18 19:52:06 +04:00
SatoshiQaziMuhammed e0d56a5fb5 fix(check-deposits): count only TON transfers sent to the deposit address (#38)
The TON check walked every jetton transfer in the deposit address's event
feed, including the ones it sends. An outgoing transfer whose comment
matched a user's deposit code would have been credited to that user; the
2026-02-13 sweep of 41.9 USDT was reported as an unknown-memo deposit on
every run.

Resolve the configured address to its raw form and skip any transfer
whose recipient is not the deposit address.
2026-10-18 19:48:14 +04:00
SatoshiQaziMuhammed 2b255c56fc fix(check-deposits): stop sending an empty TON bearer token (#37)
tonapi rejects `Authorization: Bearer ` with 401 but serves keyless
requests, so every TON check failed whenever TONAPI_KEY was unset. Send
the header only when a key is configured.

TON and Polkadot API errors now carry the first 200 bytes of the
response body and are logged, so the cause of a failure is visible
instead of a bare status code.
2026-10-18 19:37:32 +04:00
SatoshiQaziMuhammed c0c8ba074e check-deposits: one TronGrid request at a time, at most 4 a second, from a rotating start (#35)
The 429 body logged by the previous change reads: 'The key exceeds the
frequency limit(15), and the query server is suspended for 27 s'. The run sent
batches of 8 at once, which tripped it although it averaged about 7 a second,
and the suspension outlasted the 1 s and 2 s retries, which never recovered a
request.

Requests now go one at a time, at most 4 a second. The retries are removed.
Each run starts at a different user, so a run that does hit the limit stops
at a different place each time instead of always skipping the same users.
2026-10-18 16:05:14 +04:00
SatoshiQaziMuhammed 9f42edc431 check-deposits: log TronGrid's 429 reason and stop the run's TRC20 checks at it (#34)
After the pacing fix, 429 still came back in about a third of the runs: from
around the 17th user on, every request was refused, retries included
(measured 2026-10-18 over 404 runs). The body of that answer says which limit
was hit and for how long, and it was never logged. The first 429 of a run now
logs it, and the users after it are skipped with one line saying how many,
instead of each being sent into the same limit.
2026-10-18 14:49:53 +04:00
SatoshiQaziMuhammed a91d455bfb fix(check-deposits): pace TronGrid calls and retry 429 so later users are checked (#33)
check-deposits polls every user's TRC20 address each minute in unpaced
batches of 8. Since the previous change logged TronGrid failures, two
consecutive runs showed HTTP 429 from about the 24th request on: deposit
indexes 24-53, 30 of 53 users, were skipped on every run, so their TRC20
deposits were never seen. The old code returned silently on a non-2xx reply.

- batches start at least 1.1 s apart
- a 429 is retried twice, after 1 s and 2 s
- per-user failures, including thrown errors, are logged and returned in
  result.errors instead of being dropped

Deposits are found from the last 20 transfers per address and deduplicated
by tx_hash, so once users are checked again a skipped deposit is picked up
on the next run.
2026-10-18 06:28:27 +04:00
SatoshiQaziMuhammed a08754f705 fix(deposits): stop sending an empty TronGrid key and log TronGrid failures (#32)
Both TRC20 deposit functions sent `TRON-PRO-API-KEY: ''` when the key was
unset, and check-deposits returned silently on any non-2xx TronGrid
response, so a 401 or 429 skipped a user's deposits with nothing in the
logs.

- send the key header only when TRONGRID_API_KEY is set; warn once if not
- check-deposits logs the status and deposit index on a failed call
- process-deposits records the TronGrid status in results.trc20.errors
- type the batch item in check-deposits (deno check TS7006, pre-existing)
2026-10-17 20:46:08 +04:00
Satoshi Qazi Muhammed 96cda97635 perf(deposits): derive the HD master key once, not once per user
check-deposits hits the isolate's CPU ceiling on almost every run — 17 limit
events across 20 invocations in the last 20 minutes — and when it does, the
supervisor cancels the worker and the scan stops partway. Deposits after the
cut-off simply are not seen until some later run happens to reach them.

The cause is in deriveTronAddress: it calls mnemonicToSeedSync per user, which
is PBKDF2 with 2048 iterations. The seed depends only on the mnemonic, so with
51 accounts that cost was paid 51 times a minute to produce the identical seed.
Only the derivation path varies per user.

Derive the master key once and reuse it. Measured over the 51 accounts we
actually scan: 418ms -> 58ms, a 7.2x drop in the part that was blowing the
budget.

Verified the addresses are unchanged: old and new paths produce identical
private keys for every index tested, including repeated indexes in one run,
which is what would expose derive() mutating the parent key. It does not.
2026-07-30 02:36:32 -07:00
Satoshi Qazi Muhammed 8d1e5f6af5 fix(deposits): stop runaway duplicate deposit rows, fix check-deposits reliability
Root cause chain found while investigating why TRC20/TON/Polkadot deposits
were never being credited after the self-hosted Supabase migration:

- DEPOSIT_TRON_HD_MNEMONIC, TRONGRID_API_KEY, DEPOSIT_TON_ADDRESS,
  DEPOSIT_POLKADOT_ADDRESS and the pg_cron job itself were never carried
  over during the 2026-04-09 migration, so check-deposits never ran.
- Once reconnected, the TRC20 loop was fully sequential (one address at a
  time) and hit the edge function's CPU/time budget with 50+ users -
  parallelized with a bounded concurrency of 8.
- The dedup check (select-by-tx_hash + .single()) breaks permanently once
  2+ rows ever share a tx_hash - .single() then errors on every future
  lookup, so the guard silently stops working and every cron tick
  reinserts. This is what produced 50k+ and 30k+ duplicate rows for two
  historical deposits back in April. Replaced with upsert +
  onConflict/ignoreDuplicates against a new unique constraint on tx_hash,
  so duplicates are impossible at the DB level regardless of app-level
  races.
- deposit_index 0 is the platform admin account and also used as the
  treasury sweep destination - excluded from the TRC20 scan so internal
  sweep transfers landing on it are never mistaken for a customer deposit.
2026-07-12 19:21:54 -07:00
Kurdistan Tech Ministry 44a4b9395b feat(supabase): add pezkiwi.app CORS and multi-bot-token auth support
- Add telegram.pezkiwi.app to CORS allowed origins in all edge functions
- Support multiple bot tokens (TELEGRAM_BOT_TOKEN, TELEGRAM_BOT_TOKEN_KRD) in auth
- Dynamic origin matching for proper CORS headers
2026-02-14 11:09:14 +03:00
Kurdistan Tech Ministry 107dbbacdf feat: add platform fees for deposit networks
- TON: 0.1 USDT fee
- Polkadot: 0.1 USDT fee
- TRC20: 3 USDT fee (unchanged)
- Update fee display in Kurdish
2026-02-08 05:58:45 +03:00
Kurdistan Tech Ministry bf8a3cc06c fix: auto-sync wallet address to tg_users for deposit system 2026-02-08 04:24:06 +03:00
Kurdistan Tech Ministry 9cf241e859 feat: add USDT deposit system with TON, Polkadot, TRC20 support 2026-02-08 03:13:52 +03:00