Reset subscription page on account/network changes

This commit is contained in:
silverpill 2022-05-23 00:25:08 +00:00
parent 044dcf2473
commit 57caecc88b
2 changed files with 27 additions and 17 deletions

View file

@ -10,10 +10,11 @@ export function getWeb3Provider(): Web3Provider {
return new Web3Provider(provider) return new Web3Provider(provider)
} }
export async function getWallet(): Promise<Signer | null> { export async function getWallet(
const provider = getWeb3Provider() provider?: Web3Provider,
): Promise<Signer | null> {
if (!provider) { if (!provider) {
return null provider = getWeb3Provider()
} }
try { try {
await provider.send("eth_requestAccounts", []) await provider.send("eth_requestAccounts", [])

View file

@ -61,7 +61,7 @@ import {
import Sidebar from "@/components/Sidebar.vue" import Sidebar from "@/components/Sidebar.vue"
import { useInstanceInfo } from "@/store/instance" import { useInstanceInfo } from "@/store/instance"
import { useCurrentUser } from "@/store/user" import { useCurrentUser } from "@/store/user"
import { getWallet } from "@/utils/ethereum" import { getWallet, getWeb3Provider } from "@/utils/ethereum"
const route = useRoute() const route = useRoute()
const { currentUser, ensureAuthToken } = $(useCurrentUser()) const { currentUser, ensureAuthToken } = $(useCurrentUser())
@ -96,11 +96,29 @@ function canConnectWallet(): boolean {
) )
} }
function disconnectWallet() {
subscriptionConfigured = null
subscription = null
walletConnected = false
}
async function connectWallet() { async function connectWallet() {
const signer = await getWallet() const web3Provider = getWeb3Provider()
const signer = await getWallet(web3Provider)
if (!signer) { if (!signer) {
return return
} }
const walletChainId = await web3Provider.send("eth_chainId", [])
console.info("chain ID:", walletChainId)
web3Provider.provider.on("chainChanged", (chainId: string) => {
disconnectWallet()
})
web3Provider.provider.on("accountsChanged", () => {
disconnectWallet()
})
web3Provider.provider.on("disconnect", () => {
disconnectWallet()
})
walletConnected = true walletConnected = true
checkSubscription() checkSubscription()
} }
@ -117,10 +135,7 @@ async function checkSubscription() {
if (!profileEthereumAddress) { if (!profileEthereumAddress) {
return return
} }
const signer = await getWallet() const signer = getWeb3Provider().getSigner()
if (!signer) {
return
}
subscription = await getSubscriptionInfo( subscription = await getSubscriptionInfo(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,
@ -148,10 +163,7 @@ async function onConfigureSubscription() {
return return
} }
// Subscription configuration tx can be send from any address // Subscription configuration tx can be send from any address
const signer = await getWallet() const signer = getWeb3Provider().getSigner()
if (!signer) {
return
}
const authToken = ensureAuthToken() const authToken = ensureAuthToken()
const signature = await getSubscriptionAuthorization(authToken) const signature = await getSubscriptionAuthorization(authToken)
const transaction = await configureSubscription( const transaction = await configureSubscription(
@ -185,10 +197,7 @@ async function onMakeSubscriptionPayment() {
if (!profileEthereumAddress) { if (!profileEthereumAddress) {
return return
} }
const signer = await getWallet() const signer = getWeb3Provider().getSigner()
if (!signer) {
return
}
const transaction = await makeSubscriptionPayment( const transaction = await makeSubscriptionPayment(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,