# TokenFactory138 Compilation Test and Fix

**Date**: 2025-12-24  
**Status**: ✅ Fixed critical role permission issue

---

## ✅ Compilation Test

### Quick Test

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138

# Test compilation with --via-ir (recommended)
forge build --via-ir --contracts contracts/emoney/TokenFactory138.sol

# Or use the test script
./scripts/compile-and-test-tokenfactory.sh
```

### Expected Results

- ✅ Compilation successful with `--via-ir`
- ⚠️ May show "Stack too deep" without `--via-ir` (this is expected)

---

## ✅ Critical Fix Applied

### Issue Found

TokenFactory138 calls PolicyManager functions that require `POLICY_OPERATOR_ROLE`, but the deployment script wasn't granting this role to TokenFactory138 itself.

### Fix Applied

Updated `script/emoney/DeployChain138.s.sol` to grant `POLICY_OPERATOR_ROLE` to TokenFactory138:

```solidity
// Grant POLICY_OPERATOR_ROLE to TokenFactory138 so it can configure tokens during deployment
policyManager.grantRole(policyManager.POLICY_OPERATOR_ROLE(), address(factory));
// Also grant to policyOperator for manual operations
policyManager.grantRole(policyManager.POLICY_OPERATOR_ROLE(), config.policyOperator);
```

---

## ✅ Pre-Deployment Checklist

- [x] Compilation test script created
- [x] Critical role permission issue fixed
- [ ] Run compilation test
- [ ] Verify all dependencies compile
- [ ] Test deployment script (dry run)
- [ ] Deploy TokenFactory138

---

## 🚀 Deployment

Once compilation passes:

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
source .env

forge script script/emoney/DeployChain138.s.sol:DeployChain138 \
  --rpc-url $RPC_URL \
  --broadcast \
  --legacy \
  --gas-price 20000000000 \
  --via-ir \
  -vv
```

**Note**: This deploys the entire eMoney system (ComplianceRegistry, DebtRegistry, PolicyManager, eMoneyToken, TokenFactory138, BridgeVault138).

---

## 📋 Summary

1. ✅ **Compilation**: Should work with `--via-ir` flag
2. ✅ **Role Fix**: TokenFactory138 now gets POLICY_OPERATOR_ROLE
3. ⏳ **Next**: Test compilation, then deploy

---

**Last Updated**: 2025-12-24

