Skip to content

v1.6-beta.4 - add mainnet mode + minimal gas-estimate route

Choose a tag to compare

@daviddprtma daviddprtma released this 27 Jul 02:00
· 41 commits to main since this release
fbbfaba

Behaviour summary

1. Network mode (backend/src/hederaClient.js + backend/src/utils/config.js)

  • HEDERA_NETWORK env var added; defaults to "testnet" (so testnet mode is preserved exactly as before for any deployment that doesn't set the variable).
  • makeHederaClient(operatorId, operatorKey, network?):
    • When network === "testnet"Client.forTestnet()
    • When network === "mainnet"Client.forMainnet()
    • When network is anything else (typo, case mismatch) → explicit error: Unknown Hedera network ... Set HEDERA_NETWORK to "testnet" or "mainnet".
  • New getActiveNetwork() and isMainnet() helpers. isMainnet() is consumed by index.js boot path:
    • testnet mode → logs 🧪 Hedera client running in TESTNET mode
    • mainnet mode → logs a ⚠️ warning that the deployment is signing REAL transactions for account <operator>
  • Backwards-compat: every existing caller of getHederaClient() keeps the singleton behaviour. The only contract change is the log line.

2. Gas estimator (backend/src/utils/gasEstimate.js)

  • TINYBARS_PER_HBAR = 100_000_000n exported.
  • currentFeeSchedule() walks the mirror-node /api/v1/network/fees payload's transaction_fee_schedule array and matches on Hedera's name field (post-0.46 mirror-node release) to pick the right bucket per transaction type. Falls back to the lowest-priced bucket when no name match exists.
  • estimateBatchGas({ batchCount, safetyMargin, tinybarCeiling, mirrorNodeUrl, timeoutMs, usdPerHbar }) returns:
    {
      tinybarsTotal, hbarTotal, usdEstimate,
      perBatchTinybars, safetyMargin, tinybarCeiling,
      capped,         // true → clamped to ceiling
      network,        // "testnet" | "mainnet"
      source,         // "live" (mirror node returned data) or "fallback"
      timestamp       // mirror-node payload timestamp, if available
    }
    
  • Defaults: safetyMargin = 1.1 (+10%), tinybarCeiling = 5_000_000n tinybar ≈ 0.05 HBAR. Mirror node outing falls back to a published-historical estimate (10_000n tinybar on mainnet, 1_000n on testnet) so the endpoint is always 200-OK.

3. HTTP route (backend/src/routes/gasEstimate.js)

  • GET /api/gas-estimate?batchCount=N&safetyMargin=...&tinybarCeiling=...&usdPerHbar=...
  • All query params optional; refers to defaults from estimateBatchGas().
  • Validates input: non-integer batch counts and out-of-range margins → HTTP 400 { ok: false, error: ..., code: "gas_estimate_invalid_input" }. DB / mirror outage → HTTP 200 with source: 'fallback' (the API is best-effort; the user can still submit a batch).
  • Returns bigints as decimal strings to preserve precision across JSON.

4. Booting (backend/src/index.js)

  • After getHederaClient() init, the server announces which network is active. This is a single console.warn / console.log differentiated by isMainnet(). Maintaining existing try/catch/process.exit(1) flow.

5. Documentation (backend/.env.example)

  • Adds HEDERA_NETWORK=testnet block with proactive commentary on:
    • default + safety
    • mainnet requirement (mainnet-funded operator + mainnet mirror node URL).

this improvement project is proposed by @arcgod-design

You can support AgroDex project for Indonesian agriculture in here https://github.com/sponsors/daviddprtma