wallet connection fixes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {ethers} from "ethers";
|
||||
import {BrowserProvider, ethers} from "ethers";
|
||||
import {useStore} from "@/store/store";
|
||||
import {socket} from "@/socket.js";
|
||||
import {contractOrNull, vaultAddress} from "@/blockchain/contract.js";
|
||||
@@ -9,6 +9,9 @@ import {ref} from "vue";
|
||||
import {metadataMap} from "@/version.js";
|
||||
|
||||
|
||||
export let provider = null
|
||||
|
||||
|
||||
export const useWalletStore = defineStore('wallet', ()=>{
|
||||
// this is what the wallet is logged into. it could be different than the application's store.chainId.
|
||||
const chainId = ref(0)
|
||||
@@ -31,6 +34,7 @@ export const useWalletStore = defineStore('wallet', ()=>{
|
||||
|
||||
|
||||
export function onChainChanged(chainId) {
|
||||
console.log('onChainChanged', chainId)
|
||||
chainId = Number(chainId)
|
||||
const store = useStore()
|
||||
const ws = useWalletStore()
|
||||
@@ -41,6 +45,8 @@ export function onChainChanged(chainId) {
|
||||
console.log('app chain changed', chainId)
|
||||
store.chainId = chainId
|
||||
store.account = null
|
||||
provider = new BrowserProvider(window.ethereum, chainId)
|
||||
updateAccounts(chainId, provider)
|
||||
}
|
||||
else {
|
||||
console.log('app chain NOT changed')
|
||||
@@ -77,6 +83,7 @@ function changeAccounts(chainId, accounts) {
|
||||
}
|
||||
|
||||
function onAccountsChanged(accounts) {
|
||||
console.log('onAccountsChanged', accounts)
|
||||
const store = useStore()
|
||||
const ws = useWalletStore()
|
||||
if (accounts.length === 0 || accounts[0] !== store.account)
|
||||
@@ -84,11 +91,17 @@ function onAccountsChanged(accounts) {
|
||||
}
|
||||
|
||||
export function detectChain() {
|
||||
try {
|
||||
window.ethereum.on('chainChanged', onChainChanged);
|
||||
window.ethereum.on('accountsChanged', onAccountsChanged);
|
||||
}
|
||||
catch (e) {
|
||||
console.log('Could not connect change hooks to wallet', e)
|
||||
return
|
||||
}
|
||||
new ethers.BrowserProvider(window.ethereum).getNetwork().then((network)=>{
|
||||
const chainId = network.chainId
|
||||
onChainChanged(chainId)
|
||||
window.ethereum.on('chainChanged', onChainChanged);
|
||||
window.ethereum.on('accountsChanged', onAccountsChanged);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -125,50 +138,50 @@ const errorHandlingProxy = {
|
||||
}
|
||||
|
||||
|
||||
export async function connectProvider(chainId) {
|
||||
console.log('connectProvider', chainId)
|
||||
try {
|
||||
return new ethers.BrowserProvider(window.ethereum, chainId)
|
||||
}
|
||||
catch (e) {
|
||||
console.log('provider error', e)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
export async function connectWallet(chainId) {
|
||||
console.log('connectWallet', chainId)
|
||||
const ws = useWalletStore()
|
||||
if (ws.chainId !== chainId && !await switchChain(chainId))
|
||||
return null
|
||||
const p = await connectProvider(chainId)
|
||||
if (p!==null) {
|
||||
await switchChain(chainId)
|
||||
if (provider!==null) {
|
||||
try {
|
||||
console.log('getSigner')
|
||||
return await p.getSigner();
|
||||
await provider.getSigner()
|
||||
await updateAccounts(chainId, provider)
|
||||
}
|
||||
catch (e) {
|
||||
if (e.reason!=='rejected')
|
||||
console.error(e, e.reason)
|
||||
return null
|
||||
}
|
||||
}
|
||||
else
|
||||
console.error('provider was null after chain switch')
|
||||
}
|
||||
|
||||
|
||||
export async function switchChain(chainId) {
|
||||
if (useWalletStore().chainId === chainId)
|
||||
return true
|
||||
try {
|
||||
await window.ethereum.request({
|
||||
"method": "wallet_switchEthereumChain",
|
||||
"params": [{"chainId": '0x' + chainId.toString(16)}]
|
||||
})
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
return
|
||||
await window.ethereum.request({
|
||||
"method": "wallet_switchEthereumChain",
|
||||
"params": [{"chainId": '0x' + chainId.toString(16)}]
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export async function addTestnet() {
|
||||
const info = {
|
||||
"chainId": "0x539",
|
||||
"chainName": "Dexorder Alpha Testnet",
|
||||
"rpcUrls": ["https://rpc.alpha.dexorder.trade"],
|
||||
"nativeCurrency": {
|
||||
"name": "Test Ethereum",
|
||||
"symbol": "TETH",
|
||||
"decimals": 18
|
||||
}
|
||||
};
|
||||
await window.ethereum.request({
|
||||
"method": "wallet_addEthereumChain",
|
||||
"params": [info]
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -198,7 +211,7 @@ async function _discoverVaults(owner) {
|
||||
s.vaults = []
|
||||
return
|
||||
}
|
||||
const provider = s.provider;
|
||||
console.log('provider', provider)
|
||||
if (!provider) {
|
||||
console.log('No provider')
|
||||
return // do not change whatever was already found
|
||||
@@ -350,10 +363,19 @@ function pendOrderAsTransaction(pend) {
|
||||
console.error('vault contract was null while sending order transaction', pend.vault)
|
||||
return null
|
||||
}
|
||||
if (!await switchChain(pend.chainId)) {
|
||||
console.log('user refused chain switch')
|
||||
pend.state = PendingOrderState.Rejected
|
||||
return null
|
||||
try {
|
||||
await switchChain(pend.chainId)
|
||||
}
|
||||
catch (e) {
|
||||
if(e.code===4001) {
|
||||
console.log('user refused chain switch')
|
||||
pend.state = PendingOrderState.Rejected
|
||||
return null
|
||||
}
|
||||
else {
|
||||
console.error('Unknown error while switching chain to pend order', e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
console.log('placing order', pend.id)
|
||||
const tx = await contract.placeDexorder(pend.order) // todo update status
|
||||
@@ -393,7 +415,7 @@ export function flushTransactions() {
|
||||
|
||||
async function asyncFlushTransactions() {
|
||||
const s = useStore()
|
||||
if( s.provider === null ) {
|
||||
if( provider === null ) {
|
||||
console.log('warning: asyncFlushOrders() cancelled due to null provider')
|
||||
return
|
||||
}
|
||||
@@ -403,7 +425,7 @@ async function asyncFlushTransactions() {
|
||||
console.log(`flushing ${senders.length} transactions`)
|
||||
let signer
|
||||
try {
|
||||
signer = await s.provider.getSigner();
|
||||
signer = await provider.getSigner();
|
||||
} catch (e) {
|
||||
// {
|
||||
// "code": -32002,
|
||||
|
||||
Reference in New Issue
Block a user