fix web3/js stuff, remove avax

main
lza_menace 3 years ago
parent fca8ed8176
commit 61a45b667b

@ -1,7 +1,135 @@
<script src="/static/js/vendor/metamask-onboarding-1.0.1.bundle.js"></script>
<script src="/static/js/vendor/noty-3.2.0.js"></script> <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' %} <!-- <script src="/static/js/vendor/web3-1.3.6.min.js"></script> -->
<script src="/static/js/vendor/metamask-onboarding-1.0.1.bundle.js"></script>
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', () => {
// Arm connect button
onboardMetaMask();
// unpoly config options
up.fragment.config.mainTargets.push('.container');
// Detect account changes in MetaMask
if (MetaMaskOnboarding.isMetaMaskInstalled()) {
window.ethereum.on('accountsChanged', (newAccounts) => {
window.location.href = '/disconnect';
});
}
})
async function getMetamaskAccount() {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts',
});
const account = accounts[0];
return account
}
async function getSignedData(publicAddress, jsonData) {
const signedData = await window.ethereum.request({
method: 'eth_signTypedData_v3',
params: [publicAddress, JSON.stringify(jsonData)]
});
console.log(signedData);
return signedData
}
async function onboardMetaMask(){
const onboarding = new MetaMaskOnboarding();
const onboardButton = document.getElementById('metamaskConnect');
let accounts;
let nonce = 0;
if (!onboardButton) {
return false;
}
onboardButton.onclick = async () => {
if (!MetaMaskOnboarding.isMetaMaskInstalled() && !window.ethereum) {
onboardButton.onclick = () => {
onboardButton.classList.add('is-loading');
onboardButton.disabled = true;
onboarding.startOnboarding();
};
} else if (accounts && accounts.length > 0) {
onboardButton.classList.remove('is-loading');
onboardButton.disabled = false;
onboarding.stopOnboarding();
} else {
try {
onboardButton.classList.add('is-loading');
onboardButton.disabled = true;
let userExists;
await switchNetwork();
const allAccounts = await window.ethereum.request({
method: 'eth_requestAccounts',
});
await fetch('/api/v1/user_exists?public_address=' + allAccounts[0])
.then((resp) => resp.json())
.then(function(data) {
if (!data['success']) {
console.log('error checking user_exists!')
return
}
console.log(data);
nonce = data['nonce'];
})
const msg = 'Authentication request from tubbymemes app! Verifying message with nonce ' + nonce
const signedData = await window.ethereum.request({
method: 'personal_sign',
params: [msg, allAccounts[0]]
});
console.log(`Signing data with msg "${msg}", address "${allAccounts[0]}", signed data: ${signedData}`)
await fetch('/api/v1/authenticate/metamask', {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({
'signed_data': signedData,
'public_address': allAccounts[0],
'nonce': nonce,
'message': msg,
})
})
.then((resp) => resp.json())
.then(function(data) {
console.log(data);
if (data['success']) {
window.location.href = '/';
}
})
} catch(e) {
console.log(e);
onboardButton.classList.remove('is-loading');
}
}
};
};
async function switchNetwork(){
// don't do this if no metamask (errors on coinbase wallet)
if (!MetaMaskOnboarding.isMetaMaskInstalled()) {
return false;
}
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x1' }],
});
} catch (switchError) {
// handle other "switch" errors
console.log('couldnt switch chain')
}
}
</script>
{% with messages = get_flashed_messages(with_categories=true) %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% if messages %}

@ -1,206 +0,0 @@
<script src="/static/js/vendor/web3-1.3.6.min.js"></script>
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', () => {
// Arm connect button
onboardMetaMask();
// Detect account changes in MetaMask
if (MetaMaskOnboarding.isMetaMaskInstalled()) {
window.ethereum.on('accountsChanged', (newAccounts) => {
window.location.href = '/disconnect';
});
}
})
// Web3 and smart contract details
let contractABI = {{ config.CONTRACT_ABI | tojson }};
let contractAddress = "{{ config.CONTRACT_ADDRESS }}";
let wowxABI = {{ config.WOWX_ABI | tojson }};
let wowxAddress = "{{ config.WOWX_ADDRESS }}";
const w3 = new Web3(Web3.givenProvider);
const contract = new w3.eth.Contract(contractABI, contractAddress);
const wowxContract = new w3.eth.Contract(wowxABI, wowxAddress);
async function getMetamaskAccount() {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts',
});
const account = accounts[0];
return account
}
async function getSignedData(publicAddress, jsonData) {
const signedData = await window.ethereum.request({
method: 'eth_signTypedData_v3',
params: [publicAddress, JSON.stringify(jsonData)]
});
console.log(signedData);
return signedData
}
async function onboardMetaMask(){
const onboarding = new MetaMaskOnboarding();
const onboardButton = document.getElementById('metamaskConnect');
let accounts;
let nonce = 0;
if (!onboardButton) {
return false;
}
onboardButton.onclick = async () => {
if (!MetaMaskOnboarding.isMetaMaskInstalled()) {
onboardButton.onclick = () => {
onboardButton.classList.add('is-loading');
onboardButton.disabled = true;
onboarding.startOnboarding();
};
} else if (accounts && accounts.length > 0) {
onboardButton.classList.remove('is-loading');
onboardButton.disabled = false;
onboarding.stopOnboarding();
} else {
try {
onboardButton.classList.add('is-loading');
onboardButton.disabled = true;
let userExists;
await addAvalancheNetwork();
const allAccounts = await window.ethereum.request({
method: 'eth_requestAccounts',
});
await fetch('/api/v1/user_exists?public_address=' + allAccounts[0])
.then((resp) => resp.json())
.then(function(data) {
if (!data['success']) {
console.log('error checking user_exists!')
return
}
console.log(data);
nonce = data['nonce'];
})
const msg = 'Authentication request from tubbymemes app! Verifying message with nonce ' + nonce
const signedData = await window.ethereum.request({
method: 'personal_sign',
params: [msg, allAccounts[0]]
});
console.log(`Signing data with msg "${msg}", address "${allAccounts[0]}", signed data: ${signedData}`)
await fetch('/api/v1/authenticate/metamask', {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({
'signed_data': signedData,
'public_address': allAccounts[0],
'nonce': nonce,
'message': msg,
})
})
.then((resp) => resp.json())
.then(function(data) {
console.log(data);
if (data['success']) {
window.location.href = '/';
}
})
} catch(e) {
console.log(e);
onboardButton.classList.remove('is-loading');
}
}
};
// connectButton();
};
async function addWOWX() {
let tokenAddress = '{{ config.WOWX_ADDRESS }}';
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) {
await addAvalancheNetwork();
if (ipfsHash != '') {
const tokenId = await contract.methods.metadataTokenId(ipfsHash).call();
return tokenId;
}
}
async function isMinted(ipfsHash) {
await addAvalancheNetwork();
if (ipfsHash != '') {
const tokenId = await getTokenID(ipfsHash)
if (tokenId == 0) {
return false;
} else {
return true;
}
} else {
return false
}
}
</script>
Loading…
Cancel
Save