# Tokenization Deployment Guide

## Prerequisites

1. **Hyperledger Fabric Network**: Deployed and accessible
2. **Besu Network (Chain 138)**: Running and accessible
3. **FireFly Instance**: Deployed and configured
4. **Cacti Connectors**: Fabric and Besu connectors configured
5. **SolaceNet**: Capability platform deployed
6. **Indy Network**: Identity ledger deployed (optional but recommended)
7. **HSM Service**: Configured for production (optional for testing)

## Environment Variables

Create a `.env` file:

```bash
# Fabric Configuration
FABRIC_NETWORK=fabric-network
FABRIC_CHANNEL=mychannel
FABRIC_PEER_ADDRESS=peer0.org1.example.com:7051

# Besu Configuration
CHAIN_138_RPC_URL=http://localhost:8545
DEPLOYER_PRIVATE_KEY=0x...
ADMIN_ADDRESS=0x...

# FireFly Configuration
FIREFLY_API_URL=http://localhost:5000
FIREFLY_API_KEY=your-api-key

# Cacti Configuration
CACTI_API_URL=http://localhost:4000
CACTI_FABRIC_CONNECTOR_ID=fabric-connector-1
CACTI_BESU_CONNECTOR_ID=besu-connector-1

# SolaceNet Configuration
SOLACENET_API_URL=http://localhost:3000
SOLACENET_API_KEY=your-api-key

# Indy Configuration
INDY_API_URL=http://localhost:9000
INDY_POOL_NAME=dbis-pool

# Banking Integration (Optional)
SWIFT_API_URL=https://swift-api.example.com
TARGET2_API_URL=https://target2-api.example.com
```

## Deployment Steps

### 1. Deploy Fabric Chaincode

```bash
# Package chaincode
peer chaincode package tokenized-asset.tar.gz \
    --path ./chaincode/tokenized-asset/go \
    --lang golang \
    --label tokenized-asset-v1.0

# Install chaincode
peer chaincode install tokenized-asset.tar.gz

# Instantiate chaincode
peer chaincode instantiate \
    -C mychannel \
    -n tokenized-asset \
    -v 1.0 \
    -c '{"Args":[]}' \
    -P "OR('Org1MSP.member')"

# Repeat for reserve-manager chaincode
peer chaincode package reserve-manager.tar.gz \
    --path ./chaincode/reserve-manager/go \
    --lang golang \
    --label reserve-manager-v1.0

peer chaincode install reserve-manager.tar.gz

peer chaincode instantiate \
    -C mychannel \
    -n reserve-manager \
    -v 1.0 \
    -c '{"Args":[]}' \
    -P "OR('Org1MSP.member')"
```

### 2. Deploy Besu Contracts

```bash
cd smom-dbis-138
chmod +x scripts/deployment/deploy-tokenization.sh
./scripts/deployment/deploy-tokenization.sh
```

This will deploy:
- TokenizedEUR contract
- TokenRegistry contract
- Register initial token

### 3. Configure Cacti Connectors

#### Fabric Connector

```bash
curl -X POST ${CACTI_API_URL}/api/v1/plugins/ledger-connector/fabric \
  -H "Content-Type: application/json" \
  -d '{
    "ledgerId": "fabric-tokenization",
    "networkName": "${FABRIC_NETWORK}",
    "channelName": "mychannel",
    "chaincodeIds": ["tokenized-asset", "reserve-manager"]
  }'
```

#### Besu Connector

```bash
curl -X POST ${CACTI_API_URL}/api/v1/plugins/ledger-connector/besu \
  -H "Content-Type: application/json" \
  -d '{
    "ledgerId": "besu-tokenization",
    "chainId": 138,
    "rpc": {
      "http": "${CHAIN_138_RPC_URL}",
      "ws": "${CHAIN_138_WS_URL}"
    }
  }'
```

### 4. Configure FireFly

Update FireFly configuration:

```yaml
# firefly-config.yaml
blockchain:
  rpc: ${CHAIN_138_RPC_URL}
  chainId: 138
  contracts:
    tokenizedEUR: ${TOKENIZED_EUR_ADDRESS}
    tokenRegistry: ${TOKEN_REGISTRY_ADDRESS}

tokenization:
  workflows:
    mint: true
    transfer: true
    redeem: true
  settlement:
    swiftEnabled: ${SWIFT_API_URL != ""}
    target2Enabled: ${TARGET2_API_URL != ""}
```

### 5. Register SolaceNet Capabilities

