Skip to content

Instantly share code, notes, and snippets.

@MicahZoltu
Last active June 14, 2023 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MicahZoltu/d2bfc5c2e1f9046db711d2fe8720ad50 to your computer and use it in GitHub Desktop.
Save MicahZoltu/d2bfc5c2e1f9046db711d2fe8720ad50 to your computer and use it in GitHub Desktop.
vitalik-mode
(function () {
var request = async ({method, params}) => {
if (method === 'eth_requestAccounts') {
return ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045']
}
else {
const response = await fetch('https://cloudflare-eth.com/v1/mainnet', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({jsonrpc:'2.0',id:1,method,params})})
if (!response.ok) throw new Error(`${response.status}: ${response.statusText}\n${await response.text()}`)
const json = await response.json()
if ('error' in json) throw new Error(json.error)
else return json.result
}
}
var send = async (method, params) => {
if (typeof method === 'object') {
return await request({method: method.method, params: method.params})
} else {
return await request({ method, params })
}
}
var sendAsync = async (payload, callback) => {
request(payload)
.then(result => callback(null, {jsonrpc: '2.0', id: payload.id, result }))
.catch(error => callback({ jsonrpc: '2.0', id: payload.id, error: { code: error.code, message: error.message, data: { ...error.data, stack: error.stack } } }, null))
}
var isConnected = () => true
var ethereumOn = async (kind, callback) => console.log(`window.ethereum.on called for ${kind}`)
window.ethereum = { request, send, sendAsync, on: ethereumOn, isConnected }
window.dispatchEvent(new Event('ethereum#initialized'))
})()
// Bookmarklet version
javascript:((function() { var request = async ({method, params}) => { if (method === 'eth_requestAccounts') { return ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045']; } else { const response = await fetch('https://cloudflare-eth.com/v1/mainnet', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({jsonrpc:'2.0',id:1,method,params})}); if (!response.ok) throw new Error(`${response.status}: ${response.statusText}\n${await response.text()}`); const json = await response.json(); if ('error' in json) throw new Error(json.error); else return json.result; }; }; var send = async (method, params) => { if (typeof method === 'object') { return await request({method: method.method, params: method.params}); } else { return await request({ method, params }); }; }; var sendAsync = async (payload, callback) => { request(payload).then(result => callback(null, {jsonrpc: '2.0', id: payload.id, result })).catch(error => callback({ jsonrpc: '2.0', id: payload.id, error: { code: error.code, message: error.message, data: { ...error.data, stack: error.stack } } }, null)); }; var isConnected = () => true; var ethereumOn = async (kind, callback) => console.log(`window.ethereum.on called for ${kind}`); window.ethereum = { request, send, sendAsync, on: ethereumOn, isConnected }; window.dispatchEvent(new Event('ethereum#initialized'));})())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment