# Referrals

Integrator guide for using a referral code and checking/claiming referral fees.

## Get a code

Referral codes are issued by Liquid Labs. Request your code from the team on Discord.

## Ref code format

`refCode` is a `bytes32` hash, not a plain text string.

It should be the keccak256 hash of your issued code text (for example, hash `"mycode"` and pass that `bytes32` value on-chain).

Example in ethers:

```typescript
import { keccak256, toUtf8Bytes } from "ethers";

const refCode = keccak256(toUtf8Bytes("your-issued-code"));
```

## Use your code in swaps

Router and pool both support the referral swap overload:

```solidity
swap(tokenIn, tokenOut, amountIn, minAmountOut, refCode)
```

* Keep your normal execution flow the same (`minAmountOut`, approvals, quote/estimate checks).
* Use your issued `refCode` as the 5th parameter.

## Check and claim referral fees

All referral functions are available on the **router** — you don't need to know pool addresses. Pass the token pair and the router resolves the pool automatically.

### Via the router (recommended)

```solidity
function getRefCodeOwner(address tokenA, address tokenB, bytes32 refCode)
    external view returns (address)

function getRefClaimable(address tokenA, address tokenB, bytes32 refCode)
    external view returns (uint256 amount0, uint256 amount1)

function getRefCodes(address tokenA, address tokenB)
    external view returns (bytes32[] memory refCodes, address[] memory owners)

function claimRefFees(address tokenA, address tokenB, bytes32 refCode)
    external returns (uint256 amount0, uint256 amount1)

function distributeRefFees() external
```

* `getRefCodeOwner` — verify which wallet can claim for a code.
* `getRefClaimable` — check currently claimable amounts per pool token.
* `getRefCodes` — list all registered ref codes and their owners for a pair.
* `claimRefFees` — claim accrued fees for a specific ref code. Must be called by the configured claim wallet for that code.
* `distributeRefFees` — batch-distributes all pending referral fees across all pools. Iterates over every registered ref code and pays out accrued fees to each code's configured claim wallet. Anyone can call this.

### Via pool contracts (alternative)

The same functions are available directly on pool contracts (without the `tokenA`/`tokenB` pair parameters). See the [Contract Reference](https://docs.liqd.ag/api-reference#direct-pool-interface) for pool-level signatures.
