Skip to content

Commit

Permalink
feat: Add testnet api
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Jan 30, 2024
1 parent f1b80c6 commit 5d55173
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/lib/wallets/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Account, getCurrentAccount, getAddressType, getAddress, getSigner } fro
export class BtcWallet {
private account?: Account = undefined

constructor() {}
constructor() { }

static async create() {
const wallet = new BtcWallet()
Expand Down Expand Up @@ -222,7 +222,7 @@ async function getPsbtAndSelectUtxos(recipient: string, amount: Decimal, feeRate
const payInput = await createPayInput({ utxo, payment, addressType })
psbt.addInput(payInput)
} catch (e: any) {
console.log(e)
throw new Error(e.message)
}
}

Expand Down
15 changes: 4 additions & 11 deletions src/queries/balance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComputedRef, Ref } from 'vue'
import { network } from '@/lib/network'
import { getNetwork, network } from '@/lib/network'
import { useQuery } from '@tanstack/vue-query'
import { SymbolTicker } from '@/lib/asset-symbol'
import { metaletApiV3, mvcApi, unisatApi } from './request'
Expand Down Expand Up @@ -46,16 +46,9 @@ export interface BitcoinBalance {
}

export const fetchBtcBalance = async (address: string): Promise<Balance> => {
if (network.value === 'testnet') {
const data = await unisatApi<BitcoinBalance>(`/address/balance`).get({ address })
return {
address,
total: Number(data.btc_amount) * 10 ** 8,
confirmed: Number(data.confirm_amount) * 10 ** 8,
unconfirmed: Number(data.pending_amount) * 10 ** 8,
}
}
const data = await metaletApiV3<BTCBalance>(`/address/btc-balance`).get({ address })
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
const data = await metaletApiV3<BTCBalance>(`/address/btc-balance`).get({ net, address })
return {
address,
total: new Decimal(data.balance).mul(1e8).toNumber(),
Expand Down
19 changes: 8 additions & 11 deletions src/queries/brc20s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { PageResult } from './types'
import { type Asset } from '@/data/assets'
import { getBRC20Logo } from '@/data/logos'
import { SymbolTicker } from '@/lib/asset-symbol'
import { metaletApiV3, metaletApiV2 } from './request'
import { metaletApiV3 } from './request'
import { getNetwork } from '@/lib/network'

export type Brc20 = {
symbol: SymbolTicker
Expand All @@ -11,14 +12,6 @@ export type Brc20 = {
transferBalance: string
}

type RawBrc20 = {
token: SymbolTicker
balance: string
tokenType: 'BRC20'
availableBalance: string
transferBalance: string
}

interface TickerInfo {
completeBlocktime: number
completeHeight: number
Expand All @@ -45,7 +38,9 @@ interface TickerInfo {
}

export const getTickerInfo = async (tick: string): Promise<TickerInfo> => {
return await metaletApiV3<TickerInfo>(`/brc20/tick/info`).get({ tick })
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return await metaletApiV3<TickerInfo>(`/brc20/tick/info`).get({ net, tick })
}

interface TokenBalance {
Expand All @@ -59,8 +54,10 @@ interface TokenBalance {
}

export const fetchBRC20Token = async (address: string): Promise<Asset[]> => {
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return (
await metaletApiV2<PageResult<TokenBalance>>(`/brc20/tokens`).get({ address, cursor: '0', size: '100000' })
await metaletApiV3<PageResult<TokenBalance>>(`/brc20/tokens`).get({ net, address, cursor: '0', size: '100000' })
).list.map(
(token) =>
({
Expand Down
8 changes: 6 additions & 2 deletions src/queries/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import dayjs from 'dayjs'
import { Ref, ComputedRef } from 'vue'
import { fetchBRC20Token } from './brc20s'
import { useQuery } from '@tanstack/vue-query'
import { ordersApi, metaletApiV2 } from '@/queries/request'
import { ordersApi, metaletApiV3 } from '@/queries/request'
import { getNetwork } from '@/lib/network'

interface Tick {
token: string
Expand Down Expand Up @@ -102,7 +103,10 @@ export interface AddressTokenSummary {
}

export async function fetchBRC20TokenDetail(address: string, ticker: string): Promise<AddressTokenSummary> {
return await metaletApiV2<AddressTokenSummary>('/brc20/token-summary').get({
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return await metaletApiV3<AddressTokenSummary>('/brc20/token-summary').get({
net,
address,
ticker: encodeURIComponent(ticker),
})
Expand Down
7 changes: 5 additions & 2 deletions src/queries/inscribe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ref, ComputedRef } from 'vue'
import { getNetwork } from '@/lib/network'
import { useQuery } from '@tanstack/vue-query'
import { metaletApiV2, metaletApiV3 } from './request'
import { metaletApiV3 } from './request'
import { Buffer } from 'buffer'

export interface PreInscribe {
Expand Down Expand Up @@ -96,7 +96,10 @@ export async function getBRCInscriptions(
cursor = 0,
size = 10
): Promise<{ list: Inscription[]; total: number }> {
return await metaletApiV2<{ list: Inscription[]; total: number }>('/address/inscriptions').get({
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return await metaletApiV3<{ list: Inscription[]; total: number }>('/address/inscriptions').get({
net,
address,
cursor: `${cursor}`,
size: `${size}`,
Expand Down
5 changes: 4 additions & 1 deletion src/queries/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ComputedRef } from 'vue'
import { PageResult } from './types'
import { useQuery } from '@tanstack/vue-query'
import { metaletApi, metaletApiV3 } from './request'
import { getNetwork } from '@/lib/network'

export const fetchBtcTxHex = async (txId: string): Promise<string> => {
return metaletApi<{ rawTx: string }>(`/tx/raw`)
Expand All @@ -10,9 +11,11 @@ export const fetchBtcTxHex = async (txId: string): Promise<string> => {
}

export const broadcastBTCTx = async (rawTx: string) => {
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return await metaletApiV3<string>(`/tx/broadcast`).post({
chain: 'btc',
net: 'livenet',
net,
rawTx,
})
}
Expand Down
24 changes: 14 additions & 10 deletions src/queries/utxos.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Chain } from '@/lib/account'
import { mvcApi, metaletApiV2, mempoolApi, metaletApiV3 } from './request'
import { mvcApi, mempoolApi, metaletApiV3 } from './request'
import { getNetwork } from '@/lib/network'

export interface UTXO {
txId: string
vout: number
satoshi: number
confirmed: boolean
inscriptions:
| {
id: string
num: number
}[]
| null
| {
id: string
num: number
}[]
| null
}

export type MvcUtxo = {
Expand Down Expand Up @@ -78,7 +79,9 @@ function formatUnisatUTXO(utxo: UnisatUTXO & { confirmed: boolean }): UTXO {
}

export async function getBtcUtxos(address: string): Promise<UTXO[]> {
return metaletApiV3<UTXO[]>('/address/btc-utxo').get({ address, unconfirmed: '1' })
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return metaletApiV3<UTXO[]>('/address/btc-utxo').get({ net, address, unconfirmed: '1' })
}

// export async function getInscriptionUtxos(inscriptions: Inscription[]): Promise<UTXO[]> {
Expand All @@ -93,9 +96,10 @@ export async function getBtcUtxos(address: string): Promise<UTXO[]> {
// return utxos.map((utxo) => formatUnisatUTXO(utxo))
// }

export async function getInscriptionUtxo(inscriptionId: string, confirmed = false): Promise<UTXO> {
const utxo = await metaletApiV2<UnisatUTXO>('/inscription/utxo').get({ inscriptionId })
return formatUnisatUTXO({ ...utxo, confirmed })
export async function getInscriptionUtxo(inscriptionId: string): Promise<UTXO> {
const network = await getNetwork()
const net = network === 'mainnet' ? 'livenet' : network
return await metaletApiV3<UTXO>('/inscription/utxo').get({ net, inscriptionId })
}

export interface MempoolUtxo {
Expand Down

0 comments on commit 5d55173

Please sign in to comment.