rework some javascript bits

main
lza_menace 3 years ago
parent ff684ef6f2
commit 0505a1eded

@ -8,51 +8,3 @@ async function notif(s, t) {
}).show(); }).show();
return return
} }
async function confirmAvalanche(){
let debug = true;
let chainId;
let rpcUrl;
let explorerUrl;
let name;
if (debug) {
name = 'Avalance Testnet';
chainId = '0xA869';
rpcUrl = 'https://api.avax-test.network/ext/bc/C/rpc';
explorerUrl = 'https://testnet.snowtrace.io';
} else {
name = 'Avalanche Mainnet';
chainId = '0xA86A';
rpcUrl = 'https://api.avax.network/ext/bc/C/rpc';
explorerUrl = 'https://snowtrace.io';
}
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId }],
});
} 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: chainId,
chainName: name,
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18,
},
rpcUrls: [rpcUrl],
blockExplorerUrls: [explorerUrl],
}],
});
} catch (addError) {
// handle "add" error
}
}
// handle other "switch" errors
}
}

@ -30,10 +30,11 @@ async function onboardMetaMask(){
onboarding.stopOnboarding(); onboarding.stopOnboarding();
} else { } else {
onboardButton.onclick = async () => { onboardButton.onclick = async () => {
try {
onboardButton.classList.add('is-loading'); onboardButton.classList.add('is-loading');
onboardButton.disabled = true; onboardButton.disabled = true;
let userExists; let userExists;
await confirmAvalanche(); await addAvalancheNetwork();
const allAccounts = await window.ethereum.request({ const allAccounts = await window.ethereum.request({
method: 'eth_requestAccounts', method: 'eth_requestAccounts',
}); });
@ -74,6 +75,10 @@ async function onboardMetaMask(){
window.location.href = '/'; window.location.href = '/';
} }
}) })
} catch(e) {
console.log(e);
onboardButton.classList.remove('is-loading');
}
} }
} }
}; };

@ -1,5 +1,6 @@
<script src="/static/js/vendor/noty-3.2.0.js"></script>
<script src="/static/js/main.js"></script> <script src="/static/js/main.js"></script>
{% include 'includes/web3.html' %}
{% if not current_user.is_authenticated %} {% if not current_user.is_authenticated %}
<script src="/static/js/vendor/metamask-onboarding-1.0.1.bundle.js"></script> <script src="/static/js/vendor/metamask-onboarding-1.0.1.bundle.js"></script>
<script src="/static/js/metamask.js"></script> <script src="/static/js/metamask.js"></script>
@ -7,6 +8,7 @@
{% with messages = get_flashed_messages(with_categories=true) %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% if messages %}
<script src="/static/js/vendor/noty-3.2.0.js"></script>
<script type="text/javascript"> <script type="text/javascript">
{% for category, message in messages %} {% for category, message in messages %}
{% if category == None %}{% set category = 'info' %}{% endif %} {% if category == None %}{% set category = 'info' %}{% endif %}

@ -13,6 +13,75 @@
return account return account
} }
async function addWOWX() {
let tokenAddress;
if ('{{ config.CONTRACT_TESTNET }}' == 'True') {
tokenAddress = '0xc6B039b1e0be1ba0B433f319898438E782E5dEBA';
} else {
tokenAddress = '0xba5dc7e77d150816b758e9826fcad2d74820e379';
}
try {
await ethereum.request({
method: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: tokenAddress.toLowerCase(),
symbol: 'WOWX',
decimals: 18,
image: 'https://github.com/wownerox/meta/raw/main/logo.png'
}
},
});
} catch(e) {
console.log(e);
}
}
async function addAvalancheNetwork(){
let chainId, rpcUrl, explorerUrl, name;
if ('{{ config.CONTRACT_TESTNET }}' == 'True') {
name = 'Avalanche Testnet';
chainId = '0xA869';
rpcUrl = 'https://api.avax-test.network/ext/bc/C/rpc';
explorerUrl = 'https://testnet.snowtrace.io';
} else {
name = 'Avalanche Mainnet';
chainId = '0xA86A';
rpcUrl = 'https://api.avax.network/ext/bc/C/rpc';
explorerUrl = 'https://snowtrace.io';
}
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId }],
});
} 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: chainId,
chainName: name,
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18,
},
rpcUrls: [rpcUrl],
blockExplorerUrls: [explorerUrl],
}],
});
} catch (addError) {
// handle "add" error
}
}
// handle other "switch" errors
}
}
async function getTokenID(ipfsHash) { async function getTokenID(ipfsHash) {
if (ipfsHash != '') { if (ipfsHash != '') {
const tokenId = await contract.methods.metadataTokenId(ipfsHash).call(); const tokenId = await contract.methods.metadataTokenId(ipfsHash).call();

@ -94,7 +94,6 @@
</section> </section>
{% include 'includes/footer.html' %} {% include 'includes/footer.html' %}
{% if meme.approved %} {% if meme.approved %}
{% include 'includes/web3.html' %}
<script type="text/javascript"> <script type="text/javascript">
window.addEventListener('DOMContentLoaded', async () => { window.addEventListener('DOMContentLoaded', async () => {
let minted = await isMinted('{{ meme.meta_ipfs_hash }}'); let minted = await isMinted('{{ meme.meta_ipfs_hash }}');

@ -70,7 +70,7 @@
</div> </div>
</section> </section>
{% include 'includes/footer.html' %} {% include 'includes/footer.html' %}
{% include 'includes/web3.html' %}
<script type="text/javascript"> <script type="text/javascript">
async function saveDB() { async function saveDB() {
await fetch('{{ url_for("api.update_user" ) }}', { await fetch('{{ url_for("api.update_user" ) }}', {

Loading…
Cancel
Save