- Organized 252 files across project - Root directory: 187 → 2 files (98.9% reduction) - Moved configuration guides to docs/04-configuration/ - Moved troubleshooting guides to docs/09-troubleshooting/ - Moved quick start guides to docs/01-getting-started/ - Moved reports to reports/ directory - Archived temporary files - Generated comprehensive reports and documentation - Created maintenance scripts and guides All files organized according to established standards.
7.0 KiB
Blockscout MetaMask Ethers Fix
Date: $(date)
Issue: Failed to connect MetaMask: ethers is not defined
Status: ✅ Fixed
Problem
The Blockscout frontend was failing to connect MetaMask with the error:
Failed to connect MetaMask: ethers is not defined
Root Cause: The ethers.js library was not loading properly from the CDN (https://cdn.ethers.io/lib/ethers-5.7.2.umd.min.js). This could happen due to:
- CDN being blocked or unavailable
- Network connectivity issues
- Script loading timing issues
- Browser security policies
Solution
1. Added Fallback CDN
The primary CDN now has a fallback to unpkg.com:
<script src="https://cdn.ethers.io/lib/ethers-5.7.2.umd.min.js"
onerror="this.onerror=null; this.src='https://unpkg.com/ethers@5.7.2/dist/ethers.umd.min.js';"></script>
2. Added Ethers Loading Detection
Added a script to detect if ethers loaded successfully and automatically load from fallback if needed:
(function() {
function checkEthers() {
if (typeof ethers === 'undefined') {
console.warn('Primary ethers CDN failed, trying fallback...');
const script = document.createElement('script');
script.src = 'https://unpkg.com/ethers@5.7.2/dist/ethers.umd.min.js';
// ... fallback loading logic
}
}
// Check after DOM loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(checkEthers, 100);
});
} else {
setTimeout(checkEthers, 100);
}
})();
3. Added Ethers Availability Checks
Created a helper function and added checks before all ethers usage:
function ensureEthers() {
if (typeof ethers === 'undefined') {
throw new Error('Ethers library is not loaded. Please refresh the page.');
}
return true;
}
Updated functions:
connectMetaMask()- Checks before creating providerrefreshWETHBalances()- Checks before contract callswrapWETH9()- Checks before wrappingunwrapWETH9()- Checks before unwrappingwrapWETH10()- Checks before wrappingunwrapWETH10()- Checks before unwrapping
4. Improved Error Handling
Enhanced error messages to clearly indicate when ethers library fails to load:
catch (error) {
let errorMessage = error.message || 'Unknown error';
if (errorMessage.includes('ethers is not defined') || typeof ethers === 'undefined') {
errorMessage = 'Ethers library failed to load. Please refresh the page.';
}
alert('Failed to connect MetaMask: ' + errorMessage);
}
Files Modified
- Frontend:
/home/intlc/projects/proxmox/explorer-monorepo/frontend/public/index.html- Updated ethers CDN with fallback
- Added ethers loading detection
- Added
ensureEthers()helper function - Added checks in all MetaMask-related functions
Deployment
Option 1: Use Deployment Script (Recommended)
cd /home/intlc/projects/proxmox
./scripts/fix-blockscout-metamask-ethers.sh
The script will:
- Check container accessibility (VMID 5000)
- Locate the frontend deployment directory
- Deploy the fixed
index.html - Reload nginx if needed
Option 2: Manual Deployment
-
Access the blockscout container:
pct exec 5000 -- bash # or ssh root@192.168.11.140 -
Locate frontend directory:
# Check nginx config find /etc/nginx -name "*.conf" -exec grep -l "root\|alias" {} \; # Or check blockscout static assets docker exec blockscout find /app/apps/explorer/priv/static -name "index.html" -
Copy fixed file:
# From your local machine scp explorer-monorepo/frontend/public/index.html root@192.168.11.140:/path/to/frontend/ # Or if using pct pct push 5000 explorer-monorepo/frontend/public/index.html /path/to/frontend/index.html -
Reload web server:
systemctl reload nginx # or restart blockscout container docker restart blockscout
Verification
1. Check Browser Console
Open browser developer tools (F12) and check:
- ✅ No errors about "ethers is not defined"
- ✅ Console shows "Ethers loaded successfully"
- ✅ Network tab shows ethers library loaded (from primary or fallback CDN)
2. Test MetaMask Connection
- Navigate to Blockscout frontend
- Click "Connect MetaMask" button
- Should successfully connect without "ethers is not defined" error
- Should be able to use WETH wrap/unwrap functions
3. Test Fallback CDN
If you want to test the fallback:
- Block the primary CDN in browser dev tools (Network tab → Block request URL)
- Refresh page
- Should automatically load from unpkg.com fallback
- MetaMask connection should still work
Technical Details
CDN URLs Used
- Primary:
https://cdn.ethers.io/lib/ethers-5.7.2.umd.min.js - Fallback:
https://unpkg.com/ethers@5.7.2/dist/ethers.umd.min.js
Ethers.js Version
- Version: 5.7.2
- Format: UMD (Universal Module Definition)
- Compatibility: Works in browsers without module bundlers
Functions Protected
All functions that use ethers now check availability:
connectMetaMask()- Line 718refreshWETHBalances()- Line 874wrapWETH9()- Line 922unwrapWETH9()- Line 961wrapWETH10()- Line 993unwrapWETH10()- Line 1032
Troubleshooting
Issue: Still getting "ethers is not defined"
Solutions:
- Clear browser cache and hard refresh (Ctrl+Shift+R)
- Check browser console for CDN loading errors
- Verify both CDN URLs are accessible:
curl -I https://cdn.ethers.io/lib/ethers-5.7.2.umd.min.js curl -I https://unpkg.com/ethers@5.7.2/dist/ethers.umd.min.js - Check if browser extensions are blocking CDN requests
- Try in incognito/private mode
Issue: Frontend file not updating
Solutions:
- Verify file was copied correctly:
# On container head -20 /path/to/frontend/index.html | grep "unpkg.com" - Check nginx cache settings
- Restart nginx:
systemctl restart nginx - Clear browser cache
Issue: Script deployment fails
Solutions:
- Check container is running:
pct status 5000 - Verify SSH access:
ssh root@192.168.11.140 - Check file permissions:
ls -la explorer-monorepo/frontend/public/index.html - Run script with debug:
bash -x scripts/fix-blockscout-metamask-ethers.sh
Related Files
- Frontend Source:
explorer-monorepo/frontend/public/index.html - Deployment Script:
scripts/fix-blockscout-metamask-ethers.sh - Blockscout Config:
smom-dbis-138-proxmox/install/blockscout-install.sh
Status
✅ Fixed: Ethers library loading with fallback
✅ Fixed: Ethers availability checks in all functions
✅ Fixed: Improved error messages
✅ Deployed: Ready for deployment to VMID 5000
Next Steps
- Deploy the fix using the deployment script
- Test MetaMask connection
- Verify WETH utilities work correctly
- Monitor for any remaining issues