Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +2 -2
- static/style.css +14 -0
- templates/index.html +137 -35
app.py
CHANGED
|
@@ -150,12 +150,12 @@ if CDP_ENABLED:
|
|
| 150 |
split_amount = amount_usdc * (split["pct"] / 100)
|
| 151 |
amount_wei = Web3.toWei(split_amount, "ether") # Convert to wei
|
| 152 |
|
| 153 |
-
# Transfer to recipient
|
| 154 |
tx_hash = await sender.transfer(
|
| 155 |
to=split["wallet"],
|
| 156 |
amount=amount_wei,
|
| 157 |
token="usdc",
|
| 158 |
-
network="base-sepolia"
|
| 159 |
)
|
| 160 |
|
| 161 |
push("payout",
|
|
|
|
| 150 |
split_amount = amount_usdc * (split["pct"] / 100)
|
| 151 |
amount_wei = Web3.toWei(split_amount, "ether") # Convert to wei
|
| 152 |
|
| 153 |
+
# Transfer to recipient (use same network as x402)
|
| 154 |
tx_hash = await sender.transfer(
|
| 155 |
to=split["wallet"],
|
| 156 |
amount=amount_wei,
|
| 157 |
token="usdc",
|
| 158 |
+
network=os.getenv("X402_NETWORK", "base-sepolia")
|
| 159 |
)
|
| 160 |
|
| 161 |
push("payout",
|
static/style.css
CHANGED
|
@@ -8,6 +8,20 @@
|
|
| 8 |
@apply bg-blue-600 text-white hover:bg-blue-700 disabled:bg-gray-400 disabled:cursor-not-allowed;
|
| 9 |
}
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
.price-tag {
|
| 12 |
@apply inline-block bg-green-100 text-green-800 text-sm font-semibold px-3 py-1 rounded-full mb-3;
|
| 13 |
}
|
|
|
|
| 8 |
@apply bg-blue-600 text-white hover:bg-blue-700 disabled:bg-gray-400 disabled:cursor-not-allowed;
|
| 9 |
}
|
| 10 |
|
| 11 |
+
.btn-secondary {
|
| 12 |
+
@apply bg-gray-600 text-white hover:bg-gray-700 disabled:bg-gray-400 disabled:cursor-not-allowed;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.btn-cool {
|
| 16 |
+
@apply px-6 py-4 rounded-full font-semibold text-white text-lg;
|
| 17 |
+
@apply bg-gradient-to-r from-blue-600 to-purple-600;
|
| 18 |
+
@apply hover:from-blue-700 hover:to-purple-700;
|
| 19 |
+
@apply transform transition-all duration-200 ease-in-out;
|
| 20 |
+
@apply hover:scale-105 hover:shadow-lg active:scale-95;
|
| 21 |
+
@apply shadow-md hover:shadow-xl;
|
| 22 |
+
@apply disabled:bg-gray-400 disabled:cursor-not-allowed disabled:transform-none disabled:shadow-none;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
.price-tag {
|
| 26 |
@apply inline-block bg-green-100 text-green-800 text-sm font-semibold px-3 py-1 rounded-full mb-3;
|
| 27 |
}
|
templates/index.html
CHANGED
|
@@ -3,50 +3,54 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>
|
| 7 |
<link rel="stylesheet" href="/static/style.css">
|
| 8 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 9 |
</head>
|
| 10 |
<body class="bg-gray-50 min-h-screen">
|
| 11 |
-
<div class="container mx-auto px-4 py-8 max-w-
|
| 12 |
<header class="text-center mb-12">
|
| 13 |
-
<h1 class="text-4xl font-bold text-gray-900 mb-4">π€ x402
|
| 14 |
-
<p class="text-xl text-gray-600">
|
| 15 |
<div class="mt-4 p-4 bg-blue-50 rounded-lg">
|
| 16 |
<p class="text-sm text-blue-800">
|
| 17 |
-
<strong>Demo Mode:</strong> Using testnet (Base Sepolia)
|
| 18 |
</p>
|
| 19 |
</div>
|
| 20 |
|
| 21 |
<!-- Wallet Connection -->
|
| 22 |
<div class="mt-6">
|
| 23 |
-
<
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
<div id="wallet-status" class="mt-2 text-sm"></div>
|
| 27 |
</div>
|
| 28 |
</header>
|
| 29 |
|
| 30 |
<!-- Service Cards -->
|
| 31 |
-
<div class="grid
|
| 32 |
<!-- Text Generation Service -->
|
| 33 |
-
<div class="service-card bg-white rounded-lg shadow-md p-
|
| 34 |
<h2 class="text-2xl font-semibold mb-2">π Text Generation</h2>
|
| 35 |
<div class="price-tag">$0.01 per request</div>
|
| 36 |
<p class="text-gray-600 mb-4">Generate creative text using DistilGPT-2</p>
|
| 37 |
|
| 38 |
<div class="form-group">
|
| 39 |
<label class="block text-sm font-medium mb-2">Prompt:</label>
|
| 40 |
-
<
|
| 41 |
-
type="text"
|
| 42 |
id="text-prompt"
|
| 43 |
placeholder="Once upon a time..."
|
| 44 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 45 |
-
|
| 46 |
-
>
|
| 47 |
<button
|
| 48 |
id="text-btn"
|
| 49 |
-
class="
|
| 50 |
>
|
| 51 |
Generate Text
|
| 52 |
</button>
|
|
@@ -55,8 +59,8 @@
|
|
| 55 |
</div>
|
| 56 |
|
| 57 |
<!-- Sentiment Analysis Service -->
|
| 58 |
-
<div class="service-card bg-white rounded-lg shadow-md p-
|
| 59 |
-
<h2 class="text-2xl font-semibold mb-2"
|
| 60 |
<div class="price-tag">$0.005 per request</div>
|
| 61 |
<p class="text-gray-600 mb-4">Analyze text sentiment using RoBERTa</p>
|
| 62 |
|
|
@@ -67,10 +71,10 @@
|
|
| 67 |
placeholder="I love this new technology!"
|
| 68 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 69 |
rows="3"
|
| 70 |
-
>
|
| 71 |
<button
|
| 72 |
id="sentiment-btn"
|
| 73 |
-
class="
|
| 74 |
>
|
| 75 |
Analyze Sentiment
|
| 76 |
</button>
|
|
@@ -79,23 +83,22 @@
|
|
| 79 |
</div>
|
| 80 |
|
| 81 |
<!-- Image Generation Service -->
|
| 82 |
-
<div class="service-card bg-white rounded-lg shadow-md p-
|
| 83 |
<h2 class="text-2xl font-semibold mb-2">πΌοΈ Image Generation</h2>
|
| 84 |
<div class="price-tag">$0.02 per request</div>
|
| 85 |
<p class="text-gray-600 mb-4">Generate images using Amazon Nova Canvas</p>
|
| 86 |
|
| 87 |
<div class="form-group">
|
| 88 |
<label class="block text-sm font-medium mb-2">Prompt:</label>
|
| 89 |
-
<
|
| 90 |
-
type="text"
|
| 91 |
id="image-prompt"
|
| 92 |
placeholder="A futuristic city skyline at sunset..."
|
| 93 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 94 |
-
|
| 95 |
-
>
|
| 96 |
<button
|
| 97 |
id="image-btn"
|
| 98 |
-
class="
|
| 99 |
>
|
| 100 |
Generate Image
|
| 101 |
</button>
|
|
@@ -145,6 +148,7 @@
|
|
| 145 |
|
| 146 |
async function initWallet() {
|
| 147 |
const connectBtn = document.getElementById('connect-wallet');
|
|
|
|
| 148 |
const statusDiv = document.getElementById('wallet-status');
|
| 149 |
|
| 150 |
// Check if MetaMask is available
|
|
@@ -186,6 +190,13 @@
|
|
| 186 |
}
|
| 187 |
|
| 188 |
connectBtn.onclick = connectWallet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
// Show platform-specific status
|
| 191 |
if (isMetaMaskMobile()) {
|
|
@@ -197,11 +208,20 @@
|
|
| 197 |
|
| 198 |
async function connectWallet() {
|
| 199 |
const connectBtn = document.getElementById('connect-wallet');
|
|
|
|
| 200 |
const statusDiv = document.getElementById('wallet-status');
|
| 201 |
|
| 202 |
try {
|
| 203 |
-
//
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
// Switch to Base Sepolia if not already on it
|
| 207 |
try {
|
|
@@ -237,14 +257,21 @@
|
|
| 237 |
chain: viem.baseSepolia, // Base Sepolia testnet
|
| 238 |
});
|
| 239 |
|
| 240 |
-
// Get account
|
| 241 |
-
const
|
| 242 |
-
currentAccount =
|
|
|
|
| 243 |
|
| 244 |
-
// Update UI
|
| 245 |
-
connectBtn.
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
console.log('β
Wallet connected:', currentAccount);
|
| 250 |
|
|
@@ -260,6 +287,82 @@
|
|
| 260 |
}
|
| 261 |
}
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
// Create x402 payment signature using TransferWithAuthorization EIP-712 (matches facilitator exactly)
|
| 264 |
async function createPaymentSignature(paymentRequirements) {
|
| 265 |
if (!walletClient || !currentAccount) {
|
|
@@ -498,7 +601,6 @@
|
|
| 498 |
<p class="font-semibold mb-2">402 Response Details:</p>
|
| 499 |
<pre class="text-gray-700 overflow-x-auto">${JSON.stringify(paymentInfo, null, 2)}</pre>
|
| 500 |
</div>
|
| 501 |
-
<button onclick="connectWallet()" class="btn btn-primary text-sm mt-3">Connect Wallet</button>
|
| 502 |
</div>
|
| 503 |
`;
|
| 504 |
return;
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Pay-Per-Inference w/x402+CDP Payouts</title>
|
| 7 |
<link rel="stylesheet" href="/static/style.css">
|
| 8 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 9 |
</head>
|
| 10 |
<body class="bg-gray-50 min-h-screen">
|
| 11 |
+
<div class="container mx-auto px-4 py-8 max-w-7xl">
|
| 12 |
<header class="text-center mb-12">
|
| 13 |
+
<h1 class="text-4xl font-bold text-gray-900 mb-4">π€ Pay-Per-Inference w/x402+CDP Payouts π€</h1>
|
| 14 |
+
<p class="text-xl text-gray-600">Monetize your models on HF Spaces with fastapi-x402</p>
|
| 15 |
<div class="mt-4 p-4 bg-blue-50 rounded-lg">
|
| 16 |
<p class="text-sm text-blue-800">
|
| 17 |
+
<strong>Demo Mode:</strong> Using testnet (Base Sepolia)
|
| 18 |
</p>
|
| 19 |
</div>
|
| 20 |
|
| 21 |
<!-- Wallet Connection -->
|
| 22 |
<div class="mt-6">
|
| 23 |
+
<div class="flex gap-3 justify-center">
|
| 24 |
+
<button id="connect-wallet" class="btn btn-primary">
|
| 25 |
+
π¦ Connect MetaMask
|
| 26 |
+
</button>
|
| 27 |
+
<button id="disconnect-wallet" class="btn btn-secondary hidden">
|
| 28 |
+
π Disconnect Wallet
|
| 29 |
+
</button>
|
| 30 |
+
</div>
|
| 31 |
<div id="wallet-status" class="mt-2 text-sm"></div>
|
| 32 |
</div>
|
| 33 |
</header>
|
| 34 |
|
| 35 |
<!-- Service Cards -->
|
| 36 |
+
<div class="grid lg:grid-cols-3 md:grid-cols-2 grid-cols-1 gap-8">
|
| 37 |
<!-- Text Generation Service -->
|
| 38 |
+
<div class="service-card bg-white rounded-lg shadow-md p-8">
|
| 39 |
<h2 class="text-2xl font-semibold mb-2">π Text Generation</h2>
|
| 40 |
<div class="price-tag">$0.01 per request</div>
|
| 41 |
<p class="text-gray-600 mb-4">Generate creative text using DistilGPT-2</p>
|
| 42 |
|
| 43 |
<div class="form-group">
|
| 44 |
<label class="block text-sm font-medium mb-2">Prompt:</label>
|
| 45 |
+
<textarea
|
|
|
|
| 46 |
id="text-prompt"
|
| 47 |
placeholder="Once upon a time..."
|
| 48 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 49 |
+
rows="3"
|
| 50 |
+
>The future of payments and AI is</textarea>
|
| 51 |
<button
|
| 52 |
id="text-btn"
|
| 53 |
+
class="mt-3 w-full px-6 py-4 rounded-full font-semibold text-white text-lg bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 transform transition-all duration-200 ease-in-out hover:scale-105 hover:shadow-lg active:scale-95 shadow-md hover:shadow-xl disabled:bg-gray-400 disabled:cursor-not-allowed"
|
| 54 |
>
|
| 55 |
Generate Text
|
| 56 |
</button>
|
|
|
|
| 59 |
</div>
|
| 60 |
|
| 61 |
<!-- Sentiment Analysis Service -->
|
| 62 |
+
<div class="service-card bg-white rounded-lg shadow-md p-8">
|
| 63 |
+
<h2 class="text-2xl font-semibold mb-2">π Sentiment Analysis</h2>
|
| 64 |
<div class="price-tag">$0.005 per request</div>
|
| 65 |
<p class="text-gray-600 mb-4">Analyze text sentiment using RoBERTa</p>
|
| 66 |
|
|
|
|
| 71 |
placeholder="I love this new technology!"
|
| 72 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 73 |
rows="3"
|
| 74 |
+
>This Coinbase x402+CDP hackathon has been a lot of fun!!!</textarea>
|
| 75 |
<button
|
| 76 |
id="sentiment-btn"
|
| 77 |
+
class="mt-3 w-full px-6 py-4 rounded-full font-semibold text-white text-lg bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 transform transition-all duration-200 ease-in-out hover:scale-105 hover:shadow-lg active:scale-95 shadow-md hover:shadow-xl disabled:bg-gray-400 disabled:cursor-not-allowed"
|
| 78 |
>
|
| 79 |
Analyze Sentiment
|
| 80 |
</button>
|
|
|
|
| 83 |
</div>
|
| 84 |
|
| 85 |
<!-- Image Generation Service -->
|
| 86 |
+
<div class="service-card bg-white rounded-lg shadow-md p-8">
|
| 87 |
<h2 class="text-2xl font-semibold mb-2">πΌοΈ Image Generation</h2>
|
| 88 |
<div class="price-tag">$0.02 per request</div>
|
| 89 |
<p class="text-gray-600 mb-4">Generate images using Amazon Nova Canvas</p>
|
| 90 |
|
| 91 |
<div class="form-group">
|
| 92 |
<label class="block text-sm font-medium mb-2">Prompt:</label>
|
| 93 |
+
<textarea
|
|
|
|
| 94 |
id="image-prompt"
|
| 95 |
placeholder="A futuristic city skyline at sunset..."
|
| 96 |
class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
| 97 |
+
rows="3"
|
| 98 |
+
>A happy developer accepting x402 payments on HuggingFace</textarea>
|
| 99 |
<button
|
| 100 |
id="image-btn"
|
| 101 |
+
class="mt-3 w-full px-6 py-4 rounded-full font-semibold text-white text-lg bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 transform transition-all duration-200 ease-in-out hover:scale-105 hover:shadow-lg active:scale-95 shadow-md hover:shadow-xl disabled:bg-gray-400 disabled:cursor-not-allowed"
|
| 102 |
>
|
| 103 |
Generate Image
|
| 104 |
</button>
|
|
|
|
| 148 |
|
| 149 |
async function initWallet() {
|
| 150 |
const connectBtn = document.getElementById('connect-wallet');
|
| 151 |
+
const disconnectBtn = document.getElementById('disconnect-wallet');
|
| 152 |
const statusDiv = document.getElementById('wallet-status');
|
| 153 |
|
| 154 |
// Check if MetaMask is available
|
|
|
|
| 190 |
}
|
| 191 |
|
| 192 |
connectBtn.onclick = connectWallet;
|
| 193 |
+
disconnectBtn.onclick = disconnectWallet;
|
| 194 |
+
|
| 195 |
+
// Listen for account changes in MetaMask
|
| 196 |
+
if (window.ethereum) {
|
| 197 |
+
window.ethereum.on('accountsChanged', handleAccountsChanged);
|
| 198 |
+
window.ethereum.on('chainChanged', handleChainChanged);
|
| 199 |
+
}
|
| 200 |
|
| 201 |
// Show platform-specific status
|
| 202 |
if (isMetaMaskMobile()) {
|
|
|
|
| 208 |
|
| 209 |
async function connectWallet() {
|
| 210 |
const connectBtn = document.getElementById('connect-wallet');
|
| 211 |
+
const disconnectBtn = document.getElementById('disconnect-wallet');
|
| 212 |
const statusDiv = document.getElementById('wallet-status');
|
| 213 |
|
| 214 |
try {
|
| 215 |
+
// Show current status
|
| 216 |
+
statusDiv.innerHTML = '<span class="text-blue-600">π Connecting to MetaMask...</span>';
|
| 217 |
+
|
| 218 |
+
// Force fresh account access - this will show account selection if multiple accounts
|
| 219 |
+
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
|
| 220 |
+
console.log('π Available accounts:', accounts);
|
| 221 |
+
|
| 222 |
+
// Get the currently selected account immediately
|
| 223 |
+
const currentAccounts = await window.ethereum.request({ method: 'eth_accounts' });
|
| 224 |
+
console.log('π― Current account in MetaMask:', currentAccounts[0]);
|
| 225 |
|
| 226 |
// Switch to Base Sepolia if not already on it
|
| 227 |
try {
|
|
|
|
| 257 |
chain: viem.baseSepolia, // Base Sepolia testnet
|
| 258 |
});
|
| 259 |
|
| 260 |
+
// Get the currently selected account (fresh from MetaMask)
|
| 261 |
+
const freshAccounts = await window.ethereum.request({ method: 'eth_accounts' });
|
| 262 |
+
currentAccount = freshAccounts[0]; // Use the currently selected account
|
| 263 |
+
console.log('π― Using account:', currentAccount);
|
| 264 |
|
| 265 |
+
// Update UI - show disconnect button, hide connect button
|
| 266 |
+
connectBtn.classList.add('hidden');
|
| 267 |
+
disconnectBtn.classList.remove('hidden');
|
| 268 |
+
disconnectBtn.textContent = `π ${currentAccount.slice(0, 6)}...${currentAccount.slice(-4)}`;
|
| 269 |
+
statusDiv.innerHTML = `
|
| 270 |
+
<div class="text-green-600 text-center">
|
| 271 |
+
π Connected: ${currentAccount.slice(0, 8)}...${currentAccount.slice(-6)}<br>
|
| 272 |
+
<span class="text-xs text-gray-600">Ready for payments on Base Sepolia</span>
|
| 273 |
+
</div>
|
| 274 |
+
`;
|
| 275 |
|
| 276 |
console.log('β
Wallet connected:', currentAccount);
|
| 277 |
|
|
|
|
| 287 |
}
|
| 288 |
}
|
| 289 |
|
| 290 |
+
async function disconnectWallet() {
|
| 291 |
+
const connectBtn = document.getElementById('connect-wallet');
|
| 292 |
+
const disconnectBtn = document.getElementById('disconnect-wallet');
|
| 293 |
+
const statusDiv = document.getElementById('wallet-status');
|
| 294 |
+
|
| 295 |
+
try {
|
| 296 |
+
// Try to disconnect from MetaMask (this may not work in all versions)
|
| 297 |
+
if (window.ethereum && window.ethereum.request) {
|
| 298 |
+
await window.ethereum.request({
|
| 299 |
+
method: 'wallet_revokePermissions',
|
| 300 |
+
params: [{ eth_accounts: {} }]
|
| 301 |
+
});
|
| 302 |
+
}
|
| 303 |
+
} catch (error) {
|
| 304 |
+
console.log('Note: MetaMask permission revocation not available:', error.message);
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
// Reset wallet state
|
| 308 |
+
walletClient = null;
|
| 309 |
+
currentAccount = null;
|
| 310 |
+
|
| 311 |
+
// Update UI - show connect button, hide disconnect button
|
| 312 |
+
connectBtn.classList.remove('hidden');
|
| 313 |
+
connectBtn.disabled = false;
|
| 314 |
+
connectBtn.textContent = 'π¦ Connect MetaMask';
|
| 315 |
+
disconnectBtn.classList.add('hidden');
|
| 316 |
+
statusDiv.innerHTML = `
|
| 317 |
+
<div class="text-gray-600">
|
| 318 |
+
π° Disconnected.
|
| 319 |
+
<span class="text-sm">To switch accounts, change in MetaMask first, then reconnect.</span>
|
| 320 |
+
</div>
|
| 321 |
+
`;
|
| 322 |
+
|
| 323 |
+
console.log('π Wallet disconnected (app-level)');
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
// Handle account changes (when user switches accounts in MetaMask)
|
| 327 |
+
function handleAccountsChanged(accounts) {
|
| 328 |
+
console.log('π Accounts changed:', accounts);
|
| 329 |
+
|
| 330 |
+
if (accounts.length === 0) {
|
| 331 |
+
// No accounts connected - disconnect
|
| 332 |
+
disconnectWallet();
|
| 333 |
+
} else if (currentAccount && accounts[0] !== currentAccount) {
|
| 334 |
+
// Account switched - update UI
|
| 335 |
+
const disconnectBtn = document.getElementById('disconnect-wallet');
|
| 336 |
+
const statusDiv = document.getElementById('wallet-status');
|
| 337 |
+
|
| 338 |
+
currentAccount = accounts[0];
|
| 339 |
+
disconnectBtn.textContent = `π ${currentAccount.slice(0, 6)}...${currentAccount.slice(-4)}`;
|
| 340 |
+
statusDiv.innerHTML = `<span class="text-blue-600">π Switched to ${currentAccount.slice(0, 6)}...${currentAccount.slice(-4)}</span>`;
|
| 341 |
+
|
| 342 |
+
console.log('β
Account switched to:', currentAccount);
|
| 343 |
+
|
| 344 |
+
// Update wallet client with new account
|
| 345 |
+
if (walletClient) {
|
| 346 |
+
walletClient = viem.createWalletClient({
|
| 347 |
+
transport: viem.custom(window.ethereum),
|
| 348 |
+
chain: viem.baseSepolia,
|
| 349 |
+
});
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
// Handle chain changes
|
| 355 |
+
function handleChainChanged(chainId) {
|
| 356 |
+
console.log('π Chain changed to:', chainId);
|
| 357 |
+
const statusDiv = document.getElementById('wallet-status');
|
| 358 |
+
|
| 359 |
+
if (chainId === '0x14a34') {
|
| 360 |
+
statusDiv.innerHTML = '<span class="text-green-600">β
Connected to Base Sepolia</span>';
|
| 361 |
+
} else {
|
| 362 |
+
statusDiv.innerHTML = '<span class="text-orange-600">β οΈ Please switch to Base Sepolia network</span>';
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
// Create x402 payment signature using TransferWithAuthorization EIP-712 (matches facilitator exactly)
|
| 367 |
async function createPaymentSignature(paymentRequirements) {
|
| 368 |
if (!walletClient || !currentAccount) {
|
|
|
|
| 601 |
<p class="font-semibold mb-2">402 Response Details:</p>
|
| 602 |
<pre class="text-gray-700 overflow-x-auto">${JSON.stringify(paymentInfo, null, 2)}</pre>
|
| 603 |
</div>
|
|
|
|
| 604 |
</div>
|
| 605 |
`;
|
| 606 |
return;
|