> For the complete documentation index, see [llms.txt](https://docs.bitbadges.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bitbadges.io/token-standard/learn/balance-system.md).

# Balances

The Balance system in BitBadges is designed to represent ownership of tokens across different IDs and time ranges. Ownership times are a new concept to BitBadges allowing you to set that someone owns a token during a specific time but not other times.

## Balance Interface

```typescript
export interface Balance<T extends NumberType> {
    amount: T;
    tokenIds: UintRange<T>[];
    ownershipTimes: UintRange<T>[];
}
```

* `amount`: The quantity of tokens owned
* `tokenIds`: An array of ID ranges representing the tokens owned
* `ownershipTimes`: An array of time ranges during which the tokens are owned

## Interpreting Balances

When interpreting balances, it's crucial to understand that multiple ranges of token IDs and ownership times within a single Balance structure represent all possible combinations.

### Interpretation Algorithm

```javascript
for (balance of balances) {
    for (tokenIdRange of balance.tokenIds) {
        for (ownershipTimeRange of balance.ownershipTimes) {
            // User owns x(balance.amount) of (tokenIdRange) for the times (ownershipTimeRange)
        }
    }
}
```

### Example

Consider the following balance:

```json
{
    "amount": 1,
    "tokenIds": [
        { "start": 1, "end": 10 },
        { "start": 20, "end": 30 }
    ],
    "ownershipTimes": [
        { "start": 20, "end": 50 },
        { "start": 100, "end": 200 }
    ]
}
```

This balance expands to:

1. 1x of IDs 1-10 from times 20-50
2. 1x of IDs 1-10 from times 100-200
3. 1x of IDs 20-30 from times 20-50
4. 1x of IDs 20-30 from times 100-200

## Balance Subtraction

When subtracting balances, you may need to represent the result as multiple Balance objects. For example, if we subtract the first set of balances from the example above (1x of IDs 1-10 from times 20-50), the result would be:

```json
[
    {
        "amount": 1,
        "tokenIds": [
            { "start": 1, "end": 10 },
            { "start": 20, "end": 30 }
        ],
        "ownershipTimes": [{ "start": 100, "end": 200 }]
    },
    {
        "amount": 1,
        "tokenIds": [{ "start": 20, "end": 30 }],
        "ownershipTimes": [{ "start": 20, "end": 50 }]
    }
]
```

## Handling Duplicates

When duplicate token IDs are specified in balances, they are combined and their amounts are added. For example:

```json
{
    "amount": 1,
    "tokenIds": [
        { "start": 1, "end": 10 },
        { "start": 1, "end": 10 }
    ],
    "ownershipTimes": [{ "start": 100, "end": 200 }]
}
```

This is equivalent to and will be treated as:

```json
{
    "amount": 2,
    "tokenIds": [{ "start": 1, "end": 10 }],
    "ownershipTimes": [{ "start": 100, "end": 200 }]
}
```

## Best Practices

1. **Efficient Representation**: Try to represent balances in the most compact form possible by combining overlapping ranges
2. **Careful Subtraction**: When subtracting balances, ensure that you correctly split the remaining balances to accurately represent the result
3. **Avoid Duplicates**: While the system handles duplicates by combining them, it's more efficient to represent balances without duplicates in the first place
4. **Time-Aware Operations**: Always consider the time dimension when performing operations on balances, as ownership can vary over time
5. **Range Calculations**: Familiarize yourself with range operations, as they are crucial for correctly manipulating and interpreting balances


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.bitbadges.io/token-standard/learn/balance-system.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
