-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/mvc-labs/metalet-extension
- Loading branch information
Showing
10 changed files
with
173 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { computed, ref } from 'vue' | ||
|
||
interface Asset { | ||
name: string | ||
value: number | ||
} | ||
|
||
export const totalBalance = computed(() => { | ||
console.log('assetList', assetList.value) | ||
|
||
return assetList.value.reduce((sum, asset) => sum + asset.value, 0) | ||
}) | ||
|
||
export const assetList = ref<Asset[]>([]) | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { useQuery } from '@tanstack/vue-query' | ||
import { metaletApi, mvcApi } from './request' | ||
import { ComputedRef, Ref } from 'vue' | ||
import { SymbolUC, BRC20_SYMBOLS } from '@/lib/asset-symbol' | ||
|
||
type TokenType = "BRC20" | ||
|
||
interface Tick { | ||
token: SymbolUC | ||
tokenType: TokenType | ||
balance: string | ||
availableBalance: string | ||
transferBalance: string | ||
} | ||
|
||
export type Balance = { | ||
address: string | ||
confirmed?: number | ||
unconfirmed?: number | ||
availableBalance?: number | ||
transferBalance?: number | ||
total: number | ||
} | ||
|
||
export const fetchSpaceBalance = async (address: string): Promise<Balance> => { | ||
const balance: any = await mvcApi(`/address/${address}/balance`).get() | ||
balance.total = balance.confirmed + balance.unconfirmed | ||
return balance | ||
} | ||
|
||
export const fetchBtcBalance = async (address: string): Promise<Balance> => { | ||
const balance: any = await metaletApi(`/address/balance`) | ||
.get({ address, chain: 'btc' }) | ||
.then((res) => res.data) | ||
|
||
balance.total = balance.confirmed + balance.unconfirmed | ||
|
||
return balance | ||
} | ||
|
||
export const fetchBRC20Balance = async (address: string, symbol: SymbolUC): Promise<Balance> => { | ||
const { tickList } = await metaletApi(`/address/brc20/asset`) | ||
.get({ address, chain: 'btc', tick: symbol.toLowerCase() }) | ||
.then((res) => res.data) | ||
|
||
const tickAsset = tickList.find((tick: Tick) => tick.token === symbol) | ||
|
||
if (tickAsset) { | ||
return { | ||
address, | ||
availableBalance: Number(tickAsset.availableBalance), | ||
transferBalance: Number(tickAsset.transferBalance), | ||
total: Number(tickAsset.balance), | ||
} | ||
} | ||
|
||
return { | ||
address, | ||
availableBalance: 0, | ||
transferBalance: 0, | ||
total: 0, | ||
} | ||
} | ||
|
||
export const doNothing = async (): Promise<Balance> => { | ||
return { | ||
address: '', | ||
confirmed: 0, | ||
unconfirmed: 0, | ||
total: 0, | ||
} | ||
} | ||
|
||
export const useBalanceQuery = (address: Ref, symbol: SymbolUC, options: { enabled: ComputedRef<boolean> }) => { | ||
return useQuery({ | ||
queryKey: ['balance', { address, symbol }], | ||
queryFn: () => { | ||
switch (symbol) { | ||
case 'SPACE': | ||
return fetchSpaceBalance(address.value) | ||
case 'BTC': | ||
return fetchBtcBalance(address.value) | ||
default: { | ||
if (BRC20_SYMBOLS.includes(symbol)) { | ||
return fetchBRC20Balance(address.value, symbol) | ||
} | ||
return doNothing() | ||
} | ||
} | ||
}, | ||
...options, | ||
|
||
}) | ||
} |