> For the complete documentation index, see [llms.txt](https://docs.dragonswap.app/dragonswap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/dragonswapv2/core/libraries/safecast.md).

# SafeCast

Contains methods for safely casting between types

### Functions[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#functions) <a href="#functions" id="functions"></a>

#### toUint160[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#touint160) <a href="#touint160" id="touint160"></a>

```solidity
  function toUint160(
    uint256 y
  ) internal pure returns (uint160 z)
```

Cast a uint256 to a uint160, revert on overflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters)

| Name | Type    | Description                  |
| ---- | ------- | ---------------------------- |
| `y`  | uint256 | The uint256 to be downcasted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values)

| Name | Type    | Description                              |
| ---- | ------- | ---------------------------------------- |
| `z`  | uint160 | The downcasted integer, now type uint160 |

#### toInt128[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#toint128) <a href="#toint128" id="toint128"></a>

```solidity
  function toInt128(
    int256 y
  ) internal pure returns (int128 z)
```

Cast a int256 to a int128, revert on overflow or underflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters-1)

| Name | Type   | Description                 |
| ---- | ------ | --------------------------- |
| `y`  | int256 | The int256 to be downcasted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values-1)

| Name | Type   | Description                             |
| ---- | ------ | --------------------------------------- |
| `z`  | int128 | The downcasted integer, now type int128 |

#### toInt256[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#toint256) <a href="#toint256" id="toint256"></a>

```solidity
  function toInt256(
    uint256 y
  ) internal pure returns (int256 z)
```

Cast a uint256 to a int256, revert on overflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters-2)

| Name | Type    | Description              |
| ---- | ------- | ------------------------ |
| `y`  | uint256 | The uint256 to be casted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values-2)

| Name | Type   | Description                         |
| ---- | ------ | ----------------------------------- |
| `z`  | int256 | The casted integer, now type int256 |


---

# 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.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/dragonswapv2/core/libraries/safecast.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.
