-
Notifications
You must be signed in to change notification settings - Fork 24
fix: temporarily show unbonding locks the subgraph has not indexed #748
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| export type UnbondingLockInfo = { | ||
| id: string; | ||
| unbondingLockId: number; | ||
| amount: string; | ||
| withdrawRound: string; | ||
| /** | ||
| * The contract does not record which orchestrator a lock was unbonded from, | ||
| * so this is the delegator's current delegate. | ||
| */ | ||
| delegate: { id: string }; | ||
| }; | ||
|
|
||
| export type UnbondingLocks = { | ||
| locks: UnbondingLockInfo[]; | ||
| }; |
This file contains hidden or 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,102 @@ | ||
| import { getCacheControlHeader } from "@lib/api"; | ||
| import { bondingManager } from "@lib/api/abis/main/BondingManager"; | ||
| import { getBondingManagerAddress } from "@lib/api/contracts"; | ||
| import { badRequest, internalError, methodNotAllowed } from "@lib/api/errors"; | ||
| import { UnbondingLocks } from "@lib/api/types/get-unbonding-locks"; | ||
| import { l2PublicClient } from "@lib/chains"; | ||
| import { NextApiRequest, NextApiResponse } from "next"; | ||
| import { formatEther, isAddress } from "viem"; | ||
|
|
||
| /** Lock ids read per request, so a caller cannot ask for an unbounded scan. */ | ||
| const MAX_SCAN = 200; | ||
|
|
||
| /** | ||
| * TEMPORARY stopgap, paired with the subgraph outage: returns the delegator's | ||
| * unbonding locks from `from` onwards, so the UI can show locks created after | ||
| * the subgraph stopped indexing. Everything the subgraph already knows keeps | ||
| * coming from the subgraph. | ||
| * | ||
| * Remove this route along with its caller once indexing has recovered. | ||
| */ | ||
| const handler = async ( | ||
| req: NextApiRequest, | ||
| res: NextApiResponse<UnbondingLocks | null> | ||
| ) => { | ||
| try { | ||
| const method = req.method; | ||
|
|
||
| if (method === "GET") { | ||
| res.setHeader("Cache-Control", getCacheControlHeader("revalidate")); | ||
|
|
||
| const { address, from } = req.query; | ||
|
|
||
| if (!!address && !Array.isArray(address) && isAddress(address)) { | ||
| const bondingManagerAddress = await getBondingManagerAddress(); | ||
|
|
||
| const contract = { | ||
| address: bondingManagerAddress, | ||
| abi: bondingManager, | ||
| } as const; | ||
|
|
||
| // [bondedAmount, fees, delegateAddress, delegatedAmount, startRound, | ||
| // lastClaimRound, nextUnbondingLockId] | ||
| const delegator = await l2PublicClient.readContract({ | ||
| ...contract, | ||
| functionName: "getDelegator", | ||
| args: [address], | ||
| }); | ||
| const delegateAddress = delegator[2]; | ||
| const end = Number(delegator[6]); | ||
|
|
||
| // Ids are sequential and never reused, so anything the subgraph has not | ||
| // seen sits above its highest known id. Always cover the newest ids, | ||
| // whatever `from` asks for, and never read more than MAX_SCAN of them. | ||
| const requested = Number(Array.isArray(from) ? from[0] : from); | ||
| const start = Math.max( | ||
| 0, | ||
| Number.isInteger(requested) ? requested : 0, | ||
| end - MAX_SCAN | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const results = await l2PublicClient.multicall({ | ||
| allowFailure: false, | ||
| contracts: Array.from( | ||
| { length: Math.max(0, end - start) }, | ||
| (_, i) => ({ | ||
| ...contract, | ||
| functionName: "getDelegatorUnbondingLock" as const, | ||
| args: [address, BigInt(start + i)] as const, | ||
| }) | ||
| ), | ||
| }); | ||
|
|
||
| // Withdrawing and rebonding both delete the lock, so a non-zero amount | ||
| // is what makes one open. | ||
| const locks = results | ||
| .map(([amount, withdrawRound], i) => ({ | ||
| amount, | ||
| withdrawRound, | ||
| id: start + i, | ||
| })) | ||
| .filter(({ amount }) => amount > 0n) | ||
| .map(({ amount, withdrawRound, id }) => ({ | ||
| id: `${address.toLowerCase()}-${id}`, | ||
| unbondingLockId: id, | ||
| amount: formatEther(amount), | ||
| withdrawRound: withdrawRound.toString(), | ||
| delegate: { id: delegateAddress.toLowerCase() }, | ||
| })); | ||
|
|
||
| return res.status(200).json({ locks }); | ||
| } else { | ||
| return badRequest(res, "Invalid address format"); | ||
| } | ||
| } | ||
|
|
||
| return methodNotAllowed(res, method ?? "unknown", ["GET"]); | ||
| } catch (err) { | ||
| return internalError(res, err); | ||
| } | ||
| }; | ||
|
|
||
| export default handler; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.