> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xyzchain.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Contract Discovery

> Find and inspect deployed contracts

# Contract Discovery

Find deployed contracts and inspect their details with `xyz program list` and `xyz program info`.

## List Contracts

List all contracts deployed from a specific code ID:

```bash theme={null}
xyz program list <code-id>
```

### Example

```bash theme={null}
xyz program list 1
```

Output:

```
Contracts from Code ID 1:

ADDRESS                                           LABEL
xyz1contract1abc123...                            My Token
xyz1contract2def456...                            Test Token
xyz1contract3ghi789...                            Reward Token
```

### Options

| Flag      | Description | Default |
| --------- | ----------- | ------- |
| `--limit` | Max results | 10      |

### With Custom Limit

```bash theme={null}
xyz program list 1 --limit 50
```

## Contract Info

Get detailed information about a specific contract:

```bash theme={null}
xyz program info <contract-address>
```

### Example

```bash theme={null}
xyz program info xyz1contract123...
```

Output:

```
Contract Information

Address:  xyz1contract123...
Code ID:  1
Label:    My Token
Creator:  xyz1creator...
Admin:    xyz1admin...
Created:  Block 12345
```

### Options

| Flag        | Description            |
| ----------- | ---------------------- |
| `--hash`    | Include code hash      |
| `--history` | Show migration history |
| `--json`    | JSON output            |

### With Code Hash

```bash theme={null}
xyz program info xyz1contract... --hash
```

Output:

```
Contract Information

Address:   xyz1contract123...
Code ID:   1
Code Hash: abc123def456...
Label:     My Token
Creator:   xyz1creator...
Admin:     xyz1admin...
Created:   Block 12345
```

### With History

```bash theme={null}
xyz program info xyz1contract... --history
```

Output:

```
Contract Information

Address:  xyz1contract123...
Code ID:  2
Label:    My Token
Creator:  xyz1creator...
Admin:    xyz1admin...
Created:  Block 12345

Migration History:
  Block 12345: Code 1 → Code 2 (upgrade to v2)
  Block 10000: Initial deployment (Code 1)
```

### JSON Output

```bash theme={null}
xyz program info xyz1contract... --json
```

```json theme={null}
{
  "address": "xyz1contract123...",
  "code_id": 1,
  "label": "My Token",
  "creator": "xyz1creator...",
  "admin": "xyz1admin...",
  "created_block": 12345
}
```

## Using xyzd

For more advanced queries:

### List All Codes

```bash theme={null}
xyzd query wasm list-code --node tcp://localhost:26657
```

Output:

```yaml theme={null}
code_infos:
- code_id: "1"
  creator: xyz1creator...
  data_hash: ABC123...
  instantiate_permission:
    permission: Everybody
- code_id: "2"
  creator: xyz1creator...
  ...
```

### Contracts by Creator

```bash theme={null}
xyzd query wasm list-contracts-by-creator xyz1creator... --node tcp://localhost:26657
```

### Contract State (Raw)

```bash theme={null}
xyzd query wasm contract-state all xyz1contract... --node tcp://localhost:26657
```

## Discovery Workflow

### Find Your Contracts

1. List all codes you've uploaded:
   ```bash theme={null}
   xyzd query wasm list-code --node tcp://localhost:26657 | grep -A2 "creator: xyz1youraddress"
   ```

2. List contracts for each code:
   ```bash theme={null}
   xyz program list <code-id>
   ```

3. Get details on specific contract:
   ```bash theme={null}
   xyz program info xyz1contract...
   ```

### Find Contracts by Label

```bash theme={null}
# List all CW20 tokens
xyz program list 1 --limit 100 | grep -i "token"
```

### Export Contract List

```bash theme={null}
# Export to JSON
xyz program list 1 --limit 100 | while read addr label; do
  echo "\"$addr\": \"$label\""
done
```

## Contract Properties

### Admin

The admin can:

* Migrate the contract to new code
* Update the admin
* Clear the admin (make immutable)

```bash theme={null}
xyz program info xyz1contract... | grep Admin
```

* `Admin: xyz1admin...` - Admin can migrate
* `Admin: None - immutable` - No admin, cannot change

### Code ID

Identifies the contract code (bytecode):

* Same code ID = same logic
* Different instances can have different state
* Upgrading changes the code ID

### Creator

The address that deployed the contract (cannot be changed).

## Scripting Examples

### List All Your Contracts

```bash theme={null}
#!/bin/bash
MY_ADDRESS="xyz1myaddress..."

# Get all codes
for code_id in $(xyzd query wasm list-code --node tcp://localhost:26657 -o json | jq -r '.code_infos[].code_id'); do
  echo "=== Code ID: $code_id ==="
  xyz program list $code_id --limit 100
done
```

### Monitor Contract Info

```bash theme={null}
#!/bin/bash
CONTRACT="xyz1contract..."

while true; do
  echo "=== $(date) ==="
  xyz program info $CONTRACT
  sleep 60
done
```

### Verify Contract Code

```bash theme={null}
#!/bin/bash
CONTRACT="xyz1contract..."
EXPECTED_HASH="abc123def456..."

ACTUAL_HASH=$(xyz program info $CONTRACT --hash --json | jq -r '.code_hash')

if [ "$ACTUAL_HASH" = "$EXPECTED_HASH" ]; then
  echo "✓ Code hash verified"
else
  echo "✗ Code hash mismatch!"
  echo "  Expected: $EXPECTED_HASH"
  echo "  Actual:   $ACTUAL_HASH"
fi
```

## Troubleshooting

<AccordionGroup>
  <Accordion title="No contracts found">
    The code ID may have no instances:

    ```bash theme={null}
    # Verify code exists
    xyzd query wasm code 1 --node tcp://localhost:26657
    ```
  </Accordion>

  <Accordion title="Contract not found">
    Verify the address is correct:

    ```bash theme={null}
    # Address should start with xyz1
    # And be the correct length
    ```
  </Accordion>

  <Accordion title="Pagination">
    If you have many contracts, use pagination:

    ```bash theme={null}
    xyz program list 1 --limit 100
    ```
  </Accordion>
</AccordionGroup>
