class · Browser and Node

# SpendLedger

`@toip-tools/mandates` · [View source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/mandates/src/ledger.ts#L23)

Import:

```ts
import { SpendLedger } from "@toip-tools/mandates";
```

## Constructor

Constructor:

```ts
new (saved?: SpendLedgerState): SpendLedger;
```

### Parameters

- **`saved` (optional)**: `SpendLedgerState | undefined`

### Returns

`SpendLedger`

## Methods

### `spent`

Spent under `mandate` in each period: rolling windows ending at `now` (ms); `total` is everything.

Signature:

```ts
spent(mandate: string, now: number): Record<Period, Units>;
```

#### Parameters

- **`mandate` (required)**: `string`
- **`now` (required)**: `number`

#### Returns

`Record<Period, string>`

### `reserve`

Counts `spend` against `mandate` from `at`. Call it when the action is allowed, before anything moves.

Signature:

```ts
reserve(mandate: string, actionId: string, spend: Units, at: number): void;
```

#### Parameters

- **`mandate` (required)**: `string`
- **`actionId` (required)**: `string`
- **`spend` (required)**: `string`
- **`at` (required)**: `number`

#### Returns

`void`

### `tryReserve`

Checks the mandate's own budgets and reserves in one synchronous step, so two actions racing for the last of a budget can't both fit. Returns the budgets it would exceed (and reserves nothing), or \[\]. With `actionType`, budgets scoped by `when` to other types are skipped.

Signature:

```ts
tryReserve(mandate: Mandate, actionId: string, spend: Units, at: number, actionType?: string): Reason[];
```

#### Parameters

- **`mandate` (required)**: `Mandate`
- **`actionId` (required)**: `string`
- **`spend` (required)**: `string`
- **`at` (required)**: `number`
- **`actionType` (optional)**: `string | undefined`

#### Returns

`Reason[]`

### `release`

Undoes a reservation: the action failed before anything left the wallet.

Signature:

```ts
release(actionId: string): void;
```

#### Parameters

- **`actionId` (required)**: `string`

#### Returns

`void`

### `refund`

A refund gives budget back (agents decision 4): all of it, or `units` of it.

Signature:

```ts
refund(actionId: string, units?: Units): void;
```

#### Parameters

- **`actionId` (required)**: `string`
- **`units` (optional)**: `string | undefined`

#### Returns

`void`

### `markApprovalUsed`

Signature:

```ts
markApprovalUsed(id: string): void;
```

#### Parameters

- **`id` (required)**: `string`

#### Returns

`void`

### `approvalUsed`

Signature:

```ts
approvalUsed(id: string): boolean;
```

#### Parameters

- **`id` (required)**: `string`

#### Returns

`boolean`

### `save`

Signature:

```ts
save(): SpendLedgerState;
```

No parameters.

#### Returns

`SpendLedgerState`

[Usage guide](https://toip.tools/toolkit/mandates/#usage) · [Error handling](https://toip.tools/toolkit/mandates/#errors) · [All exports](https://toip.tools/toolkit/mandates/api/)

---

This is the Markdown version of https://toip.tools/toolkit/mandates/api/SpendLedger/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
