Repository navigation
Handle TRY_AGAIN_LATER and DUPLICATE in the JS payment example - #2978
Conversation
The example threw on every send status but PENDING. It now resends the same signed transaction on TRY_AGAIN_LATER, treats DUPLICATE as pending, and polls getTransaction until a ledger closes past the time bound, so a slow ledger no longer reads as a failed payment.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
🤖 Automated message from the Stellar Triage Bot. Reviewing because this is my own PR for #2963, which @oceans404 called me in on. Verified: the statuses, response fields and |
ElliotFriend
left a comment
There was a problem hiding this comment.
The status handling is a big improvement, and the TRY_AGAIN_LATER and NOT_FOUND loops match the submitTx helper from #2977. Three things before this can merge:
-
The resend bullet above the code is now wrong. It says that if the transaction was already applied, "RPC will simply return the saved result and not attempt to submit the transaction again." That isn't what happens, and it contradicts this PR's own code and the submitTx guide: sending a transaction again after it's in a ledger returns
TRY_AGAIN_LATERfor about 10 ledgers, thenERROR(txBAD_SEQ). Please rewrite that bullet (and the one after it) to say that resending the same signed transaction is safe because it can only go into a ledger once, and that you check the result withgetTransaction. -
Look up the hash before throwing on
ERROR. Because of point 1, anERRORreply can mean the payment already succeeded on an earlier send. Do what the submitTx helper does: callgetTransaction(hash), and only throw when it returnsNOT_FOUND. -
Don't log "submitted" when the transaction was never accepted. If the
TRY_AGAIN_LATERloop stops because a ledger closed aftermaxTime, the status is stillTRY_AGAIN_LATER, but the code logs "Payment Transaction submitted" before it eventually throws the expiry error. Please only log that forPENDINGandDUPLICATE. You could also throw right after the loop when the status is stillTRY_AGAIN_LATER.
…nts-send-statuses
|
🤖 Automated message from the Stellar Triage Bot. Back on this because @ElliotFriend requested changes. All three are done in 23525a9:
I also merged main. Checks pass and the preview shows the new code. Ready to merge once your review is cleared. |
ElliotFriend
left a comment
There was a problem hiding this comment.
All three addressed. Thanks.
🤖 Automated message from the Stellar Triage Bot.
Closes #2963
Working this because @oceans404 added
bot:take. The JS payment example saidDUPLICATEandTRY_AGAIN_LATERneeded handling, then threw on both. This PR handles them in the code.What changes
In
send-and-receive-payments.mdx:getTransaction. An applied transaction sent again returnsTRY_AGAIN_LATERfor about 10 ledgers, thenERROR(txBAD_SEQ). The old text said RPC returns the saved result, which is false.In the JavaScript tab:
TRY_AGAIN_LATER: wait 2 s and send the same signed transaction again, untilmaxTimepasses. If it is stillTRY_AGAIN_LATERthen, throw an expiry error.DUPLICATE: treated likePENDING. The example waits for the result.ERROR: look up the hash withgetTransactionfirst. Throw only when it returnsNOT_FOUND.PENDINGandDUPLICATE.pollTransaction()(default 30 attempts, 1 s apart, then the lastNOT_FOUND) is replaced by agetTransactionloop. It stops atSUCCESSorFAILED, or when a ledger closes aftermaxTime. The old code reported a slow ledger as a failed payment after about 30 s, while the transaction stays valid for 180 s. That is the double-pay risk the issue names.The logic matches the
submitTxhelper insubmit-transaction-wait-js.mdx.Sources
@stellar/stellar-sdkv17.4.0:SendTransactionStatus(rpc/api.ts:515),pollTransaction(rpc/server.ts:1240-1260, defaultDEFAULT_GET_TRANSACTION_TIMEOUT = 30at:114,BasicSleepStrategy1000 ms at:231).TransactionQueue.cpp: meanings ofDUPLICATEandTRY_AGAIN_LATER.removeAppliedbans an applied transaction's hash, andisBannedreturnsTRY_AGAIN_LATER. The ban lastsTRANSACTION_QUEUE_BAN_LEDGERS = 10(HerderImpl.cpp).TransactionFrame::isTooLate: a transaction is too late whenmaxTimeis earlier than the ledger close time.getTransaction:latestLedgerCloseTimeis a string, so the code wraps it inNumber().Checks
pnpmis not available in this environment, sopnpm format:mdx,pnpm checkandpnpm check:linksdid not run here. The CIchecksjob is the format gate.Not changed (follow-ups)
PENDING, and Go gives up after 5 polls.