Skip to content

Commit

Permalink
Handle stake/withdraw when user account is a WalletConnect account bu…
Browse files Browse the repository at this point in the history
…t no active session
  • Loading branch information
ccali11 committed Dec 19, 2023
1 parent e8e0913 commit cc58800
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 0 additions & 1 deletion apps/mvp/src/composables/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ export default function useAuth() {
} else if (provider === "Trezor") {
await loginWithTrezor(loginCredentials)
} else if (provider === "WalletConnect") {
console.log("logging in with wallet connect")
await loginWithWalletConnectV2(loginCredentials)
} else {
console.log("Sign up not yet supported for this wallet provider")
Expand Down
3 changes: 3 additions & 0 deletions apps/mvp/src/composables/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export default function useStaking() {
async function deposit({ amount, walletProvider, type, pathIndex }: { amount: string, walletProvider: ProviderString, type: "default" | "eigen", pathIndex: number | undefined}) {
stakeWithdrawError.value = ""
try {
// This is currently handling the connectWalletConnectV2 if no current session
const activeNetwork = await detectActiveNetwork(walletProvider)
console.log("activeNetwork :>> ", activeNetwork)
if (activeNetwork !== 5) {
await switchEthersNetwork(walletProvider, "0x5")
return window.location.reload()
Expand Down Expand Up @@ -184,6 +186,7 @@ export default function useStaking() {
async function withdraw({ amount, walletProvider, type }: { amount: string, walletProvider: ProviderString, type: "default" | "eigen" }) {
try {
stakeWithdrawError.value = ""
// This is currently handling the connectWalletConnectV2 if no current session
const activeNetwork = await detectActiveNetwork(walletProvider)
if (activeNetwork !== 5) {
await switchEthersNetwork(walletProvider, "0x5")
Expand Down
17 changes: 17 additions & 0 deletions apps/mvp/src/composables/walletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function useWalletConnectV2() {
}
const rpcUrl = customRpcs[chainId]
if (!rpcUrl) throw new Error(`RPC URL not found for chainId: ${chainId}`)
if (ethereumProvider.value.accounts.length > 0) throw new Error("Prompt user to simply change accounts to add it to their account")
await ethereumProvider.value.connect()
createEthersProvider()
// const _accounts = await ethereumProvider.value.enable()
Expand Down Expand Up @@ -181,6 +182,22 @@ export default function useWalletConnectV2() {
const handleAccountsChanged = async (walletConnectAccounts: Array<string>) => {
console.log("ACCOUNTS CHANGED EVENT", walletConnectAccounts)
if (!user.value && accounts.value.length > 0) return window.location.reload()

// Check which accounts are new
const userAccounts = user.value?.accounts.map((account) => account.address.toLowerCase().trim())
const newAccounts = walletConnectAccounts.filter((account) => !userAccounts?.includes(account.toLowerCase().trim()))

// Add new accounts to user
if (newAccounts.length > 0) {
// await addAccountToUser(newAccounts) // TODO: Enable this (review with @DemogorGod)
const newAccountsWithBalances = await Promise.all(newAccounts.map(async (account) => {
return {
address: account,
balance: (await getEthersBalance(account)).toString()
}
}))
walletConnectSelectedAccount.value = [...walletConnectSelectedAccount.value, ...newAccountsWithBalances]
}
}

// Event handler for chainChanged
Expand Down
5 changes: 4 additions & 1 deletion apps/mvp/src/composables/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useWalletConnectV2 from "@/composables/walletConnectV2"

const installedWallets = ref([] as ProviderString[])
const { browserProvidersList, detectActiveEthersWalletAddress, getBrowserProvider } = useEthers()
const { web3Provider } = useWalletConnectV2()
const { connectWalletConnectV2, web3Provider } = useWalletConnectV2()

export default function useWallets() {
async function detectActiveNetwork(providerString: ProviderString): Promise<number> {
Expand All @@ -15,6 +15,9 @@ export default function useWallets() {
const chainId = parseInt(await provider.request({ method: "eth_chainId" }), 16)
return chainId
} else if (providerString === "WalletConnect") {
if (!web3Provider.value) {
await connectWalletConnectV2("5")
}
return web3Provider.value.provider.chainId
} else if (providerString === "Ledger") {
// TODO: Determine if there is a way to implement with Ledger or if have to rely on selected network on device
Expand Down

0 comments on commit cc58800

Please sign in to comment.