chainInfo from server, routes, wallet rework

This commit is contained in:
Tim Olson
2023-10-04 03:40:47 -04:00
parent e7a9600c54
commit 7239987867
26 changed files with 603 additions and 297 deletions

View File

@@ -1,86 +1,36 @@
import {ethers} from "ethers";
import {useStore} from "@/store/store";
const store = useStore()
export let provider = null
function onChainChanged(chainId) {
chainId = Number(chainId)
console.log('chain changed', chainId)
const store = useStore()
store.chainId = chainId
provider = new ethers.BrowserProvider(window.ethereum, chainId)
}
class Wallet {
connected = false
_provider = null
_signer = null
_onSignerInits = {}
_enabled = true // prevents tight loops on errors
async connect() {
this._enabled = true
if( !this.connected )
await this.signer()
function onAccountsChanged(accounts) {
console.log('accounts changed', accounts)
const store = useStore()
if( accounts.length === 0 ) {
store.account = null
}
async provider() {
if (this._provider === null && this._enabled) {
console.log('creating provider')
const provider = new ethers.BrowserProvider(window.ethereum, store.chain.id)
await provider.getNetwork() // this invokes a check on having the correct network connected
this._provider = provider
console.log('wallet connected')
}
return this._provider
}
async signer() {
/*
if( !store.geo.approved ) {
console.log('not approved')
this._connected(false)
return null
}
*/
const provider = await this.provider()
if( provider === null ) {
console.log('provider null')
this._connected(false)
return null
}
const signer = await provider.getSigner()
if( this._signer?.address !== signer.address ) {
console.log('new signer', signer.address)
this._signer = signer
for (const key in this._onSignerInits) {
try {
await this._onSignerInits[key](signer)
}
catch (e) {
console.log('during onSignerInit', e)
}
}
console.log('wallet connected')
}
this._connected(true)
return signer
}
async address() {
const signer = await this.signer()
if( signer === null )
return null
return await signer.getAddress()
}
_connected(value) {
this.connected = value
store.$patch({wallet:{connected:value}})
}
onSignerInit(id, cb) {
this._onSignerInits[id] = cb
else if (accounts[0] !== store.account) {
store.account = accounts[0]
}
}
export async function watchWallet() {
const chainId = (await new ethers.BrowserProvider(window.ethereum).getNetwork()).chainId
onChainChanged(chainId)
window.ethereum.on('chainChanged', onChainChanged);
window.ethereum.on('accountsChanged', onAccountsChanged);
}
const handler = {
const errorHandlingProxy = {
get(target, prop, proxy) {
const got = Reflect.get(target, prop, proxy);
if( typeof got !== 'function' ) {
@@ -95,8 +45,11 @@ const handler = {
target._connected(false)
target._enabled = false
if( x.code === 'NETWORK_ERROR' ) {
store.error('Wrong Blockchain', 'Your wallet is connected to a different blockchain. Please select '+import.meta.env.VITE_CHAIN_NAME+' in your wallet.')
console.log('wallet network error', x)
// todo available chain names
// store.error('Wrong Blockchain', 'Your wallet is connected to a different blockchain. Please select Arbitrum in your wallet.') // todo hardcoded arb
console.log('wallet chain error', x)
// store.chainId = store.chainInfo[]
throw x
}
else {
console.log('wallet error')
@@ -109,4 +62,4 @@ const handler = {
}
export default new Proxy(new Wallet(), handler)
// const wallet = new Proxy(new Wallet(), errorHandlingProxy);