-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into new-gas-price-oracle
- Loading branch information
Showing
11 changed files
with
270 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { VercelResponse } from "@vercel/node"; | ||
import { ethers } from "ethers"; | ||
import { utils } from "@across-protocol/sdk"; | ||
import axios from "axios"; | ||
|
||
import { TypedVercelRequest } from "./_types"; | ||
import { HUB_POOL_CHAIN_ID, getLogger, handleErrorCondition } from "./_utils"; | ||
import { UnauthorizedError } from "./_errors"; | ||
import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "./_constants"; | ||
|
||
const endpoints = [ | ||
{ | ||
url: "https://preview.across.to/api/swap/allowance", | ||
params: { | ||
amount: ethers.utils.parseUnits("1", 6).toString(), | ||
tradeType: "minOutput", | ||
inputToken: TOKEN_SYMBOLS_MAP.USDC.addresses[CHAIN_IDs.ARBITRUM], | ||
originChainId: CHAIN_IDs.ARBITRUM, | ||
outputToken: TOKEN_SYMBOLS_MAP.USDC.addresses[CHAIN_IDs.OPTIMISM], | ||
destinationChainId: CHAIN_IDs.OPTIMISM, | ||
depositor: "0x9A8f92a830A5cB89a3816e3D267CB7791c16b04D", | ||
skipOriginTxEstimation: true, | ||
refundOnOrigin: false, | ||
}, | ||
updateIntervalSec: 10, | ||
}, | ||
]; | ||
|
||
const maxDurationSec = 60; | ||
|
||
const handler = async ( | ||
request: TypedVercelRequest<Record<string, never>>, | ||
response: VercelResponse | ||
) => { | ||
const logger = getLogger(); | ||
logger.debug({ | ||
at: "CronPingEndpoints", | ||
message: "Starting cron job...", | ||
}); | ||
try { | ||
const authHeader = request.headers?.["authorization"]; | ||
if ( | ||
!process.env.CRON_SECRET || | ||
authHeader !== `Bearer ${process.env.CRON_SECRET}` | ||
) { | ||
throw new UnauthorizedError(); | ||
} | ||
|
||
// Skip cron job on testnet | ||
if (HUB_POOL_CHAIN_ID !== 1) { | ||
return; | ||
} | ||
|
||
const functionStart = Date.now(); | ||
|
||
const requestPromises = endpoints.map( | ||
async ({ updateIntervalSec, url, params }) => { | ||
while (true) { | ||
const diff = Date.now() - functionStart; | ||
// Stop after `maxDurationSec` seconds | ||
if (diff >= maxDurationSec * 1000) { | ||
break; | ||
} | ||
await axios.get(url, { params }); | ||
await utils.delay(updateIntervalSec); | ||
} | ||
} | ||
); | ||
await Promise.all(requestPromises); | ||
|
||
logger.debug({ | ||
at: "CronPingEndpoints", | ||
message: "Finished", | ||
}); | ||
response.status(200); | ||
response.send("OK"); | ||
} catch (error: unknown) { | ||
return handleErrorCondition("cron-ping-endpoints", response, logger, error); | ||
} | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useQueries } from "@tanstack/react-query"; | ||
import axios from "axios"; | ||
import { BigNumber } from "ethers"; | ||
import { indexerApiBaseUrl, isDefined } from "utils"; | ||
|
||
/** | ||
* A hook used to track the statuses of multiple deposits via the indexer API | ||
* @param deposits Array of deposit objects containing `originChainId` and `depositId` | ||
*/ | ||
export function useIndexerDepositsTracking( | ||
deposits: { | ||
originChainId?: number; | ||
depositId?: number; | ||
}[] | ||
) { | ||
const queries = useQueries({ | ||
queries: deposits.map((deposit) => ({ | ||
queryKey: [ | ||
"indexer_deposit_tracking", | ||
deposit.originChainId, | ||
deposit.depositId, | ||
] as [string, number, number], | ||
enabled: | ||
isDefined(deposit.originChainId) && | ||
isDefined(deposit.depositId) && | ||
isDefined(indexerApiBaseUrl), | ||
queryFn: async (): Promise<BigNumber | undefined> => { | ||
try { | ||
const response = await axios.get( | ||
`${indexerApiBaseUrl}/deposit/status`, | ||
{ | ||
params: { | ||
originChainId: deposit.originChainId, | ||
depositId: deposit.depositId, | ||
}, | ||
} | ||
); | ||
return response.data; | ||
} catch (e) { | ||
// FIXME: for now we ignore since this is for load testing purposes | ||
} | ||
}, | ||
refetchInterval: 5_000, // 5 seconds | ||
})), | ||
}); | ||
|
||
return queries.map((query) => ({ | ||
depositStatus: query.data ?? undefined, | ||
...query, | ||
})); | ||
} | ||
|
||
/** | ||
* A hook used to track a single deposit status via the indexer API | ||
* @param originChainId The chain ID of the deposit's origin | ||
* @param depositId The deposit ID | ||
*/ | ||
export function useIndexerDepositTracking( | ||
originChainId?: number, | ||
depositId?: number | ||
) { | ||
const [singleDeposit] = useIndexerDepositsTracking( | ||
isDefined(originChainId) && isDefined(depositId) | ||
? [{ originChainId, depositId }] | ||
: [] | ||
); | ||
|
||
return ( | ||
singleDeposit ?? { | ||
depositStatus: undefined, | ||
isLoading: false, | ||
isError: false, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.