|
| 1 | +# Verify a USDC payment on Hedera |
| 2 | + |
| 3 | +USDC on Hedera is a normal **HTS token**, so hbar-kit can already verify it with |
| 4 | +[`verifyHtsPayment`](/guide/verify-hts). `verifyUsdcPayment` is a thin convenience wrapper that |
| 5 | +fills in the **verified USDC token id** for the network and parses amounts at **6 decimals**, so you |
| 6 | +don't rebuild that boilerplate for every invoice, checkout, or payment-link flow. |
| 7 | + |
| 8 | +It is the same read-only, non-custodial verification as the rest of hbar-kit: no private keys, no |
| 9 | +funds moved — it only reads public Mirror Node data and tells you whether the expected payment |
| 10 | +arrived. |
| 11 | + |
| 12 | +```ts |
| 13 | +import { verifyUsdcPayment } from "@hbar-kit/payments" |
| 14 | + |
| 15 | +const result = await verifyUsdcPayment({ |
| 16 | + network: "mainnet", |
| 17 | + receiver: "0.0.12345", |
| 18 | + amount: "25.00", // 25 USDC — a decimal string, never a float |
| 19 | + memo: "invoice_123", // your order / invoice id |
| 20 | + after: new Date(Date.now() - 30 * 60 * 1000), |
| 21 | +}) |
| 22 | + |
| 23 | +if (result.matched) { |
| 24 | + // Confirmed. Mark the invoice paid using result.transactionId as your idempotency key. |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +## What it does under the hood |
| 29 | + |
| 30 | +`verifyUsdcPayment` calls `verifyHtsPayment` with: |
| 31 | + |
| 32 | +- the canonical USDC token id for the selected `network`, |
| 33 | +- `decimals = 6`, |
| 34 | +- the same `receiver` / `amount` / `memo` / time-window rules, |
| 35 | +- the same [payment status model](/guide/concepts). |
| 36 | + |
| 37 | +The returned `PaymentResult` is identical to the HTS one, except its `asset` is tagged as USDC: |
| 38 | + |
| 39 | +```ts |
| 40 | +result.asset // { tokenId: "0.0.456858", decimals: 6, symbol: "USDC" } |
| 41 | +``` |
| 42 | + |
| 43 | +Use `isUsdcPaymentResult(result)` (or inspect `result.asset.tokenId`) to detect USDC results in a |
| 44 | +mixed pipeline. |
| 45 | + |
| 46 | +## Verified token ids |
| 47 | + |
| 48 | +USDC token ids are **network-specific**, so `network` is **required** — the USDC helper never |
| 49 | +assumes mainnet. The ids are verified against the live Hedera Mirror Node and Circle's official |
| 50 | +[USDC contract addresses](https://developers.circle.com/stablecoins/usdc-contract-addresses): |
| 51 | + |
| 52 | +| Network | USDC token id | |
| 53 | +| ---------- | --------------------------------------------------- | |
| 54 | +| mainnet | `0.0.456858` | |
| 55 | +| testnet | `0.0.429274` | |
| 56 | +| previewnet | _not issued by Circle_ → throws `UnsupportedAssetError` | |
| 57 | + |
| 58 | +```ts |
| 59 | +import { getUsdcTokenId, USDC_TOKEN_IDS } from "@hbar-kit/payments" |
| 60 | + |
| 61 | +getUsdcTokenId("mainnet") // "0.0.456858" |
| 62 | +USDC_TOKEN_IDS.testnet // "0.0.429274" |
| 63 | +``` |
| 64 | + |
| 65 | +## Custom / mock tokens (testnet & dev) |
| 66 | + |
| 67 | +For local testing against a mock HTS token, pass an explicit `tokenId`. It is still parsed at 6 |
| 68 | +decimals and tagged as USDC: |
| 69 | + |
| 70 | +```ts |
| 71 | +const result = await verifyUsdcPayment({ |
| 72 | + network: "testnet", |
| 73 | + tokenId: process.env.TESTNET_USDC_TOKEN_ID!, // your dev/mock token |
| 74 | + receiver: "0.0.12345", |
| 75 | + amount: "10.00", |
| 76 | + memo: "test_invoice_1", |
| 77 | +}) |
| 78 | +``` |
| 79 | + |
| 80 | +Production mainnet flows should omit `tokenId` and use the verified canonical id. |
| 81 | + |
| 82 | +## Amount precision |
| 83 | + |
| 84 | +Amounts are **decimal strings**, never floats, and USDC is always **6 decimals**: |
| 85 | + |
| 86 | +```ts |
| 87 | +"25.00" // ✅ 25 USDC |
| 88 | +"25.001234" // ✅ 6 decimals |
| 89 | +"25.0012345" // ❌ 7 decimals → throws InvalidAmountError |
| 90 | +``` |
| 91 | + |
| 92 | +## Production checklist |
| 93 | + |
| 94 | +- **Always verify server-side.** Never trust a client-supplied amount, receiver, token id, or memo |
| 95 | + — look them up from your own records, keyed by an opaque order id. |
| 96 | +- **Always use a time window** (`after`/`before`) to bound the search. |
| 97 | +- **Use a unique memo per payment request** so duplicates are detectable. |
| 98 | +- **Handle every status**, not just `confirmed`: `underpaid`, `overpaid`, `duplicate`, `mismatch`, |
| 99 | + `pending`, `expired`. |
| 100 | +- **Use `comparison: "atLeast"` only if you accept overpayment**; the default is exact-match. |
| 101 | +- **Use `transactionId` for idempotency** in your own database so each payment is processed once. |
| 102 | + |
| 103 | +```ts |
| 104 | +const result = await verifyUsdcPayment({ |
| 105 | + network: "mainnet", |
| 106 | + receiver: "0.0.12345", |
| 107 | + amount: "25.00", |
| 108 | + memo: "invoice_123", |
| 109 | + after: new Date(Date.now() - 30 * 60 * 1000), |
| 110 | +}) |
| 111 | + |
| 112 | +switch (result.status) { |
| 113 | + case "confirmed": |
| 114 | + // result.matched === true — settle the order, store result.transactionId |
| 115 | + break |
| 116 | + case "underpaid": |
| 117 | + case "overpaid": |
| 118 | + case "duplicate": |
| 119 | + case "mismatch": |
| 120 | + case "pending": |
| 121 | + // branch on each explicitly |
| 122 | + break |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Wait for a USDC payment |
| 127 | + |
| 128 | +`waitForUsdcPayment` polls until the payment is confirmed (or `overpaid`/`duplicate`), or until |
| 129 | +`timeoutMs` elapses (then `status: "expired"`). It mirrors `waitForHbarPayment` / |
| 130 | +`waitForHtsPayment` and accepts `timeoutMs`, `pollIntervalMs`, and an `AbortSignal`: |
| 131 | + |
| 132 | +```ts |
| 133 | +import { waitForUsdcPayment } from "@hbar-kit/payments" |
| 134 | + |
| 135 | +const result = await waitForUsdcPayment({ |
| 136 | + network: "mainnet", |
| 137 | + receiver: "0.0.12345", |
| 138 | + amount: "25.00", |
| 139 | + memo: "invoice_123", |
| 140 | + timeoutMs: 5 * 60 * 1000, |
| 141 | + pollIntervalMs: 3000, |
| 142 | +}) |
| 143 | +``` |
| 144 | + |
| 145 | +## When to use which |
| 146 | + |
| 147 | +- **`verifyUsdcPayment`** — you're accepting USDC and want the token id + 6-decimal parsing handled |
| 148 | + for you. The common case. |
| 149 | +- **`verifyHtsPayment`** — any other HTS token, or when you need full control over `tokenId` and |
| 150 | + `decimals`. |
0 commit comments