优化切换链

This commit is contained in:
CounterFire2023 2024-07-18 17:37:20 +08:00
parent d205b39930
commit 06da2ac759
3 changed files with 38 additions and 11 deletions

View File

@ -85,6 +85,11 @@ export class BlockChain {
this.wallet = new allProviders[walletType]();
const { provider, accounts } = await this.wallet.web3Provider();
await this.updateInfo({provider, accounts })
if ((walletType == 1 || walletType == 2) && this.store.passportAddress) {
await this.appendPassport();
} else if (walletType == 3 && this.store.eoaType) {
await this.restoreEoa();
}
return provider;
}
@ -126,6 +131,17 @@ export class BlockChain {
this.store.passportAddress = accounts[0];
this.store.$persist();
}
async restoreEoa() {
if (this.store.walletType != 3 || !this.store.eoaType) {
return;
}
this.eoaWallet = new allProviders[this.store.eoaType]();
const { provider, accounts } = await this.eoaWallet.web3Provider();
this.eoaProvider = provider;
this.store.eoaAddress = accounts[0];
this.store.$persist
}
/**
* 用于passport登录后, 添加eoa地址
*/
@ -139,13 +155,14 @@ export class BlockChain {
initData: { 1: 'MetaMask', 2: 'OKX Wallet', 3: 'Passport' },
disabled: [3]
});
const { errcode, errmsg, walletInstance, provider, accounts } = await rewardModal.show();
const { errcode, errmsg, walletInstance, wallet, provider, accounts } = await rewardModal.show();
if (errcode) {
console.log(`select address result : ${errmsg}`);
throw new Error(errmsg);
}
this.eoaProvider = provider;
this.store.eoaAddress = accounts[0];
this.store.eoaType = wallet;
this.store.$persist();
}
@ -162,19 +179,18 @@ export class BlockChain {
return token+suffix
}
// 检查是否已登录aoa
// 检查是否已登录eoa
get eoaLogined() {
return this.store.walletType != 3 || !!this.store.eoaAddress
return this.store.walletType == 1 || this.store.walletType == 2 || !!this.eoaProvider
}
/**
* 检查passport是否已登录
*/
get passportLogined() {
return this.store.walletType == 3 || !!this.store.passportAddress
return this.store.walletType == 3 || !!this.passportProvider
}
/**
* 如果是用passport登录的, 直接返回store中的token, 否则调用passport的getAccessToken
* @returns
@ -203,15 +219,24 @@ export class BlockChain {
async getChainId() {
return this.wallet.getChainId();
}
async getEoaChainId(provider) {
const chainId = await provider.provider.request({ method: "eth_chainId" });
return parseInt(chainId);
}
/**
* 检查并切换到目标链, 各上链前须调用该方法
*/
async checkAndChangeChain(targetChainId) {
async checkAndChangeChain(targetChainId, provider) {
targetChainId = targetChainId || cfgChainId;
let chainId = await this.getChainId();
if (!provider && this.store.walletType == 3) {
return
}
provider = provider || this.web3Provider;
let chainId = await this.getEoaChainId(provider);
if (chainId !== targetChainId) {
console.log(`current chain: ${chainId}, want: ${targetChainId}`)
chainId = await switchEthereumChain(this.web3Provider.provider, targetChainId);
chainId = await switchEthereumChain(provider.provider, targetChainId);
}
}

View File

@ -51,7 +51,7 @@ export class Locker {
console.log('lock nft on main', nft, tokenIds)
const chainId = parseInt(import.meta.env.VUE_APP_NET_ID_MAIN);
await this.bc.checkPassportLogin();
await this.bc.checkAndChangeChain(chainId);
await this.bc.checkAndChangeChain(chainId, this.bc.eoaProvider);
return this.execLock(this.bc.eoaProvider, lockAddressMain, nft, tokenIds)
}
@ -105,8 +105,7 @@ export class Locker {
console.log('unlock nft on main', nft, tokenIds)
const chainId = parseInt(import.meta.env.VUE_APP_NET_ID_MAIN);
await this.bc.checkPassportLogin();
await this.bc.checkAndChangeChain(chainId);
// const address = this.bc.store.eoaAddress
await this.bc.checkAndChangeChain(chainId, this.bc.eoaProvider);
return this.execUnlock(chainId, nft, tokenIds)
}

View File

@ -5,6 +5,7 @@ export const walletStore = defineStore(
"localwallet",
() => {
const walletType = ref();
const eoaType = ref();
const address = ref();
const chainId = ref();
const token = ref();
@ -28,6 +29,7 @@ export const walletStore = defineStore(
passportAddress.value = '';
refreshToken.value = '';
eoaAddress.value = '';
eoaType.value = '';
}
return {
walletType,
@ -39,6 +41,7 @@ export const walletStore = defineStore(
showAddress,
eoaAddress,
reset,
eoaType,
};
},
{