Skip to content
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

Fix fetchActions #844

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,26 +805,28 @@ async function fetchActions(
let actionsList: { actions: string[][]; hash: string }[] = [];

fetchedActions.forEach((fetchedAction) => {
const { actionData } = fetchedAction;
let { actionData } = fetchedAction;
let latestActionsHash = Field(fetchedAction.actionState.actionStateTwo);
let actionState = Field(fetchedAction.actionState.actionStateOne);

if (actionData.length === 0)
throw new Error(
`No action data was found for the account ${publicKey} with the latest action state ${actionState}`
);


actionData.reverse();
let { accountUpdateId: currentAccountUpdateId } = actionData[0];
let currentActionList: string[][] = [];



actionData.forEach((action, i) => {
const { accountUpdateId, data } = action;
const isLastAction = i === actionData.length - 1;
const isSameAccountUpdate = accountUpdateId === currentAccountUpdateId;

if (isSameAccountUpdate && !isLastAction) {
currentActionList.push(data);
currentAccountUpdateId = accountUpdateId;
return;
} else if (isSameAccountUpdate && isLastAction) {
currentActionList.push(data);
Expand All @@ -837,7 +839,7 @@ async function fetchActions(
actions: currentActionList,
hash: Ledger.fieldToBase58(Field(latestActionsHash)),
});
currentAccountUpdateId = accountUpdateId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure we want to remove this? if yes, why? in this else branch, we don't have accountUpdateId === currentAccountUpdateId so the line is not redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure, because the code after the end of this branch will also perform this operation.
You can see the code comments below:

fetchedActions.forEach((fetchedAction) => {
    const { actionData } = fetchedAction;
    let latestActionsHash = Field(fetchedAction.actionState.actionStateTwo);
    let actionState = Field(fetchedAction.actionState.actionStateOne);

    if (actionData.length === 0)
      throw new Error(
        `No action data was found for the account ${publicKey} with the latest action state ${actionState}`
      );

    let { accountUpdateId: currentAccountUpdateId } = actionData[0];
    let currentActionList: string[][] = [];

    actionData.forEach((action, i) => {
      const { accountUpdateId, data } = action;
      const isLastAction = i === actionData.length - 1;
      const isSameAccountUpdate = accountUpdateId === currentAccountUpdateId;

      if (isSameAccountUpdate && !isLastAction) {
        currentActionList.push(data);
        currentAccountUpdateId = accountUpdateId;
        return;
      } else if (isSameAccountUpdate && isLastAction) {
        currentActionList.push(data);
      } else if (!isSameAccountUpdate && isLastAction) {
        // branch 3
        latestActionsHash = processActionData(
          currentActionList,
          latestActionsHash
        );
        actionsList.push({
          actions: currentActionList,
          hash: Ledger.fieldToBase58(Field(latestActionsHash)),
        });
        // The following line of code is redundant
        currentAccountUpdateId = accountUpdateId;
        currentActionList = [data];
      }

      latestActionsHash = processActionData(
        currentActionList,
        latestActionsHash
      );
      actionsList.push({
        actions: currentActionList,
        hash: Ledger.fieldToBase58(Field(latestActionsHash)),
      });
      // After the above branch 3 is executed, the currentAccoutUpdateId will also be updated here
      currentAccountUpdateId = accountUpdateId;
      currentActionList = [data];
    });

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!


currentActionList = [data];
}

Expand Down