Skip to content

Commit

Permalink
Merge pull request #843 from Comdex/main
Browse files Browse the repository at this point in the history
Fix Fetch.fetchActions and add new fetchActions method for external use
  • Loading branch information
mitschabaude authored Apr 11, 2023
2 parents 33a9946 + e5de3c4 commit 9c2b94d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -824,6 +824,7 @@ async function fetchActions(

if (isSameAccountUpdate && !isLastAction) {
currentActionList.push(data);
currentAccountUpdateId = accountUpdateId;
return;
} else if (isSameAccountUpdate && isLastAction) {
currentActionList.push(data);
Expand Down
58 changes: 58 additions & 0 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
accountCreationFee,
sendTransaction,
fetchEvents,
fetchActions,
getActions,
FeePayerSpec,
ActionStates,
Expand Down Expand Up @@ -332,6 +333,11 @@ interface Mina {
tokenId?: Field,
filterOptions?: Fetch.EventActionFilterOptions
) => ReturnType<typeof Fetch.fetchEvents>;
fetchActions: (
publicKey: PublicKey,
actionStates?: ActionStates,
tokenId?: Field
) => ReturnType<typeof Fetch.fetchActions>;
getActions: (
publicKey: PublicKey,
actionStates?: ActionStates,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down
47 changes: 47 additions & 0 deletions src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,21 @@ type ReducerReturn<Action> = {
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<Action[][]>;
};

function getReducer<A>(contract: SmartContract): ReducerReturn<A> {
Expand Down Expand Up @@ -1403,6 +1418,38 @@ Use the optional \`maxTransactionsWithActions\` argument to increase this number
);
});

return actionsForAccount;
},
async fetchActions({
fromActionState,
endActionState,
}: {
fromActionState?: Field;
endActionState?: Field;
}): Promise<A[][]> {
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<A>).fromFields(
action.map((fieldAsString: string) => Field(fieldAsString))
)
)
);

return actionsForAccount;
},
};
Expand Down

0 comments on commit 9c2b94d

Please sign in to comment.