diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index 0eb629c045..cd5c2028bb 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -749,7 +749,7 @@ async function fetchActions( ) { if (!graphqlEndpoint) throw new Error( - 'fetchEvents: Specified GraphQL endpoint is undefined. Please specify a valid endpoint.' + 'fetchActions: Specified GraphQL endpoint is undefined. Please specify a valid endpoint.' ); const { publicKey, actionStates, tokenId } = accountInfo; let [response, error] = await makeGraphqlRequest( @@ -824,6 +824,7 @@ async function fetchActions( if (isSameAccountUpdate && !isLastAction) { currentActionList.push(data); + currentAccountUpdateId = accountUpdateId; return; } else if (isSameAccountUpdate && isLastAction) { currentActionList.push(data); diff --git a/src/lib/mina.ts b/src/lib/mina.ts index d96feef518..478cb23e69 100644 --- a/src/lib/mina.ts +++ b/src/lib/mina.ts @@ -47,6 +47,7 @@ export { accountCreationFee, sendTransaction, fetchEvents, + fetchActions, getActions, FeePayerSpec, ActionStates, @@ -332,6 +333,11 @@ interface Mina { tokenId?: Field, filterOptions?: Fetch.EventActionFilterOptions ) => ReturnType; + fetchActions: ( + publicKey: PublicKey, + actionStates?: ActionStates, + tokenId?: Field + ) => ReturnType; getActions: ( publicKey: PublicKey, actionStates?: ActionStates, @@ -573,6 +579,13 @@ function LocalBlockchain({ async fetchEvents(publicKey: PublicKey, tokenId: Field = TokenId.default) { return events?.[publicKey.toBase58()]?.[TokenId.toBase58(tokenId)] ?? []; }, + async fetchActions( + publicKey: PublicKey, + actionStates?: ActionStates, + tokenId: Field = TokenId.default + ) { + return this.getActions(publicKey, actionStates, tokenId); + }, getActions( publicKey: PublicKey, actionStates?: ActionStates, @@ -840,6 +853,33 @@ function Network(input: { mina: string; archive: string } | string): Mina { filterOptions ); }, + async fetchActions( + publicKey: PublicKey, + actionStates?: ActionStates, + tokenId: Field = TokenId.default + ) { + let pubKey = publicKey.toBase58(); + let token = TokenId.toBase58(tokenId); + let { fromActionState, endActionState } = actionStates ?? {}; + let fromActionStateBase58 = fromActionState + ? fromActionState.toString() + : undefined; + let endActionStateBase58 = endActionState + ? endActionState.toString() + : undefined; + + return Fetch.fetchActions( + { + publicKey: pubKey, + actionStates: { + fromActionState: fromActionStateBase58, + endActionState: endActionStateBase58, + }, + tokenId: token, + }, + archiveEndpoint + ); + }, getActions( publicKey: PublicKey, actionStates?: ActionStates, @@ -941,6 +981,13 @@ let activeInstance: Mina = { fetchEvents(_publicKey: PublicKey, _tokenId: Field = TokenId.default) { throw Error('must call Mina.setActiveInstance first'); }, + fetchActions( + _publicKey: PublicKey, + _actionStates?: ActionStates, + _tokenId: Field = TokenId.default + ) { + throw Error('must call Mina.setActiveInstance first'); + }, getActions( _publicKey: PublicKey, _actionStates?: ActionStates, @@ -1088,6 +1135,17 @@ async function fetchEvents( return await activeInstance.fetchEvents(publicKey, tokenId, filterOptions); } +/** + * @return A list of emitted sequencing actions associated to the given public key. + */ +async function fetchActions( + publicKey: PublicKey, + actionStates: ActionStates, + tokenId?: Field +) { + return await activeInstance.fetchActions(publicKey, actionStates, tokenId); +} + /** * @return A list of emitted sequencing actions associated to the given public key. */ diff --git a/src/lib/zkapp.ts b/src/lib/zkapp.ts index 799b30849d..a08e5c6997 100644 --- a/src/lib/zkapp.ts +++ b/src/lib/zkapp.ts @@ -1285,6 +1285,21 @@ type ReducerReturn = { fromActionState?: Field; endActionState?: Field; }): Action[][]; + /** + * Fetches the list of previously emitted {@link Action}s by zkapp {@link SmartContract}. + * ```ts + * let pendingActions = await zkapp.reducer.fetchActions({ + * fromActionState: actionsHash, + * }); + * ``` + */ + fetchActions({ + fromActionState, + endActionState, + }: { + fromActionState?: Field; + endActionState?: Field; + }): Promise; }; function getReducer(contract: SmartContract): ReducerReturn { @@ -1403,6 +1418,38 @@ Use the optional \`maxTransactionsWithActions\` argument to increase this number ); }); + return actionsForAccount; + }, + async fetchActions({ + fromActionState, + endActionState, + }: { + fromActionState?: Field; + endActionState?: Field; + }): Promise { + let actionsForAccount: A[][] = []; + let res = await Mina.fetchActions( + contract.address, + { + fromActionState, + endActionState, + }, + contract.self.tokenId + ); + if(res.hasOwnProperty('error')) { + throw Error(JSON.stringify(res)); + } + + actionsForAccount = (res as { hash: string; actions: string[][] }[]).map( + (event: { hash: string; actions: string[][] }) => + // putting our string-Fields back into the original action type + event.actions.map((action: string[]) => + (reducer.actionType as ProvablePure).fromFields( + action.map((fieldAsString: string) => Field(fieldAsString)) + ) + ) + ); + return actionsForAccount; }, };