Getting the cumulative Mapping of a sequence of Transactions

In appendTransaction(transactions, oldState, newState) how do I get the cumulative Mapping of transactions?

Is the following right?

const cumulativeMapping: Mapping = transactions[0].mapping;
if (transactions.length > 1) {
  for (let i = 1; i < transactions.length; i++) {
    cumulativeMapping.appendMapping(transactions[i].mapping)
  }
}
1 Like

I’m no expert, but it might be easier to use filterTransaction since it receives transactions one at a time.

Unfortunately I don’t know the answer to your question though.

That will mutate transactions[0].mapping, which can be a problem. So I’d recommend starting with an empty mapping and adding them all to it.

Thank you, Marijn.

I suspected that and I looked for a static Mapping.empty(), but now I realize that new Mapping() is enough.

Here’s the new implementation:

const cumulativeMapping: Mapping = transactions.reduce(
        (acc, tr) => {
          acc.appendMapping(tr.mapping)
          return acc
        },
        new Mapping(),
      );