> ## Documentation Index
> Fetch the complete documentation index at: https://stackrlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Rollup State

> Basis of Rollup for compute

Stackr-JS comes with a base `RollupState` base class that can be extended to create a custom rollup.

```ts theme={null}
import { RollupState } from "@stackr/stackr-js/execution";

export class FastestUTXORollup extends RollupState<
  StateVariable,
  StateTransport
> {}
```

<Info>
  You need to provide the `StateVariable` and `StateTransport` types to the
  `RollupState` class which you defined previously.
</Info>

### Extending RollupState

You need to extend the `RollupState` class and implement 4 methods:

### `constructor`

You need to call the super constructor with the `StateVariable` type for it to be able to use the defined state

```ts theme={null}
constructor(initialCount: StateVariable) {
    super(initialCount);
  }
```

### `createTransport`

This method is used to create a transport object that will be used to wrap a StateVariable.

<CodeGroup>
  ```ts simpler theme={null}
  createTransport(state: StateVariable): StateTransport {
      return { currentCount: state };
    }

  ```

  ```ts simple theme={null}
  createTransport(accounts: accountsVariable): MerkleTreeTransport {
      const newTree = new BetterMerkleTree(state);
      return newTree;
    }
  ```
</CodeGroup>

### `getState`

This is the opposite of `createTransport` and is used to extract the state from a transport object.

<CodeGroup>
  ```ts simpler theme={null}
  getState(transport: StateTransport): StateVariable {
      return transport.currentCount;
    }
  ```

  ```ts simple theme={null}
  getState(transport: MerkleTreeTransport): accountsVariable {
      return transport.leaves;
    }
  ```
</CodeGroup>

### `calculateRoot`

This method is used to calculate the state root of the rollup.
The idea behind is that the rollups must converge into single state root that is succint representation of the state of the rollup.

<CodeGroup>
  ```ts simpler theme={null}
  calculateRoot(): ethers.BytesLike {
      return ethers.solidityPackedKeccak256(
        ["uint256"],
        [this.transport.currentCount]
      );
    }
  ```

  ```ts simple theme={null}
  calculateRoot(): ethers.BytesLike {
      return this.transport.merkleTree.getHexRoot();
    }
  ```
</CodeGroup>

### Final RollupState

Overall the final rollup state could look like this:

```ts theme={null}
export class CounterRollup extends RollupState<StateVariable, StateTransport> {
  constructor(count: StateVariable) {
    super(count);
  }

  createTransport(state: StateVariable): StateTransport {
    return { currentCount: state };
  }

  getState(): StateVariable {
    return this.transport.currentCount;
  }

  calculateRoot(): ethers.BytesLike {
    return ethers.solidityPackedKeccak256(
      ["uint256"],
      [this.transport.currentCount]
    );
  }
}
```