```bash
# Register tokenization capabilities
curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/capabilities \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "tokenization.mint",
    "name": "Tokenization Mint",
    "description": "Mint tokenized assets",
    "category": "tokenization"
  }'

curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/capabilities \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "tokenization.transfer",
    "name": "Tokenization Transfer",
    "description": "Transfer tokenized assets",
    "category": "tokenization"
  }'

curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/capabilities \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "tokenization.redeem",
    "name": "Tokenization Redeem",
    "description": "Redeem tokenized assets",
    "category": "tokenization"
  }'

curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/capabilities \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "tokenization.view",
    "name": "Tokenization View",
    "description": "View tokenized assets",
    "category": "tokenization"
  }'
```

### 6. Set Up Entitlements

```bash
# Grant tokenization capabilities to tenant
curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/entitlements \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "tenantId": "tenant-001",
    "programId": "program-001",
    "capabilityId": "tokenization.mint",
    "enabled": true,
    "effectiveDate": "2025-01-01T00:00:00Z"
  }'
```

### 7. Configure Indy (Optional)

```bash
# Create pool
curl -X POST ${INDY_API_URL}/api/v1/pools \
  -H "Content-Type: application/json" \
  -d '{
    "name": "dbis-pool",
    "genesisTxn": "..."
  }'

# Issue DID for institution
curl -X POST ${INDY_API_URL}/api/v1/ledger/did \
  -H "Content-Type: application/json" \
  -d '{
    "alias": "DBIS",
    "role": "TRUSTEE"
  }'
```

### 8. Register Routes in API Gateway

Update `dbis_core/src/integration/api-gateway/app.ts`:

```typescript
import tokenizationRoutes from '@/core/solacenet/capabilities/tokenization/tokenization.routes';

// Register routes
app.use('/api/v1/solacenet/tokenization', tokenizationRoutes);
```

## Verification

### 1. Test Fabric Chaincode

```bash
# Query token
peer chaincode query \
    -C mychannel \
    -n tokenized-asset \
    -c '{"Args":["GetToken","EUR-T-2025-001"]}'
```

### 2. Test Besu Contracts

```bash
# Get token balance
cast call ${TOKENIZED_EUR_ADDRESS} \
    "balanceOf(address)" \
    ${USER_ADDRESS} \
    --rpc-url ${CHAIN_138_RPC_URL}
```

### 3. Test SolaceNet Capability

```bash
curl -X POST ${SOLACENET_API_URL}/api/v1/solacenet/entitlements/check \
  -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "capabilityId": "tokenization.mint",
    "tenantId": "tenant-001",
    "programId": "program-001"
  }'
```

### 4. Test End-to-End Flow

```bash
# Mint tokenized asset
curl -X POST http://localhost:3000/api/v1/solacenet/tokenization/mint \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "X-Tenant-Id: tenant-001" \
  -H "Content-Type: application/json" \
  -d '{
    "underlyingAsset": "EUR",
    "amount": "1000.00",
    "issuer": "0x...",
    "reserveId": "RESERVE-EUR-001"
  }'
```

## Integration with Sub-Volumes

### GAS Network Integration

Tokenized assets automatically integrate with GAS network for atomic settlement. No additional configuration needed.

### GRU Integration

Tokenized assets can be valued in GRU via XAU triangulation. Ensure GRU services are running.

### Metaverse Integration

Tokenized assets can be represented in metaverse. Configure metaverse nodes:

```bash
curl -X POST http://localhost:3000/api/metaverse/nodes \
  -H "Content-Type: application/json" \
  -d '{
    "metaverseName": "MetaverseDubai",
    "settlementEndpoint": "gas://...",
    "assetTokenizationEnabled": true
  }'
```

## Troubleshooting

### Fabric Chaincode Issues

- Check peer logs: `docker logs peer0.org1.example.com`
- Verify chaincode installed: `peer chaincode list --installed`
- Check channel configuration

### Besu Contract Issues

- Verify contract deployed: `cast code ${TOKEN_ADDRESS} --rpc-url ${RPC_URL}`
- Check transaction receipt
- Verify contract ABI matches

### FireFly Issues

- Check FireFly logs: `kubectl logs -f firefly-core`
- Verify FireFly can connect to Besu
- Check workflow status in FireFly UI

### SolaceNet Issues

- Verify capability registered: `GET /api/v1/solacenet/capabilities`
- Check entitlements: `GET /api/v1/solacenet/entitlements`
- Review policy rules: `GET /api/v1/solacenet/policy/rules`

### Cacti Issues

- Test connector health: `GET /api/v1/plugins/ledger-connector/fabric/health`
- Check connector logs
- Verify network connectivity

## Support

For deployment support:
- Check logs: `logs/tokenization-deployment.log`
- Review documentation: `docs/tokenization/`
- Contact: devops@chain138.example.com
