diff --git a/js/onchain.js b/js/onchain.js index 26dbc4c..ccbcb97 100644 --- a/js/onchain.js +++ b/js/onchain.js @@ -4,9 +4,6 @@ import { dbIGAI, dbTo, dbRef, dbGetRef } from "./supabase.js"; import { NonceManager as O } from "@ethersproject/experimental"; import ethers from "ethers"; -let n = null; -let nonceLock = Promise.resolve(); - const { providers, Contract, Wallet } = ethers, { JsonRpcProvider } = providers, w = new Wallet(PK, new JsonRpcProvider(pr)), @@ -50,25 +47,6 @@ export async function getInfo(a) { } } -async function getNextNonce() { - await nonceLock; - let releaseLock; - nonceLock = new Promise((resolve) => { - releaseLock = resolve; - }); - try { - if (n === null) { - n = await w.provider.getTransactionCount(w.address, "pending"); - console.log("Fetched initial nonce:", n); - } - const nonceToUse = n; - n++; - return nonceToUse; - } finally { - releaseLock(); - } -} - export async function processQueue() { if (p) return; p = true; @@ -76,27 +54,20 @@ export async function processQueue() { const { d, ra, rt, aa } = q.shift(); try { await b(); - const uploadPromise = (async () => { - const cid = ( - await c.uploadFile(new File([JSON.stringify(d)], "")) - ).toString(); - dbIGAI(cid, ra, rt); - })(); - let txPromise = Promise.resolve(); - try { - const nonce = await getNextNonce(); - const tx = await r.deduct(ra, aa, { nonce }); - await tx.wait(1); - } catch (err) { - n = await w.provider.getTransactionCount(w.address, "pending"); - throw err; - } - await Promise.all([uploadPromise, txPromise]); + // Run upload and transaction in parallel + await Promise.all([ + (async () => { + const cid = ( + await c.uploadFile(new File([JSON.stringify(d)], "")) + ).toString(); + dbIGAI(cid, ra, rt); + })(), + (async () => { + const tx = await r.deduct(ra, aa); + await tx.wait(1); + })(), + ]); } catch (e) { - if (String(e).includes("Invalid nonce")) { - n = await w.provider.getTransactionCount(w.address, "pending"); - console.warn("Nonce reset to", n); - } console.error(new Date().toISOString(), "Retrying...", e); q.push({ d, ra, rt, aa }); await new Promise((r) => setTimeout(r, 5000));