> For the complete documentation index, see [llms.txt](https://docs.gnoswap.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gnoswap.io/contracts/pool/swap.gno.md).

# swap.gno

{% embed url="<https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/v1/swap.gno>" %}

## DrySwap

```go
func DrySwap(
	token0Path string,
	token1Path string,
	fee uint32,
	zeroForOne bool,
	amountSpecified string,
	sqrtPriceLimitX96 string
) (string, string, error)
```

DrySwap simulates a swap without executing it, returning the expected output.

This is a read-only operation that does not modify pool state. Used by router for multi-hop swap simulations and by clients for price quotes.

#### Parameters

| Name                | Type   | Description                                                                              |
| ------------------- | ------ | ---------------------------------------------------------------------------------------- |
| `token0Path`        | string | Path of the first token in the pool pair.                                                |
| `token1Path`        | string | Path of the second token in the pool pair.                                               |
| `fee`               | uint32 | Fee tier identifying the pool.                                                           |
| `zeroForOne`        | bool   | True to sell token0 for token1; false to sell token1 for token0.                         |
| `amountSpecified`   | string | Decimal signed amount; positive requests exact input and negative requests exact output. |
| `sqrtPriceLimitX96` | string | Price boundary in Q64.96 fixed-point form at which the simulation must stop.             |

#### Return Values

| Name      | Type   | Description                                                                                          |
| --------- | ------ | ---------------------------------------------------------------------------------------------------- |
| `amount0` | string | Simulated signed token0 delta, as a decimal string; "0" on an unsuccessful simulation.               |
| `amount1` | string | Simulated signed token1 delta, as a decimal string; "0" on an unsuccessful simulation.               |
| `error`   | error  | Nil when the simulation succeeds; non-nil when validation, computation, or pool-balance checks fail. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/proxy.gno#L346)

## SetSwapEndHook

```go
func SetSwapEndHook(
	cur realm,
	hook func(realm, string) error
)
```

SetSwapEndHook sets the hook to be called at the end of a swap.

#### Parameters

| Name   | Type                      | Description                                                                                                             |
| ------ | ------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `cur`  | realm                     | Current realm context; callers use cross(cur) when crossing into this realm.                                            |
| `hook` | func(realm, string) error | Callback invoked with the current realm and pool path after swap settlement; its error is propagated to abort the swap. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/proxy.gno#L364)

## SetSwapStartHook

```go
func SetSwapStartHook(
	cur realm,
	hook func(realm, string, int64)
)
```

SetSwapStartHook sets the hook to be called at the start of a swap.

#### Parameters

| Name   | Type                       | Description                                                                                     |
| ------ | -------------------------- | ----------------------------------------------------------------------------------------------- |
| `cur`  | realm                      | Current realm context; callers use cross(cur) when crossing into this realm.                    |
| `hook` | func(realm, string, int64) | Callback invoked with the current realm, pool path, and swap timestamp before swap computation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/proxy.gno#L375)

## SetTickCrossHook

```go
func SetTickCrossHook(
	cur realm,
	hook func(realm, string, int32, bool, int64)
)
```

SetTickCrossHook sets the hook to be called when a tick is crossed during a swap.

#### Parameters

| Name   | Type                                    | Description                                                                                                  |
| ------ | --------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `cur`  | realm                                   | Current realm context; callers use cross(cur) when crossing into this realm.                                 |
| `hook` | func(realm, string, int32, bool, int64) | Callback invoked with the current realm, pool path, crossed tick index, swap direction, and block timestamp. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/proxy.gno#L386)

## Swap

```go
func Swap(
	cur realm,
	token0Path string,
	token1Path string,
	fee uint32,
	recipient address,
	zeroForOne bool,
	amountSpecified string,
	sqrtPriceLimitX96 string,
	swapCallback func(realm, int64, int64, *CallbackMarker) error
) (string, string)
```

Swap executes a token swap in the pool.

> **Halt check:** reverts while the Pool halt scope is active.

#### Parameters

| Name                | Type                                              | Description                                                                                                                                                         |
| ------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cur`               | realm                                             | Current realm context; callers use cross(cur) when crossing into this realm.                                                                                        |
| `token0Path`        | string                                            | Path of the first token in the pool pair.                                                                                                                           |
| `token1Path`        | string                                            | Path of the second token in the pool pair.                                                                                                                          |
| `fee`               | uint32                                            | Fee tier identifying the pool.                                                                                                                                      |
| `recipient`         | address                                           | Address receiving output tokens.                                                                                                                                    |
| `zeroForOne`        | bool                                              | True to sell token0 for token1; false to sell token1 for token0.                                                                                                    |
| `amountSpecified`   | string                                            | Decimal signed amount; positive requests exact input and negative requests exact output.                                                                            |
| `sqrtPriceLimitX96` | string                                            | Price boundary in Q64.96 fixed-point form at which the swap must stop.                                                                                              |
| `swapCallback`      | func(realm, int64, int64, \*CallbackMarker) error | Callback invoked with the current realm, signed token deltas, and a callback marker; it must settle the input token and return a non-nil error to abort settlement. |

#### Return Values

| Name      | Type   | Description                                            |
| --------- | ------ | ------------------------------------------------------ |
| `amount0` | string | Signed token0 delta for the swap, as a decimal string. |
| `amount1` | string | Signed token1 delta for the swap, as a decimal string. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/pool/proxy.gno#L298)
