From 2c2782797a6a729fd4fef7b3f34055267c53392e Mon Sep 17 00:00:00 2001 From: lza_menace Date: Wed, 29 Dec 2021 02:02:48 -0800 Subject: [PATCH] improve js and onboarding flow --- suchwowx/static/js/main.js | 32 ++++++++++++++++++++++++ suchwowx/static/js/metamask.js | 3 ++- suchwowx/templates/includes/navbar.html | 2 +- suchwowx/templates/includes/scripts.html | 4 +-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/suchwowx/static/js/main.js b/suchwowx/static/js/main.js index b479bd1..4aef5d2 100644 --- a/suchwowx/static/js/main.js +++ b/suchwowx/static/js/main.js @@ -6,3 +6,35 @@ async function getSignedData(publicAddress, jsonData) { console.log(signedData); return signedData } + +async function confirmAvalanche(){ + try { + await ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: '0xA86A' }], + }); + } catch (switchError) { + // This error code indicates that the chain has not been added to MetaMask. + if (switchError.code === 4902) { + try { + await ethereum.request({ + method: 'wallet_addEthereumChain', + params: [{ + chainId: '0xA86A', + chainName: 'Avalanche', + nativeCurrency: { + name: 'Avalanche', + symbol: 'AVAX', + decimals: 18, + }, + rpcUrls: ['https://api.avax.network/ext/bc/C/rpc'], + blockExplorerUrls: ['https://snowtrace.io'], + }], + }); + } catch (addError) { + // handle "add" error + } + } + // handle other "switch" errors + } +} diff --git a/suchwowx/static/js/metamask.js b/suchwowx/static/js/metamask.js index 190edd3..78297f2 100644 --- a/suchwowx/static/js/metamask.js +++ b/suchwowx/static/js/metamask.js @@ -1,5 +1,5 @@ window.addEventListener('DOMContentLoaded', () => { - onboardMetaMask() + onboardMetaMask(); }) async function onboardMetaMask(){ @@ -25,6 +25,7 @@ async function onboardMetaMask(){ onboardButton.classList.add('is-loading'); onboardButton.disabled = true; let userExists; + await confirmAvalanche(); const allAccounts = await window.ethereum.request({ method: 'eth_requestAccounts', }); diff --git a/suchwowx/templates/includes/navbar.html b/suchwowx/templates/includes/navbar.html index 2382709..6a3eea6 100644 --- a/suchwowx/templates/includes/navbar.html +++ b/suchwowx/templates/includes/navbar.html @@ -11,7 +11,7 @@ Profile Disconnect {% if current_user.is_moderator() %} - Mod + Mod {% endif %} New Meme {% else %} diff --git a/suchwowx/templates/includes/scripts.html b/suchwowx/templates/includes/scripts.html index 48fdd60..ef28c93 100644 --- a/suchwowx/templates/includes/scripts.html +++ b/suchwowx/templates/includes/scripts.html @@ -1,10 +1,10 @@ + + {% if not current_user.is_authenticated %} {% endif %} - - {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %}