-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(site): handle metamask not being installed and prompt user to co…
…nnect to sepolia
- Loading branch information
Showing
8 changed files
with
132 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { ethers } from 'ethers'; | ||
import { BrowserProvider, Eip1193Provider } from 'ethers/types/providers'; | ||
|
||
// See https://kit.svelte.dev/docs/types#app | ||
// for information about these interfaces | ||
declare global { | ||
namespace App { | ||
// interface Error {} | ||
// interface Locals {} | ||
// interface PageData {} | ||
// interface Platform {} | ||
} | ||
namespace App { | ||
// interface Error {} | ||
// interface Locals {} | ||
// interface PageData {} | ||
// interface Platform {} | ||
} | ||
interface Window { | ||
ethereum: undefined | (Eip1193Provider & BrowserProvider); | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ethers } from 'ethers'; | ||
import { BrowserProvider, Eip1193Provider } from 'ethers/types/providers'; | ||
|
||
// See https://kit.svelte.dev/docs/types#app | ||
// for information about these interfaces | ||
declare global { | ||
interface Window { | ||
ethereum: undefined | (Eip1193Provider & BrowserProvider); | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<button class="bg-red-500 px-4" on:click on:focus on:submit {...$$restProps}> | ||
<slot /> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ethers } from 'ethers'; | ||
import { | ||
ethereumAddress, | ||
ethersProvider, | ||
ethersSigner, | ||
connectedToSepolia | ||
} from './stores/wallets'; | ||
import { get } from 'svelte/store'; | ||
|
||
const SEPOLIA_CHAIN_ID = '0xaa36a7'; | ||
|
||
export const updateConnectedToSeplia = async () => { | ||
if (window.ethereum === undefined) { | ||
console.error('trying to update connected to sepolia with no metamask installed'); | ||
} | ||
const currentChainId = await window.ethereum.request({ method: 'eth_chainId' }); | ||
connectedToSepolia.set(currentChainId === SEPOLIA_CHAIN_ID); | ||
}; | ||
|
||
export const connectToSepolia = async () => { | ||
if (window.ethereum === undefined) { | ||
console.error('trying to set up ethers while metamask is not installed'); | ||
} | ||
|
||
const eProvider = new ethers.BrowserProvider(window.ethereum); | ||
|
||
eProvider.on('network', (newNetwork, oldNetwork) => { | ||
// When a Provider makes its initial connection, it emits a "network" | ||
// event with a null oldNetwork along with the newNetwork. So, if the | ||
// oldNetwork exists, it represents a changing network | ||
console.log(newNetwork); | ||
console.log(oldNetwork); | ||
if (oldNetwork) { | ||
window.location.reload(); | ||
} | ||
}); | ||
ethersProvider.set(eProvider); | ||
|
||
/// Requests the end user to switch to sepolia. | ||
await window.ethereum.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: SEPOLIA_CHAIN_ID }] | ||
}); | ||
|
||
updateConnectedToSeplia(); | ||
if (get(connectedToSepolia) == false) { | ||
console.error('did not properly connect to sepolia'); | ||
} | ||
console.log('connected to sepolia!'); | ||
}; | ||
|
||
export const ethersSetup = async () => { | ||
const eProvider = new ethers.BrowserProvider(window.ethereum); | ||
const allAccounts = await eProvider.listAccounts(); | ||
if (allAccounts.length === 0) { | ||
alert('Please add an account to MetaMask to continue'); | ||
} | ||
const eSigner = await eProvider.getSigner(0); | ||
const eAddress = await eSigner.getAddress(); | ||
ethersSigner.set(eSigner); | ||
ethereumAddress.set(eAddress); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
site/src/routes/blog/start-of-the-endgame/ConnectToMetamask.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script lang="ts"> | ||
import { browser } from '$app/environment'; | ||
import { initClients, startBalanceWorkers } from '$lib/transferDemo'; | ||
import { onMount } from 'svelte'; | ||
import { metamaskInstalled, connectedToSepolia } from '$lib/stores/wallets'; | ||
import { ethersSetup, connectToSepolia, updateConnectedToSeplia } from '$lib/ethersSetup'; | ||
import DemoButton from '$lib/DemoButton.svelte'; | ||
onMount(async () => { | ||
if (browser) { | ||
const mmInstalled = window.ethereum !== undefined; | ||
metamaskInstalled.set(mmInstalled); | ||
if (mmInstalled) { | ||
updateConnectedToSeplia(); | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
|
||
<div class="bg-black p-4 font-jetbrains rounded"> | ||
{#if !$metamaskInstalled} | ||
Install MetaMask to continue 🦊 | ||
{:else} | ||
<div>MetaMask is intalled ✅</div> | ||
{#if $connectedToSepolia } | ||
<div>Connected to Sepolia ✅</div> | ||
{:else} | ||
<DemoButton on:click={connectToSepolia}>Connect to Sepolia</DemoButton> | ||
{/if} | ||
{/if} | ||
</div> |