> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.adid.dev/llms.txt.
> For full documentation content, see https://docs.adid.dev/llms-full.txt.

# Contract Deployment

Smart contracts live at `packages/blockchain/contracts/*.sol` and are deployed with Hardhat. Three target networks are pre-configured:

| Network | `--network` flag | Chain ID | Use                                                             |
| ------- | ---------------- | -------- | --------------------------------------------------------------- |
| Local   | `localhost`      | `31337`  | `npx hardhat node` (in `packages/blockchain/`)                  |
| Testnet | `adi_testnet`    | `99999`  | Public ADI testnet (`https://rpc.ab.testnet.adifoundation.ai/`) |
| Mainnet | `adi_mainnet`    | `8400`   | Production ADI mainnet                                          |

#### 14.7.1 Deploy commands

```bash
cd packages/blockchain

# 1. Compile
npx hardhat compile

# 2. Deploy to local node
npx hardhat node &                                          # in another terminal
npx hardhat run scripts/deploy.ts --network localhost

# 3. Deploy to testnet
PRIVATE_KEY=<deployer-pk> \
ADI_TESTNET_RPC=https://rpc.ab.testnet.adifoundation.ai/ \
npx hardhat run scripts/deploy.ts --network adi_testnet

# 4. Deploy to mainnet
PRIVATE_KEY=<deployer-pk> \
ADI_MAINNET_RPC=https://rpc.ab.mainnet.adifoundation.ai/ \
npx hardhat run scripts/deploy.ts --network adi_mainnet
```

The deploy script writes the deployed addresses to `deployments/<network>.json`. Copy them into your API env (`DID_REGISTRY_ADDRESS`, `SCHEMA_REGISTRY_ADDRESS`, etc.) before restarting the API.

#### 14.7.2 Post-deploy steps

1. **Verify contracts on the explorer** (if the ADI explorer supports verification): `npx hardhat verify --network adi_testnet 0xAddr <constructor-args>`.
2. **Initialise registries** — some registries require an `initialize(adminAddress, ...)` call after deployment. Confirm in `scripts/deploy.ts` that this is run automatically. 3. **Update API env** with all five addresses.
3. **Run a smoke test:** `curl https://adid.dev/api/v1/dids` after restart should return `200`.

#### 14.7.3 Upgrade strategy

The contracts use OpenZeppelin's `ReentrancyGuard` and access-control patterns; they are **not** UUPS-upgradeable in the current revision. Upgrades require a redeploy + data migration. Plan:

1. Deploy v2 contracts.
2. Migrate state via custom script (read events, replay into new registry).
3. Update API env atomically.
4. Sunset v1 by removing API references; chain state remains historically queryable.

> **VERIFY:** confirm no proxy pattern is in use.

***