优化第三方钱包网络切换功能
This commit is contained in:
parent
807e4d2e01
commit
4b9267929a
@ -58,27 +58,26 @@ export class ZWalletConnect {
|
|||||||
resolve && resolve(chainId);
|
resolve && resolve(chainId);
|
||||||
}
|
}
|
||||||
this.provider.on('chainChanged', onChainChange)
|
this.provider.on('chainChanged', onChainChange)
|
||||||
|
let params = {
|
||||||
|
chainId: toHexChainId(data.id),
|
||||||
|
chainName: data.name,
|
||||||
|
nativeCurrency: {
|
||||||
|
name: data.symbol,
|
||||||
|
symbol: data.symbol,
|
||||||
|
decimals: data.decimals || 18
|
||||||
|
},
|
||||||
|
rpcUrls: [data.rpc],
|
||||||
|
blockExplorerUrls: [data.explorerurl]
|
||||||
|
}
|
||||||
this.provider.request({
|
this.provider.request({
|
||||||
method: 'wallet_addEthereumChain',
|
method: 'wallet_addEthereumChain',
|
||||||
params: [
|
params: [params]
|
||||||
{
|
|
||||||
chainId: toHexChainId(data.id),
|
|
||||||
chainName: data.name,
|
|
||||||
nativeCurrency: {
|
|
||||||
name: data.symbol,
|
|
||||||
symbol: data.symbol,
|
|
||||||
decimals: data.decimals || 18
|
|
||||||
},
|
|
||||||
blockExplorerUrls: [data.explorerurl],
|
|
||||||
rpcUrls: [data.rpc]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('add chain success, wait result');
|
console.log('add chain success, wait result');
|
||||||
})
|
})
|
||||||
.catch(err=>{
|
.catch(err=>{
|
||||||
console.error('add chain error: ', err);
|
console.error('add chain error: ', JSON.stringify(err));
|
||||||
this.provider.removeListener('chainChanged', onChainChange);
|
this.provider.removeListener('chainChanged', onChainChange);
|
||||||
reject && reject(err);
|
reject && reject(err);
|
||||||
})
|
})
|
||||||
|
35
src/index.ts
35
src/index.ts
@ -130,7 +130,7 @@ export default class JCWallet {
|
|||||||
start = Date.now()
|
start = Date.now()
|
||||||
this.data = loadData()
|
this.data = loadData()
|
||||||
console.log(`init wallet ext data cost: ${(Date.now() - start) / 1000}`)
|
console.log(`init wallet ext data cost: ${(Date.now() - start) / 1000}`)
|
||||||
this.init({ chains: [322, 97], password: this.password })
|
this.init({ chains: [322, 80001], password: this.password })
|
||||||
}
|
}
|
||||||
|
|
||||||
private initThirdPartyWallet(chain: number) {
|
private initThirdPartyWallet(chain: number) {
|
||||||
@ -140,6 +140,7 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.wConnect = new ZWalletConnect(this.rpc)
|
this.wConnect = new ZWalletConnect(this.rpc)
|
||||||
|
this.init({ chains: [322, 80001], password: this.password })
|
||||||
}
|
}
|
||||||
|
|
||||||
private init({ chains, password }: { chains: number[]; password: string }) {
|
private init({ chains, password }: { chains: number[]; password: string }) {
|
||||||
@ -155,6 +156,9 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (this.walletType !== WalletType.INTERNAL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!this.wallet || this.wallet.length === 0) {
|
if (!this.wallet || this.wallet.length === 0) {
|
||||||
// this.createAccount();
|
// this.createAccount();
|
||||||
this.newWallet(password)
|
this.newWallet(password)
|
||||||
@ -179,12 +183,29 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateCurrentChain(chainId: number) {
|
updateCurrentChain(chainId: number) {
|
||||||
const chainData = this.chainMap.get(chainId)
|
return new Promise((resolve, reject) => {
|
||||||
this._currentChain = chainData
|
if (this.walletType === WalletType.INTERNAL) {
|
||||||
this.web3.eth.setProvider(chainData.rpc)
|
const chainData = this.chainMap.get(chainId)
|
||||||
this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData)
|
this._currentChain = chainData
|
||||||
this.updateListType('tokens')
|
this.web3.eth.setProvider(chainData.rpc)
|
||||||
return this.currentChain
|
this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData)
|
||||||
|
this.updateListType('tokens')
|
||||||
|
resolve && resolve(this.currentChain);
|
||||||
|
} else {
|
||||||
|
console.log('third parth wallet change chain: ' + chainId)
|
||||||
|
const chainData = this.chainMap.get(chainId)
|
||||||
|
this.wConnect.addOrChangeChain(chainData)
|
||||||
|
.then(() => {
|
||||||
|
resolve && resolve(chainData);
|
||||||
|
})
|
||||||
|
.catch(err=>{
|
||||||
|
reject && reject(err);
|
||||||
|
})
|
||||||
|
// @ts-ignore
|
||||||
|
jumpToWallet()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateListType(type: string) {
|
updateListType(type: string) {
|
||||||
|
@ -84,6 +84,7 @@ class WalletConnectProvider extends ProviderEngine {
|
|||||||
};
|
};
|
||||||
|
|
||||||
request = async (payload: any): Promise<any> => {
|
request = async (payload: any): Promise<any> => {
|
||||||
|
console.log('[WalletProvider]send: ' + JSON.stringify(payload));
|
||||||
return this.send(payload);
|
return this.send(payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user