-
Notifications
You must be signed in to change notification settings - Fork 136
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
Sort actions by tx sequence when available #1917
Changes from all commits
bccfd1c
fa6779c
3b59976
d609844
65c3159
9c40282
b2fc111
05af361
67d9570
5ef0bdc
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 |
---|---|---|
|
@@ -641,9 +641,11 @@ function sendZkapp( | |
* @returns A promise that resolves to an array of objects containing event data, block information and transaction information for the account. | ||
* @throws If the GraphQL request fails or the response is invalid. | ||
* @example | ||
* ```ts | ||
* const accountInfo = { publicKey: 'B62qiwmXrWn7Cok5VhhB3KvCwyZ7NHHstFGbiU5n7m8s2RqqNW1p1wF' }; | ||
* const events = await fetchEvents(accountInfo); | ||
* console.log(events); | ||
* ``` | ||
*/ | ||
async function fetchEvents( | ||
accountInfo: { publicKey: string; tokenId?: string }, | ||
|
@@ -691,10 +693,32 @@ async function fetchEvents( | |
}); | ||
} | ||
|
||
/** | ||
* Fetches account actions for a specified public key and token ID by performing a GraphQL query. | ||
* | ||
* @param accountInfo - An {@link ActionsQueryInputs} containing the public key, and optional query parameters for the actions query | ||
* @param graphqlEndpoint - The GraphQL endpoint to fetch from. Defaults to the configured Mina endpoint. | ||
* | ||
* @returns A promise that resolves to an object containing the final actions hash for the account, and a list of actions | ||
* @throws Will throw an error if the GraphQL endpoint is invalid or if the fetch request fails. | ||
* | ||
* @example | ||
* ```ts | ||
* const accountInfo = { publicKey: 'B62qiwmXrWn7Cok5VhhB3KvCwyZ7NHHstFGbiU5n7m8s2RqqNW1p1wF' }; | ||
* const actionsList = await fetchAccount(accountInfo); | ||
* console.log(actionsList); | ||
* ``` | ||
*/ | ||
async function fetchActions( | ||
accountInfo: ActionsQueryInputs, | ||
graphqlEndpoint = networkConfig.archiveEndpoint | ||
) { | ||
): Promise< | ||
| { | ||
actions: string[][]; | ||
hash: string; | ||
}[] | ||
| { error: FetchError } | ||
> { | ||
if (!graphqlEndpoint) | ||
throw Error( | ||
'fetchActions: Specified GraphQL endpoint is undefined. When using actions, you must set the archive node endpoint in Mina.Network(). Please ensure your Mina.Network() configuration includes an archive node endpoint.' | ||
|
@@ -710,7 +734,26 @@ async function fetchActions( | |
graphqlEndpoint, | ||
networkConfig.archiveFallbackEndpoints | ||
); | ||
if (error) throw Error(error.statusText); | ||
// As of 2025-01-07, minascan is running a version of the node which supports `sequenceNumber` and `zkappAccountUpdateIds` fields | ||
// We could consider removing this fallback since no other nodes are widely used | ||
if (error) { | ||
const originalError = error; | ||
[response, error] = await makeGraphqlRequest<ActionQueryResponse>( | ||
getActionsQuery( | ||
publicKey, | ||
actionStates, | ||
tokenId, | ||
/* _filterOptions= */ undefined, | ||
/* _excludeTransactionInfo= */ true | ||
), | ||
graphqlEndpoint, | ||
networkConfig.archiveFallbackEndpoints | ||
); | ||
if (error) | ||
throw Error( | ||
`ORIGINAL ERROR: ${originalError.statusText} \n\nRETRY ERROR: ${error.statusText}` | ||
); | ||
} | ||
let fetchedActions = response?.data.actions; | ||
if (fetchedActions === undefined) { | ||
return { | ||
|
@@ -757,9 +800,33 @@ export function createActionsList( | |
`No action data was found for the account ${publicKey} with the latest action state ${actionState}` | ||
); | ||
|
||
actionData = actionData.sort((a1, a2) => { | ||
return Number(a1.accountUpdateId) < Number(a2.accountUpdateId) ? -1 : 1; | ||
}); | ||
// DEPRECATED: In case the archive node is running an out-of-date version, best guess is to sort by the account update id | ||
// As of 2025-01-07, minascan is running a version of the node which supports `sequenceNumber` and `zkappAccountUpdateIds` fields | ||
// We could consider removing this fallback since no other nodes are widely used | ||
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.
I would be careful with that and I don't think we should. App developers are currently incentivized to run their own services for that type of stuff, so we will never know what users are exactly using. Imo we should just keep it in there, it doesn't hurt 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.
It's a little sloppy because we will retry on any error. The archive node API responds with But I'll leave it as-is for now. The most common case will be that that the first request succeeds and this case is never hit. |
||
if (!actionData[0].transactionInfo) { | ||
actionData = actionData.sort((a1, a2) => { | ||
return Number(a1.accountUpdateId) - Number(a2.accountUpdateId); | ||
}); | ||
} else { | ||
// sort actions within one block by transaction sequence number and account update sequence | ||
actionData = actionData.sort((a1, a2) => { | ||
const a1TxSequence = a1.transactionInfo!.sequenceNumber; | ||
const a2TxSequence = a2.transactionInfo!.sequenceNumber; | ||
if (a1TxSequence === a2TxSequence) { | ||
const a1AuSequence = | ||
a1.transactionInfo!.zkappAccountUpdateIds.indexOf( | ||
Number(a1.accountUpdateId) | ||
); | ||
const a2AuSequence = | ||
a2.transactionInfo!.zkappAccountUpdateIds.indexOf( | ||
Number(a2.accountUpdateId) | ||
); | ||
return a1AuSequence - a2AuSequence; | ||
} else { | ||
return a1TxSequence - a2TxSequence; | ||
} | ||
}); | ||
} | ||
|
||
// split actions by account update | ||
let actionsByAccountUpdate: string[][][] = []; | ||
|
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.
Rather than retry the query, perhaps we can set the archive node API version in the Mina.Network settings somewhere. Then we could make a different query based on which version we believe we're talking to.
For this specific example, after we get Minascan to update their version, the new query will work in nearly every case, but we ought to prepare for the case that different versions of the API could be running.