From 81cee067632ae91751c7dc666c6e73a7fdc7b2d4 Mon Sep 17 00:00:00 2001 From: AricRedemption Date: Tue, 26 Sep 2023 00:17:05 +0800 Subject: [PATCH 1/5] chore: add brc20 fiter and total balance --- src/lib/actions/btc/sign-psbt.ts | 2 +- src/lib/balance.ts | 21 +++++ src/pages/wallet/components/AssetItem.vue | 2 + src/pages/wallet/components/AssetList.vue | 19 +++-- src/pages/wallet/components/Balance.vue | 34 +------- src/queries/btc.ts | 23 +++++- src/queries/utxo.ts | 94 +++++++++++++++++++++++ 7 files changed, 157 insertions(+), 38 deletions(-) create mode 100644 src/queries/utxo.ts diff --git a/src/lib/actions/btc/sign-psbt.ts b/src/lib/actions/btc/sign-psbt.ts index 2534009..453894d 100644 --- a/src/lib/actions/btc/sign-psbt.ts +++ b/src/lib/actions/btc/sign-psbt.ts @@ -1,5 +1,5 @@ import ECPairFactory from 'ecpair' -import { getPrivateKey, getCurrentAccount, getAddressType, getPublicKey, getAddress } from '@/lib/account' +import { getPrivateKey, getAddressType, getPublicKey, getAddress } from '@/lib/account' import bitcoin, { address as PsbtAddress, networks } from 'bitcoinjs-lib' import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs' import { getNetwork } from '@/lib/network' diff --git a/src/lib/balance.ts b/src/lib/balance.ts index e69de29..6603b31 100644 --- a/src/lib/balance.ts +++ b/src/lib/balance.ts @@ -0,0 +1,21 @@ +import { computed, ref } from 'vue' + +interface Asset { + name: string + value: number +} + +export const totalBalance = computed(() => { + return assetList.value.reduce((sum, asset) => sum + asset.value, 0) +}) + +export const assetList = ref([]) + +export async function updateAsset(asset: Asset) { + const preAsset = assetList.value.find((assetItem) => assetItem.name === asset.name) + if (!preAsset) { + assetList.value.push(asset) + } else { + preAsset.value = asset.value + } +} diff --git a/src/pages/wallet/components/AssetItem.vue b/src/pages/wallet/components/AssetItem.vue index 9bd8219..18eaa3c 100644 --- a/src/pages/wallet/components/AssetItem.vue +++ b/src/pages/wallet/components/AssetItem.vue @@ -9,6 +9,7 @@ import { type Asset, getTagInfo, Tag } from '@/data/assets' import { useExchangeRatesQuery } from '@/queries/exchange-rates' import { getCurrentAccount } from '@/lib/account' import { getNetwork } from '@/lib/network' +import { updateAsset } from '@/lib/balance' const { asset } = defineProps<{ asset: Asset @@ -45,6 +46,7 @@ const exchange = computed(() => { const usdRate: number = Number(exchangeRate.value.price) const balanceInStandardUnit = balance.value.total / 10 ** asset.decimal const exchanged = balanceInStandardUnit * usdRate + updateAsset({ name: asset.symbol, value: exchanged }) return `$${exchanged.toFixed(2)} USD` } diff --git a/src/pages/wallet/components/AssetList.vue b/src/pages/wallet/components/AssetList.vue index de66b27..338aa4e 100644 --- a/src/pages/wallet/components/AssetList.vue +++ b/src/pages/wallet/components/AssetList.vue @@ -10,17 +10,26 @@ import useTokensQuery from '@/queries/tokens' import { getAssetsDisplay } from '@/lib/assets' import { BTCAssets, MVCAssets } from '@/data/assets' +import { fetchBtcAsset } from '@/queries/btc' + const router = useRouter() const address = ref('') -getAddress().then((add) => { - if (!add) return router.push('/welcome') - - address.value = add +const btcAssets = ref(BTCAssets) +getAddress().then((addr) => { + if (!addr) return router.push('/welcome') + address.value = addr + fetchBtcAsset(addr).then(userBRC20Asset => { + btcAssets.value = btcAssets.value + .filter(asset => + asset.symbol === 'BTC' || userBRC20Asset.includes(asset.symbol)) + }) }) + + const enabled = computed(() => !!address.value) -const btcAssets = ref(BTCAssets) + const listedAssets = ref(MVCAssets) const { isLoading, data: userOwnedTokens } = useTokensQuery(address, { enabled }) diff --git a/src/pages/wallet/components/Balance.vue b/src/pages/wallet/components/Balance.vue index 4274519..5e29f65 100644 --- a/src/pages/wallet/components/Balance.vue +++ b/src/pages/wallet/components/Balance.vue @@ -1,37 +1,10 @@