|
| 1 | +import type { GelatoEvmRelayerClient } from '@gelatocloud/gasless'; |
| 2 | +import { createGelatoEvmRelayerClient } from '@gelatocloud/gasless'; |
| 3 | +import { baseSepolia } from 'viem/chains'; |
| 4 | + |
| 5 | +const chain = baseSepolia; |
| 6 | +const TARGET_CONTRACT = '0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De'; |
| 7 | +const INCREMENT_CALLDATA = '0xd09de08a'; |
| 8 | + |
| 9 | +// Pre-fill API key from env (Vite exposes VITE_-prefixed vars) |
| 10 | +const envApiKey = import.meta.env['VITE_GELATO_API_KEY'] as string | undefined; |
| 11 | + |
| 12 | +// DOM elements |
| 13 | +const apiKeyInput = document.getElementById('apiKey') as HTMLInputElement; |
| 14 | +const connectBtn = document.getElementById('connectBtn') as HTMLButtonElement; |
| 15 | +const sendBtn = document.getElementById('sendBtn') as HTMLButtonElement; |
| 16 | +const disconnectBtn = document.getElementById('disconnectBtn') as HTMLButtonElement; |
| 17 | +const statusDot = document.getElementById('statusDot') as HTMLSpanElement; |
| 18 | +const statusText = document.getElementById('statusText') as HTMLSpanElement; |
| 19 | +const logEl = document.getElementById('log') as HTMLDivElement; |
| 20 | + |
| 21 | +if (envApiKey) apiKeyInput.value = envApiKey; |
| 22 | + |
| 23 | +let relayer: GelatoEvmRelayerClient | null = null; |
| 24 | +let subscriptionId: string | null = null; |
| 25 | + |
| 26 | +function log( |
| 27 | + message: string, |
| 28 | + type: 'info' | 'submitted' | 'success' | 'rejected' | 'reverted' | 'error' = 'info' |
| 29 | +) { |
| 30 | + const entry = document.createElement('div'); |
| 31 | + entry.className = `log-entry ${type}`; |
| 32 | + const time = new Date().toLocaleTimeString(); |
| 33 | + entry.innerHTML = `<span class="time">${time}</span>${message}`; |
| 34 | + logEl.appendChild(entry); |
| 35 | + logEl.scrollTop = logEl.scrollHeight; |
| 36 | +} |
| 37 | + |
| 38 | +function setConnected(connected: boolean) { |
| 39 | + connectBtn.disabled = connected; |
| 40 | + apiKeyInput.disabled = connected; |
| 41 | + sendBtn.disabled = !connected; |
| 42 | + disconnectBtn.disabled = !connected; |
| 43 | + statusDot.className = connected ? 'dot connected' : 'dot'; |
| 44 | + statusText.textContent = connected ? 'Connected' : 'Disconnected'; |
| 45 | +} |
| 46 | + |
| 47 | +connectBtn.addEventListener('click', async () => { |
| 48 | + const apiKey = apiKeyInput.value.trim(); |
| 49 | + if (!apiKey) { |
| 50 | + log('Please enter an API key', 'error'); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + log('Creating relayer client...'); |
| 56 | + |
| 57 | + relayer = createGelatoEvmRelayerClient({ |
| 58 | + apiKey, |
| 59 | + testnet: chain.testnet |
| 60 | + }); |
| 61 | + |
| 62 | + log('Subscribing to transaction updates...'); |
| 63 | + const subscription = await relayer.ws.subscribe(); |
| 64 | + subscriptionId = subscription.subscriptionId; |
| 65 | + |
| 66 | + subscription.on('submitted', (data) => |
| 67 | + log(`[submitted] Transaction ${data.id} submitted with hash: ${data.hash}`, 'submitted') |
| 68 | + ); |
| 69 | + subscription.on('success', (data) => |
| 70 | + log( |
| 71 | + `[success] Transaction ${data.id} included in block ${data.receipt.blockNumber}`, |
| 72 | + 'success' |
| 73 | + ) |
| 74 | + ); |
| 75 | + subscription.on('rejected', (data) => |
| 76 | + log(`[rejected] Transaction ${data.id} was rejected: ${data.message}`, 'rejected') |
| 77 | + ); |
| 78 | + subscription.on('reverted', (data) => |
| 79 | + log( |
| 80 | + `[reverted] Transaction ${data.id} reverted on block ${data.receipt.blockNumber}`, |
| 81 | + 'reverted' |
| 82 | + ) |
| 83 | + ); |
| 84 | + |
| 85 | + setConnected(true); |
| 86 | + log('Connected! Listening for transaction updates...'); |
| 87 | + } catch (err) { |
| 88 | + log(`Connection failed: ${err instanceof Error ? err.message : err}`, 'error'); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +sendBtn.addEventListener('click', async () => { |
| 93 | + if (!relayer) return; |
| 94 | + |
| 95 | + try { |
| 96 | + sendBtn.disabled = true; |
| 97 | + log(`Sending increment() to ${TARGET_CONTRACT} on ${chain.name}...`); |
| 98 | + |
| 99 | + const id = await relayer.sendTransaction({ |
| 100 | + chainId: chain.id, |
| 101 | + data: INCREMENT_CALLDATA, |
| 102 | + to: TARGET_CONTRACT |
| 103 | + }); |
| 104 | + |
| 105 | + log(`Transaction sent: ${id}`); |
| 106 | + sendBtn.disabled = false; |
| 107 | + } catch (err) { |
| 108 | + log(`Send failed: ${err instanceof Error ? err.message : err}`, 'error'); |
| 109 | + sendBtn.disabled = false; |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +disconnectBtn.addEventListener('click', async () => { |
| 114 | + if (!relayer) return; |
| 115 | + |
| 116 | + try { |
| 117 | + if (subscriptionId) { |
| 118 | + await relayer.ws.unsubscribe(subscriptionId); |
| 119 | + subscriptionId = null; |
| 120 | + } |
| 121 | + relayer.ws.disconnect(); |
| 122 | + relayer = null; |
| 123 | + setConnected(false); |
| 124 | + log('Disconnected.'); |
| 125 | + } catch (err) { |
| 126 | + log(`Disconnect error: ${err instanceof Error ? err.message : err}`, 'error'); |
| 127 | + } |
| 128 | +}); |
0 commit comments