# Prepare Provider

> Source: https://taquito.io/docs/24.2.0/prepare

Before operations are _forged_, _signed_, and then _injected_, they first need to go through a _prepare_ step.

In Taquito, the act of preparing an operation is to create the Operation Object and the counter in one single object that we name `PreparedOperation`.

An example of `PreparedOperation` object for a `ballot` operation looks something like this:
```typescript
{
  opOb: {
    branch: 'test_block_hash',
    contents: [
      {
        kind: 'ballot',
        ballot: 'yay',
        period: 103,
        proposal: 'PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg',
      },
    ],
    protocol: 'test_protocol',
  },
  counter: 0,
}
```

Having access to the `PreparedOperation` object offers a few benefits, a few of them being:
- The ability to retrieve information about the operation before injecting (operation hash, etc)
- The ability to simulate an operation before injecting

The `PrepareProvider` class affords extension and control to users when preparing operations while also promoting modularity in Taquito as a design principle.

## Usage example

### Individual Operations
The `PrepareProvider` is accessible via the `TezosToolkit` class:
```typescript
// const Tezos = new TezosToolkit('RPC_ENDPOINT');

const prepared = await Tezos.prepare.transaction({
  to: 'tz1QZ6KY7d3BuZDT1d19dUxoQrtFPN2QJ3hn',
  amount: 5
});
```

Let's break that transaction prepare call down.

The interface of the `transaction` member method is as follows:
```typescript
transaction(params: TransferParams , source?: string): Promise<PreparedOperation>;
```

- `params` is the Transaction operation parameters. In this case, the required properties are `to` and `amount`.
- `source` is the source public key hash if you do wish to override the source. This parameter is optional by design. If you do not wish to override it, the source will be grabbed from the `Context`.

### Batch Operations
`PrepareProvider` also has support for batch operations:
```typescript
const prepared = await Tezos.prepare.batch([
  {
    kind: OpKind.TRANSACTION,
    to: 'tz1QZ6KY7d3BuZDT1d19dUxoQrtFPN2QJ3hn',
    amount: 2,
  },
  {
    kind: OpKind.TRANSACTION,
    to: 'tz1QZ6KY7d3BuZDT1d19dUxoQrtFPN2QJ3hn',
    amount: 2,
  },
]);
```
The parameters for each object in the array are the required parameters for each respective operation, with the added `kind` property that denotes the operation kind. Users can also utilize `OpKind` which is an enum that holds operation kind values.

### Contract Calls
Users are also able to utilize the `PrepareProvider` to prepare contract calls:
```typescript
const contractAbs = await Tezos.contract.at(contractAddress);
const method = await contractAbs.methodsObject.increment(1);
const prepared = await Tezos.prepare.contractCall(method);
```

## Conversion methods

We've also added a couple utility methods to convert a `PreparedOperation` into objects that can be consumed by the `forger` as well as the `preapplyOperations` method.

### `toPreapply()`
The `toPreapply()` method converts a `PreparedOperation` object into an entity that is consumable by the `preapplyOperations()` method in the RPC package (i.e. `PreapplyParams` type object).

#### Example
```typescript
const preparedTransferOp = await Tezos.prepare.transaction({
    amount: 1,
    to: 'tz1ZfrERcALBwmAqwonRXYVQBDT9BjNjBHJu'
});
const params = await Tezos.prepare.toPreapply(preparedTransferOp);
const preapplyOp = await Tezos.rpc.preapplyOperations(params);
```

### `toForge()`
The `toForge()` method converts a `PreparedOperation` into an object that can be passed into the `forge` method (i.e. `ForgeParams` type object)

#### Example

```typescript
const preparedTransfer = await Tezos.prepare.transaction({
  amount: 1,
  to: 'tz1ZfrERcALBwmAqwonRXYVQBDT9BjNjBHJu'
});
const params = Tezos.prepare.toForge(preparedTransfer);
const forgedBytes = await forger.forge(params);
```
