-
-
Notifications
You must be signed in to change notification settings - Fork 27
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,27 @@ | ||
| 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) { | ||
| const msg = error?.code === 4001 | ||
| ? "User denied account access" | ||
| : "Failed to initialize wallet provider"; | ||
| console.error(msg, error); | ||
| throw error; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Fallback to RPC | ||
| 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)); | ||
|
Comment on lines
+3
to
+26
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 Agents
Rav1Chauhan marked this conversation as resolved.
|
||
| }; | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.