-
Notifications
You must be signed in to change notification settings - Fork 102
chore: Replace axios with native Node.js fetch #3134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7a6ecaa
82195bc
8b12031
6ffc4a0
b9e39a4
5478073
50b6ea8
db322e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,12 @@ import { | |
| } from "../../utils"; | ||
| import { spreadEventWithBlockNumber } from "../../utils/EventUtils"; | ||
| import { FinalizerPromise, CrossChainMessage } from "../types"; | ||
| import axios from "axios"; | ||
| import UNIVERSAL_SPOKE_ABI from "../../common/abi/Universal_SpokePool.json"; | ||
| import { RelayedCallDataEvent, StoredCallDataEvent } from "../../interfaces/Universal"; | ||
| import { ApiProofRequest, ProofOutputs, ProofStateResponse, SP1HeliosProofData } from "../../interfaces/ZkApi"; | ||
| import { StorageSlotVerifiedEvent, HeadUpdateEvent } from "../../interfaces/Helios"; | ||
| import { calculateProofId, decodeProofOutputs } from "../../utils/ZkApiUtils"; | ||
| import { calculateHubPoolStoreStorageSlot, getHubPoolStoreContract } from "../../utils/UniversalUtils"; | ||
| import { stringifyThrownValue } from "../../utils/LogUtils"; | ||
| import { getSp1HeliosContractEVM } from "../../utils/HeliosUtils"; | ||
| import { getBlockFinder, getBlockForTimestamp } from "../../utils/BlockUtils"; | ||
|
|
||
|
|
@@ -344,34 +342,36 @@ async function enrichHeliosActions( | |
|
|
||
| let proofState: ProofStateResponse | null = null; | ||
|
|
||
| // @dev We need try - catch here because of how API responds to non-existing proofs: with NotFound status | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| let getError: any = null; | ||
| try { | ||
| const response = await axios.get<ProofStateResponse>(getProofUrl); | ||
| proofState = response.data; | ||
| logger.debug({ ...logContext, message: "Proof state received", proofId, status: proofState.status }); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| } catch (error: any) { | ||
| getError = error; | ||
| } | ||
|
|
||
| // Axios error. Handle based on whether was a NOTFOUND or another error | ||
| if (getError) { | ||
| const isNotFoundError = axios.isAxiosError(getError) && getError.response?.status === 404; | ||
| if (isNotFoundError) { | ||
| // NOTFOUND error -> Request proof | ||
| // @dev The API responds to non-existing proofs with 404, so we handle that specially | ||
| const getResponse = await fetch(getProofUrl); | ||
| if (!getResponse.ok) { | ||
| if (getResponse.status === 404) { | ||
| // NOTFOUND -> Request proof | ||
| logger.debug({ ...logContext, message: "Proof not found (404), requesting...", proofId }); | ||
| await axios.post(`${apiBaseUrl}/v1/api/proofs`, apiRequest); | ||
| const postResponse = await fetch(`${apiBaseUrl}/v1/api/proofs`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(apiRequest), | ||
| }); | ||
| if (!postResponse.ok) { | ||
| throw new Error( | ||
| `Failed to request proof for proofId ${proofId}: ${postResponse.status} ${postResponse.statusText}` | ||
| ); | ||
| } | ||
| logger.debug({ ...logContext, message: "Proof requested successfully.", proofId }); | ||
| continue; | ||
| } else { | ||
| // If other error is returned -- throw and alert PD; this shouldn't happen | ||
| throw new Error(`Failed to get proof state for proofId ${proofId}: ${stringifyThrownValue(getError)}`); | ||
| throw new Error( | ||
| `Failed to get proof state for proofId ${proofId}: ${getResponse.status} ${getResponse.statusText}` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // No axios error, process `proofState` | ||
| proofState = (await getResponse.json()) as ProofStateResponse; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to follow up and check how to validate types on this. I'm not really sure why it wasn't required for; perhaps the return was typed as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| logger.debug({ ...logContext, message: "Proof state received", proofId, status: proofState.status }); | ||
|
|
||
| // Process `proofState` | ||
| switch (proofState.status) { | ||
| case "pending": | ||
| // If proof generation is pending -- there's nothing for us to do yet. Will check this proof next run | ||
|
|
@@ -391,7 +391,16 @@ async function enrichHeliosActions( | |
| errorMessage: proofState.error_message, | ||
| }); | ||
|
|
||
| await axios.post(`${apiBaseUrl}/v1/api/proofs`, apiRequest); | ||
| const retryResponse = await fetch(`${apiBaseUrl}/v1/api/proofs`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(apiRequest), | ||
| }); | ||
| if (!retryResponse.ok) { | ||
| throw new Error( | ||
| `Failed to re-request errored proof for proofId ${proofId}: ${retryResponse.status} ${retryResponse.statusText}` | ||
| ); | ||
| } | ||
| logger.debug({ ...logContext, message: "Errored proof requested again successfully.", proofId }); | ||
| break; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdyt about getting rid of this try/catch construction? fetch won't throw by default so it's basically artificial that we're setting up the try/catch loop and then are the sole reason for it to throw. i.e. it should be possible to just evaluate response.status directly, rather than doing string comparisons on error.message.