Here`s my log.
Ethereum is defined. Starting network switch…
Switching to Avalanche Fuji C-Chain with chainId 0xa869…
Attempting to switch to Avalanche Fuji C-Chain using RPC URL: https://api.avax-test.network/ext/bc/C/rpc...
Switched to Avalanche Fuji C-Chain successfully using RPC URL: http
access.js:51
s://api.avax-test.network/ext/bc/C/rpc.
Checking contract deployment… Contract is deployed. Requesting accounts…
Fetching accounts…Accounts found. Fetching or registering username…
MetaMask RPC Error: Internal JSON-RPC error.
►(code: -32603, message: ‘Internal JSON-RPC error., data: {…}}
User is not registered. Proceeding with registration…
My log stops here.
My .js functions
import { ethers } from 'ethers';
import abi from './abi.json';
const checkContractDeployment = async (provider, address) => {
const code = await provider.getCode(address);
return code !== '0x';
};
const switchToAvalanche = async () => {
const avalancheChainId = '0xa869'; // Avalanche Fuji C-Chain
const avalancheChainName = 'Avalanche Fuji C-Chain';
const avalancheRpcUrls = [
'https://api.avax-test.network/ext/bc/C/rpc',
'https://rpc.ankr.com/avalanche_fuji',
'https://avalanche-fuji-c-chain.publicnode.com',
];
const avalancheBlockExplorerUrl = 'https://testnet.snowtrace.io';
console.log(`Switching to ${avalancheChainName} with chainId ${avalancheChainId}...`);
let rpcUrlIndex = 0;
let successfulSwitch = false;
while (rpcUrlIndex < avalancheRpcUrls.length && !successfulSwitch) {
const avalancheRpcUrl = avalancheRpcUrls[rpcUrlIndex];
try {
console.log(`Attempting to switch to ${avalancheChainName} using RPC URL: ${avalancheRpcUrl}...`);
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: avalancheChainId }],
});
console.log(`Switched to ${avalancheChainName} successfully using RPC URL: ${avalancheRpcUrl}.`);
successfulSwitch = true;
} catch (switchError) {
console.error(`Error switching to ${avalancheChainName} using RPC URL: ${avalancheRpcUrl}:`, switchError);
if (switchError.code === 4902) {
try {
console.log(`Adding ${avalancheChainName} with RPC URL: ${avalancheRpcUrl}...`);
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: avalancheChainId,
chainName: avalancheChainName,
rpcUrls: [avalancheRpcUrl],
blockExplorerUrls: [avalancheBlockExplorerUrl],
}],
});
console.log(`Added ${avalancheChainName} successfully using RPC URL: ${avalancheRpcUrl}.`);
successfulSwitch = true;
} catch (addError) {
console.error(`Failed to add ${avalancheChainName} to MetaMask using RPC URL: ${avalancheRpcUrl}:`, addError);
}
} else if (switchError.code === -32002) {
console.error(`A request to add or switch to ${avalancheChainName} is already pending. Please check MetaMask.`);
alert(`A request to add or switch to ${avalancheChainName} is already pending in MetaMask. Please open MetaMask and complete the request.`);
return;
}
}
rpcUrlIndex++;
}
if (!successfulSwitch) {
alert(`Failed to switch to ${avalancheChainName}. Please try again.`);
}
};
export const validation = async (router, username, setError) => {
validateUsername(username, setError);
if (document.querySelector("#errorAccess").innerHTML !== "") {
return;
}
console.log('Starting validation...');
if (typeof window.ethereum !== 'undefined') {
console.log('Ethereum is defined. Starting network switch...');
await switchToAvalanche();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contractAddress = "0xf97d82fd7203d74Aa4a169F933992e350445D8fd"; // Update with your new contract address
const userAuthContract = new ethers.Contract(contractAddress, abi, signer);
console.log('Checking contract deployment...');
try {
const isContractDeployed = await checkContractDeployment(provider, contractAddress);
if (!isContractDeployed) {
console.error('Contract not deployed at this address.');
await router.push('/access?error=Contract not deployed at this address.');
return;
}
} catch (error) {
console.error('Error during contract deployment check:', error);
await router.push('/access?error=' + encodeURIComponent(error.message));
return;
}
console.log('Contract is deployed. Requesting accounts...');
try {
await provider.send('eth_requestAccounts', []);
console.log('Fetching accounts...');
const accounts = await provider.listAccounts();
if (!accounts || accounts.length === 0) {
await router.push('/access?error=No accounts found. Please login to MetaMask.');
return;
}
console.log('Accounts found. Fetching or registering username...');
let registeredUsername;
try {
registeredUsername = await userAuthContract.getUser(); // No argument needed
console.log('Registered username:', registeredUsername);
} catch (error) {
if (error.data && error.data.message.includes('User not registered')) {
console.log('User is not registered. Proceeding with registration...');
if (confirm('You are about to create a new account. Is this what you would like?')) {
const tx = await userAuthContract.register(username);
await tx.wait();
console.log('User registered successfully.');
const logged = await makeLog(username);
if (logged === 200) {
await router.push('/');
} else {
await router.push('/access?error=' + encodeURIComponent(logged));
}
} else {
setusernameError('Invalid user.', setError);
}
} else {
throw error; // Re-throw if it's a different error
}
}
} catch (providerError) {
console.error('Error requesting accounts:', providerError);
await router.push('/access?error=' + encodeURIComponent(providerError.message));
}
} else {
document.querySelector(".overlay").style.display = "flex";
}
};
My .sol code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract UserAuth {
struct User {
string username;
address userAddress;
}
mapping(address => User) private users;
mapping(string => address) private usernames;
event UserRegistered(address indexed userAddress, string username);
function register(string memory _username) public {
require(bytes(_username).length > 0, "Username cannot be empty");
require(users[msg.sender].userAddress == address(0), "User already registered");
require(usernames[_username] == address(0), "Username already taken");
// Register the new user
users[msg.sender] = User(_username, msg.sender);
usernames[_username] = msg.sender;
emit UserRegistered(msg.sender, _username);
}
function getUser() public view returns (string memory) {
require(users[msg.sender].userAddress != address(0), "User not registered");
return users[msg.sender].username;
}
}
Can you figure out what is causing the code to stop here?
const tx = await userAuthContract.register(username);
The last thing that happens is select yes in this if.
if (confirm('You are about to create a new account. Is this what you would like?')) {