From c70862cf7148fa91b308c2781f0b10f14c72f26b Mon Sep 17 00:00:00 2001 From: Ramin Date: Mon, 2 Sep 2024 01:11:04 +0330 Subject: [PATCH] add loop to search for attestationHash --- src/routes/replyAttestation.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/routes/replyAttestation.ts b/src/routes/replyAttestation.ts index 18f83f8..87fe211 100644 --- a/src/routes/replyAttestation.ts +++ b/src/routes/replyAttestation.ts @@ -1,9 +1,17 @@ import neynarClient from "../helpers/neynarClient.js"; import {getProvider} from "../helpers/ethers.js"; import {NETWORK_IDS} from "../helpers/constants.js"; +import {JsonRpcProvider} from "ethers"; const baseAttestationScan = 'https://base.easscan.org/attestation/view/'; +const wait = async (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + +const getAttestationHash = async (txHash: string, provider: JsonRpcProvider) => { + const receipt = await provider.getTransactionReceipt(txHash); + return receipt?.logs[0]?.data +} + const replyAttestation = async (req: Request) => { if (!process.env.SIGNER_UUID) { throw new Error("Make sure you set SIGNER_UUID in your .env file"); @@ -11,11 +19,24 @@ const replyAttestation = async (req: Request) => { const body = await req.text(); const data = JSON.parse(body); const { txHash, praiseHash } = data; + if (!txHash || !praiseHash) { + throw new Error("txHash and praiseHash are required"); + } + console.log('-----------------'); + console.log('txHash', txHash); + console.log('praiseHash', praiseHash); + console.log('-----------------'); const provider = getProvider(NETWORK_IDS.BASE_MAINNET); - const receipt = await provider.getTransactionReceipt(txHash); - const attestationHash = receipt?.logs[0]?.data - if (!attestationHash || !praiseHash) { - throw new Error("attestationHash and praiseHash are required"); + let attestationHash; + let loopCount = 0; + while (!attestationHash || loopCount < 10) { + await wait(5000); + attestationHash = await getAttestationHash(txHash, provider); + loopCount++; + if (loopCount === 10 && !attestationHash) { + console.log("Attestation hash not found! Date: " + new Date().toISOString() + " txHash: " + txHash + " praiseHash: " + praiseHash); + throw new Error("Attestation hash not found"); + } } await neynarClient.publishCast( process.env.SIGNER_UUID,