-
-
Notifications
You must be signed in to change notification settings - Fork 24
Fix: Initialize Web3 with injected provider when MetaMask is available #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,20 @@ | ||||||||||||||
| import Web3 from "web3"; | ||||||||||||||
|
|
||||||||||||||
| export const getWeb3 = (BLOCKCHAIN_URI) => | ||||||||||||||
| new Promise((resolve, reject) => { | ||||||||||||||
| if (window.ethereum) { | ||||||||||||||
| try { | ||||||||||||||
| const web3 = new Web3(BLOCKCHAIN_URI); | ||||||||||||||
| resolve(web3); | ||||||||||||||
| } catch (error) { | ||||||||||||||
| reject(error); | ||||||||||||||
| } | ||||||||||||||
| } else { | ||||||||||||||
| reject("Please install Metamask"); | ||||||||||||||
| export const getWeb3 = async (URI) => { | ||||||||||||||
| if (typeof window !== "undefined" && window.ethereum) { | ||||||||||||||
| try { | ||||||||||||||
| const web3 = new Web3(window.ethereum); | ||||||||||||||
|
|
||||||||||||||
| // Request account access (important) | ||||||||||||||
| await window.ethereum.request({ method: "eth_requestAccounts" }); | ||||||||||||||
|
|
||||||||||||||
| return web3; | ||||||||||||||
| } catch (error) { | ||||||||||||||
| console.error("User denied account access", error); | ||||||||||||||
| throw error; | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Fallback to RPC | ||||||||||||||
| return new Web3(new Web3.providers.HttpProvider(URI)); | ||||||||||||||
|
Comment on lines
+3
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# 1. Check if djed-sdk/dist files are tracked by git
echo "=== Checking if djed-sdk/dist files are tracked by git ==="
git -C djed-sdk ls-files dist/ 2>/dev/null | head -20
# 2. Confirm what entrypoints package.json exposes
echo ""
echo "=== Checking djed-sdk/package.json entrypoints ==="
fd "package.json" djed-sdk --max-depth 2 --exec cat {}
# 3. Show the stale provider logic still in dist
echo ""
echo "=== Searching for BLOCKCHAIN_URI or old Web3 logic in dist ==="
rg -n "BLOCKCHAIN_URI\|new Web3\(BLOCKCHAIN_URI" djed-sdk/dist/ 2>/dev/null || echo "No matches found"Repository: DjedAlliance/StablePay Length of output: 836 🏁 Script executed: #!/bin/bash
# Check the actual content of dist files to see if they're truly stale
echo "=== Content of dist/esm/index.js ==="
cat djed-sdk/dist/esm/index.js
echo ""
echo "=== Content of dist/umd/index.js ==="
cat djed-sdk/dist/umd/index.js
# Also check git history to see when dist was last updated
echo ""
echo "=== Recent git commits touching dist files ==="
git log --oneline -n 10 -- djed-sdk/dist/Repository: DjedAlliance/StablePay Length of output: 50378 Dist bundles in The dist files ( 🤖 Prompt for AI AgentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a caller invokes 🛡️ Suggested guard+ if (!URI) {
+ throw new Error("No injected provider found and no fallback URI supplied.");
+ }
// Fallback to RPC
return new Web3(new Web3.providers.HttpProvider(URI));📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| }; | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ | |
| # macOS specific files | ||
| .DS_Store | ||
|
|
||
| dist/ | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>StablePay Test</title> | ||
| <script src="./index.js"></script> | ||
| </head> | ||
| <body> | ||
| <h2>Test Web3 Init</h2> | ||
| <button onclick="testWeb3()">Connect Wallet</button> | ||
|
|
||
| <script> | ||
| async function testWeb3() { | ||
| if (typeof window.ethereum === "undefined") { | ||
| alert("MetaMask not installed"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const web3 = await StablePay.getWeb3("https://mainnet.infura.io/v3/YOUR_KEY"); | ||
| console.log("Web3 instance:", web3); | ||
| alert("Connected! Check console."); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catch-all error message misleadingly implies the error is always a user denial.
"User denied account access"is only accurate for the EIP-1193 user-rejection code (4001). The same catch block fires for any runtime error (e.g.,window.ethereum.requestmissing, unexpected provider failures). Either narrow the message or inspect the error code before logging.🛡️ Suggested fix
Or, if you want to preserve the specific message for user rejections:
} catch (error) { + if (error.code === 4001) { + console.error("User denied account access", error); + } else { console.error("User denied account access", error); + } throw error; }Simplified:
📝 Committable suggestion
🤖 Prompt for AI Agents