# `swap`

Contains the `swapOwner` and `swapOwnerAsync` functions, which execute a Safe transaction to swap an owner of the Safe connected to the [`SafeProvider`](../safeprovider.mdx) or send it to the Safe Transaction Service if it isn't executable.

## Usage

- If the `threshold` of the connected Safe is greater than `1`, it creates the Safe transaction and submits it to the Safe Transaction Service to collect the signatures from the Safe owners.
- If the `threshold` of the connected Safe is `1`, it executes the Safe transaction.

The connected Safe must be already deployed.


<Tabs items={['App.tsx', 'main.tsx']}>
  <Tabs.Tab>
    ```typescript
    import { useUpdateOwners, SwapOwnerVariables } from '@safe-global/safe-react-hooks'
    
    function SwapOwner() {
      const { swap } = useUpdateOwners()
      const {
        swapOwner,
        data,
        // ...
      } = swap

      const swapOwnerParams: SwapOwnerVariables = {
        oldOwnerAddress: '0x...',
        newOwnerAddress: '0x...'
      }

      return (
        <>
          <button onClick={() => swapOwner(swapOwnerParams)}>
            Swap Owner
          </button>
          {data && JSON.stringify(data)}
        </>
      )
    }

    export default SwapOwner
    ```
  </Tabs.Tab>
  <Tabs.Tab>
    ```typescript
    import ReactDOM from 'react-dom/client'
    import { createConfig, SafeProvider } from '@safe-global/safe-react-hooks'
    import { sepolia } from 'viem/chains'
    import App from './App.tsx  '

    const config = createConfig({
      chain: sepolia,
      provider,
      signer,
      safeAddress: '0x...'
    })

    const root = document.getElementById('root')
    
    ReactDOM.createRoot(root).render(
      <SafeProvider config={config}>
        <App />
      </SafeProvider>
    )
    ```
  </Tabs.Tab>
</Tabs>


## Structure

`UseSwapOwnerReturnType`

```typescript
import { UseSwapOwnerReturnType } from '@safe-global/safe-react-hooks'
```

[TanStack Query mutation documentation](https://tanstack.com/query/v5/docs/framework/react/reference/useMutation).

### `swapOwner`

- **Type**: `UseMutateFunction<SafeClientResult, Error, SwapOwnerVariables, unknown>`

Function to swap an owner of the connected Safe.

#### Parameters

`SwapOwnerVariables`

```typescript
import { SwapOwnerVariables } from '@safe-global/safe-react-hooks'
```

Variables to update the threshold.

#### Returns

[`SafeClientResult`](https://github.com/safe-global/safe-core-sdk/blob/main/packages/sdk-starter-kit/src/types.ts#L67-L85)

The result of the transaction in the [`data`](#data) property.

### `swapOwnerAsync`

- **Type**: `UseMutateAsyncFunction<SafeClientResult, Error, SwapOwnerVariables, unknown>`

Asynchronous function to swap an owner of the connected Safe.

#### Parameters

`SwapOwnerVariables`

```typescript
import { SwapOwnerVariables } from '@safe-global/safe-react-hooks'
```

Variables to update the threshold.

#### Returns

[`SafeClientResult`](https://github.com/safe-global/safe-core-sdk/blob/main/packages/sdk-starter-kit/src/types.ts#L67-L85)

The result of the transaction in the [`data`](#data) property.

### `data`

- **Type**: [`SafeClientResult`](https://github.com/safe-global/safe-core-sdk/blob/main/packages/sdk-starter-kit/src/types.ts#L67-L85)
- **Default**: `undefined`

The last successfully resolved data for the mutation.

### `error`

- **Type**: `null | TError`
- **Default**: `null`

The error object for the mutation, if an error was encountered.

### `failureCount`

- **Type**: `number`

The failure count for the mutation.

Incremented every time the mutation fails.

Reset to `0` when the mutation succeeds.

### `failureReason`

- **Type**: `null | TError`

The failure reason for the mutation retry.

Reset to `null` when the mutation succeeds.

### `isError` / `isIdle` / `isPending` / `isSuccess`

- **Type**: `boolean`

The boolean variables derived from `status`.

### `isPaused`

- **Type**: `boolean`

Will be `true` if the mutation has been `paused`.

See [Network Mode](https://tanstack.com/query/v5/docs/framework/react/guides/network-mode) for more information.

### `reset`

- **Type**: `() => void`

A function to clean the mutation internal state (for example, it resets the mutation to its initial state).

### `status`

- **Type**: `'idle' | 'pending' | 'error' | 'success'`

`'idle'` initial status prior to the mutation function executing.

`'pending'` if the mutation is currently executing.

`'error'` if the last mutation attempt resulted in an error.

`'success'` if the last mutation attempt was successful.

### `submittedAt`

- **Type**: `number`
- **Default**: `0`

The timestamp for when the mutation was submitted.

### `variables`

- **Type**: `SwapOwnerVariables`
- **Default**: `undefined`

The `variables` object passed to the mutation function.
