# Deployment Guide

The protocol deploys with [Foundry](https://book.getfoundry.sh) scripts from
the `contract/` package. This page covers the full sequence used on ARC
Testnet.

## Environment

```shell
cp .env.example .env
```

| Variable | Description |
| --- | --- |
| `PRIVATE_KEY` | Deployer key (0x-prefixed) |
| `WETH9` | Wrapped-native address (on ARC: USDC `0x3600…0000`) |
| `NATIVE_CURRENCY_LABEL` | Symbol for the NFT position descriptor |
| `RPC_URL` | Target network RPC |

## Deployment order

```
1. V3 suite               DeployUniswapV3.s.sol
2. StableSwap suite       (factory, deployer, LP factory, router, info)
3. BiscottiToken          DeployBiscottiToken.s.sol
4. Farms                  DeployFarms.s.sol
   ├─ MasterChefERC20        (grants MINTER_ROLE if deployer is admin)
   ├─ MasterChefV3
   └─ SmartChefFactory
5. Smart Router           (wires V3 factory + stable factory)
6. RewardDistributor      DeployRewardDistributor.s.sol
7. Coffee pools           via CoffeePoolFactory.createPool
8. Register pools/farms   cast send (below)
9. Fund V3 rewards        masterChefV3.upkeep(amount, duration)
```

## 1. Deploy the V3 suite

:::code-group
```shell [Dry run]
source .env
forge script script/DeployUniswapV3.s.sol --rpc-url $RPC_URL
```

```shell [Broadcast]
source .env
forge script script/DeployUniswapV3.s.sol --rpc-url $RPC_URL --broadcast
```

```shell [Broadcast + verify]
source .env
forge script script/DeployUniswapV3.s.sol \
  --rpc-url $RPC_URL --broadcast \
  --verify --etherscan-api-key $ETHERSCAN_API_KEY
```
:::

Deploys `UniswapV3Factory`, `NonfungibleTokenPositionDescriptor`,
`NonfungiblePositionManager`, `SwapRouter`, `QuoterV2`, `TickLens` and
`UniswapInterfaceMulticall`. Addresses print after a successful broadcast.

## 2–4. Token, farms, distributor

```shell
forge script script/DeployBiscottiToken.s.sol --rpc-url $RPC_URL --broadcast
forge script script/DeployFarms.s.sol         --rpc-url $RPC_URL --broadcast

REWARD_TOKEN=$BSCT \
forge script script/DeployRewardDistributor.s.sol --rpc-url $RPC_URL --broadcast
```

After farms deploy, confirm both MasterChefs hold `MINTER_ROLE` (the script
grants it when the deployer is token admin), then **revoke the deployer's
minter role** — see [Security Notes](/developers/security).

## 5. Create pools & farms

### Stable pool + classic farm

```bash
# Create the pair (A=200, fee=0.04%, admin fee=50%)
cast send $STABLE_FACTORY \
  "createSwapPair(address,address,uint256,uint256,uint256)" \
  $TOKEN_A $TOKEN_B 200 4000000 5000000000 \
  --rpc-url $RPC_URL --private-key $PRIVATE_KEY

# Fetch its LP token
LP=$(cast call $STABLE_FACTORY \
  "getPairInfo(address,address)(address,address,address,address)" \
  $TOKEN_A $TOKEN_B --rpc-url $RPC_URL | awk 'NR==4')

# Register the farm (100 alloc points)
cast send $MASTERCHEF_ERC20 "add(uint256,address,bool)" \
  100 $LP true --rpc-url $RPC_URL --private-key $PRIVATE_KEY
```

### V3 pool farm

```bash
POOL=$(cast call $V3_FACTORY \
  "getPool(address,address,uint24)(address)" \
  $TOKEN_A $TOKEN_B 500 --rpc-url $RPC_URL)

cast send $MASTERCHEF_V3 "add(uint256,address,bool)" \
  100 $POOL true --rpc-url $RPC_URL --private-key $PRIVATE_KEY
```

`add()` deploys the pool's `MasterChefV3LmPool` automatically.

## 6. Fund a V3 reward period

```bash
# 10,000 BSCT over 7 days — MasterChefV3 mints this itself
cast send $MASTERCHEF_V3 "upkeep(uint256,uint256)" \
  10000000000000000000000 604800 \
  --rpc-url $RPC_URL --private-key $PRIVATE_KEY
```

:::warning[One upkeep per period]
Calling `upkeep()` again before the previous period ends mints excess BSCT.
Run it on a keeper schedule, once per period.
:::

## Local development

```shell
anvil
```

```shell
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
WETH9=<mock-weth> \
RPC_URL=http://127.0.0.1:8545 \
forge script script/DeployUniswapV3.s.sol \
  --rpc-url http://127.0.0.1:8545 --broadcast
```

## Record the deployment

Update `contract/deployments/<network>.json` after each deploy — the frontend
and these docs read from it. Current testnet state:
[Contract Addresses](/developers/addresses).

## Verify

```shell
forge build
forge test
# targeted, e.g.:
forge test --match-path test/incentives/RewardDistributor.t.sol
```
