Skip to content

Commit

Permalink
fix: watch user balance
Browse files Browse the repository at this point in the history
  • Loading branch information
MGrgr committed Nov 2, 2023
1 parent 9b38d78 commit 809d50e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/stores/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { useWallet } from 'solana-wallets-vue'
import type { PublicKey, PublicKeyInitData } from '@solana/web3.js'
import debounce from 'lodash-es/debounce'
import { lowerCase } from 'lodash-es'
import type { Policy, ServiceProvider } from '@mfactory-lab/albus-sdk'
import { AlbusClient, ProofRequestStatus } from '@mfactory-lab/albus-sdk'
Expand All @@ -25,6 +26,7 @@ export const useUserStore = defineStore('user', () => {
const wallet = useWallet()
const { publicKey } = wallet
const route = useRoute()
const emitter = useEmitter()

const client = computed(() => AlbusClient.factory(connectionStore.connection))
const { tokens } = useToken()
Expand Down Expand Up @@ -74,7 +76,7 @@ export const useUserStore = defineStore('user', () => {

const mints = computed(() => tokens.value.map(t => t.mint).filter(t => !!t))

async function getUserTokens() {
const getUserTokens = debounce(async () => {
if (!publicKey.value) {
return
}
Expand Down Expand Up @@ -116,7 +118,7 @@ export const useUserStore = defineStore('user', () => {
} finally {
state.loading = false
}
}
}, 500)

const tokenBalance = (token: string) => {
return state.tokens.find(t => [lowerCase(t.symbol), lowerCase(t.name)].includes(lowerCase(token)))?.balance ?? 0
Expand Down Expand Up @@ -166,6 +168,7 @@ export const useUserStore = defineStore('user', () => {
state.tokens = []
}
}, { immediate: true })
emitter.on(ACCOUNT_CHANGE_EVENT, getUserTokens)

return {
state,
Expand Down

0 comments on commit 809d50e

Please sign in to comment.