Не запускается окно WalletConnect JS
Появилась ошибка с коннектом крипто-кошелька. С ПК ошибок никаких не возникает, но на мобильных устройствах ошибка:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'code') at WalletConnector.connectWallet
Строка на которую ведёт ошибка: if (e.code == "4001") {
Код JS
<script type="text/javascript">
// You have to refer to default since it was bundled for ESModules
// but after that the documentation will be the same
const Web3Modal = window.Web3Modal.default;
const WalletConnectProvider = window.WalletConnectProvider.default;
let Web3Provider;
let Web3Instance;
const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: "59610ce9402a4c58a6179c697d5385fa",
}
},
'custom-walletlink': {
display: {
logo: 'https://play-lh.googleusercontent.com/PjoJoG27miSglVBXoXrxBSLveV6e3EeBPpNY55aiUUBM9Q1RCETKCOqdOkX2ZydqVf0',
name: 'Coinbase',
description: 'Connect to Coinbase Wallet (not Coinbase App)',
},
options: {
appName: 'Coinbase', // Your app name
networkUrl: `https://mainnet.infura.io/v3/59610ce9402a4c58a6179c697d5385fa`,
chainId: 1,
},
package: WalletLink,
connector: async (_, options) => {
const { appName, networkUrl, chainId } = options
const walletLink = new WalletLink({
appName,
})
const provider = walletLink.makeWeb3Provider(networkUrl, chainId)
await provider.enable()
return provider
},
},
/* See Provider Options Section */
};
const web3Modal = new Web3Modal({
network: "mainnet", // optional
cacheProvider: false, // optional
providerOptions // required
});
</script>
<script>
const WalletConnector = function (messageBox, connectWalletButton, disconnectWalletButton) {
this.messageBox = messageBox ? messageBox : document.getElementById('message'),
this.connectWalletButton = connectWalletButton ? connectWalletButton : document.getElementById('connect-wallet'),
this.disconnectWalletButton = disconnectWalletButton ? disconnectWalletButton : document.getElementById('disconnect-wallet'),
this.account = "",
this.walletConnected = false,
this.init = function () {
if (!(this.connectWalletButton && this.connectWalletButton instanceof Element)) {
throw "connectWalletButton must be a DOM Element"
}
if (!(this.disconnectWalletButton && this.disconnectWalletButton instanceof Element)) {
throw "disconnectWalletButton must be a DOM Element"
}
this.connectWalletButton.addEventListener('click', () => {
this.connectWallet()
})
this.disconnectWalletButton.addEventListener('click', () => {
this.disconnectWallet()
})
},
this.checkProviderIfLoggedIn = async function () {
if (this.walletConnected && providerLoggedInWith == 'walletconnect') {
Web3Provider = await web3Modal.connectTo('walletconnect')
}
},
this.showMessage = function (message) {
this.messageBox.innerText = message
},
this.connectWallet = async function () {
try {
Web3Provider = await web3Modal.connect()
this.account = await this.fetchAccountData()
this.walletConnected = true
this.showMessage("Here's your Wallet: " + this.account)
} catch (e) {
if (e.code == "4001") {
this.showMessage(e.message)
} else {
this.showMessage("Couldn't get the wallet connection")
}
console.log("Couldn't get a wallet connection")
console.log(e)
}
this.initProviderEvents();
},
this.disconnectWallet = async function () {
// Try to close the web3 session
try {
await Web3Provider.close()
} catch (e) {
console.log(e)
}
// Also clear the cacheProvider so it asks for wallet options again, instead of asking the login of previous provider
try {
await web3Modal.clearCachedProvider();
} catch (e) {
console.log(e)
}
},
this.fetchAccountData = async function () {
// Get a Web3 instance for the wallet
Web3Instance = new Web3(Web3Provider)
// Get list of accounts of the connected wallet
const accounts = await Web3Instance.eth.getAccounts()
selectedAccount = accounts[0].toLowerCase()
return selectedAccount
},
this.initProviderEvents = function () {
// Subscribe to accounts change
Web3Provider.on("networkChanged", (accounts) => {
console.log("networkChanged");
console.log(accounts);
});
Web3Provider.on("accountsChanged", (accounts) => {
console.log("accountsChanged");
console.log(accounts);
});
// Subscribe to chainId change
Web3Provider.on("chainChanged", (chainId) => {
console.log("chainChanged");
console.log(chainId);
});
// Subscribe to provider connection
Web3Provider.on("connect", (info) => {
console.log("connect");
console.log(info);
});
// Subscribe to provider disconnection
Web3Provider.on("disconnect", (error) => {
this.showMessage("Disconnected")
console.log("disconnect");
console.log(error);
});
}
}
let walletConnector = new WalletConnector()
walletConnector.init();
</script>
Помогите пожалуйста)