# TokenFactory138 Compilation Analysis

**Date**: 2025-12-24  
**Status**: Pre-deployment analysis

---

## ✅ Contract Structure

TokenFactory138 is well-structured with:
- ✅ Proper imports
- ✅ Interface compliance
- ✅ Error handling
- ✅ Access control

---

## ⚠️ Potential Issue: Role Permissions

### Problem

TokenFactory138 calls PolicyManager functions that require `POLICY_OPERATOR_ROLE`:

```solidity
// In TokenFactory138.deployToken()
IPolicyManager(policyManager).setLienMode(token, config.defaultLienMode);
IPolicyManager(policyManager).setBridgeOnly(token, config.bridgeOnly);
IPolicyManager(policyManager).setBridge(token, config.bridge);
```

But PolicyManager requires `POLICY_OPERATOR_ROLE`:
```solidity
// In PolicyManager
function setLienMode(...) external override onlyRole(POLICY_OPERATOR_ROLE)
function setBridgeOnly(...) external override onlyRole(POLICY_OPERATOR_ROLE)
function setBridge(...) external override onlyRole(POLICY_OPERATOR_ROLE)
```

### Solution

The deployment script (`DeployChain138.s.sol`) should grant `POLICY_OPERATOR_ROLE` to TokenFactory138:

```solidity
// After deploying TokenFactory138
policyManager.grantRole(policyManager.POLICY_OPERATOR_ROLE(), address(factory));
```

**Check**: Verify this is done in the deployment script.

---

## ✅ Compilation Test Commands

### Test 1: Standard Compilation

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
forge build --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tf138-std.log
```

**Expected**: May show "Stack too deep" error

### Test 2: Via-IR Compilation (Recommended)

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
forge build --via-ir --contracts contracts/emoney/TokenFactory138.sol 2>&1 | tee /tmp/tf138-viair.log
```

**Expected**: ✅ Compilation successful

### Test 3: Full Project Build

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
forge build --via-ir 2>&1 | grep -i "error\|TokenFactory138" | head -20
```

---

## ✅ Dependency Check

All dependencies should exist:

1. ✅ `contracts/emoney/interfaces/ITokenFactory138.sol`
2. ✅ `contracts/emoney/interfaces/IeMoneyToken.sol`
3. ✅ `contracts/emoney/interfaces/IPolicyManager.sol`
4. ✅ `contracts/emoney/eMoneyToken.sol`
5. ✅ `contracts/emoney/errors/FactoryErrors.sol`
6. ✅ `contracts/emoney/errors/RegistryErrors.sol`
7. ✅ OpenZeppelin contracts (via lib/)

---

## ✅ Pre-Deployment Checklist

- [ ] Run compilation test: `./scripts/compile-and-test-tokenfactory.sh`
- [ ] Verify no "Stack too deep" errors (use --via-ir)
- [ ] Check all dependencies compile
- [ ] Verify deployment script grants POLICY_OPERATOR_ROLE to TokenFactory138
- [ ] Ensure all required contracts are deployed first:
  - [ ] ComplianceRegistry ✅
  - [ ] DebtRegistry
  - [ ] PolicyManager
  - [ ] eMoneyToken (implementation)

---

## 🚀 Deployment Command

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, including TokenFactory138.

---

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

