Skip to content

Commit

Permalink
fix: formating issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka committed Dec 10, 2024
1 parent 4177d31 commit 90ff117
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 203 deletions.
84 changes: 42 additions & 42 deletions packages/adapters/bitcoin/src/utils/BitcoinApi.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import type { CaipNetwork } from '@reown/appkit-common'
import { bitcoinTestnet } from '@reown/appkit/networks'

export const BitcoinApi: BitcoinApi.Interface = {
getUTXOs: async ({ network, address }: BitcoinApi.GetUTXOsParams): Promise<BitcoinApi.UTXO[]> => {
const isTestnet = network.caipNetworkId === bitcoinTestnet.caipNetworkId
// Make chain dynamic

const response = await fetch(
`https://mempool.space${isTestnet ? '/testnet' : ''}/api/address/${address}/utxo`
)

if (!response.ok) {
throw new Error(`Failed to fetch UTXOs: ${await response.text()}`)
}

return (await response.json()) as BitcoinApi.UTXO[]
}
}

export namespace BitcoinApi {
export type Interface = {
getUTXOs: (params: GetUTXOsParams) => Promise<UTXO[]>
}

export type GetUTXOsParams = {
network: CaipNetwork
address: string
}

export type UTXO = {
txid: string
vout: number
value: number
status: {
confirmed: boolean
block_height: number
block_hash: string
block_time: number
}
}
}
import type { CaipNetwork } from '@reown/appkit-common'
import { bitcoinTestnet } from '@reown/appkit/networks'

export const BitcoinApi: BitcoinApi.Interface = {
getUTXOs: async ({ network, address }: BitcoinApi.GetUTXOsParams): Promise<BitcoinApi.UTXO[]> => {
const isTestnet = network.caipNetworkId === bitcoinTestnet.caipNetworkId
// Make chain dynamic

const response = await fetch(
`https://mempool.space${isTestnet ? '/testnet' : ''}/api/address/${address}/utxo`
)

if (!response.ok) {
throw new Error(`Failed to fetch UTXOs: ${await response.text()}`)
}

return (await response.json()) as BitcoinApi.UTXO[]
}
}

export namespace BitcoinApi {
export type Interface = {
getUTXOs: (params: GetUTXOsParams) => Promise<UTXO[]>
}

export type GetUTXOsParams = {
network: CaipNetwork
address: string
}

export type UTXO = {
txid: string
vout: number
value: number
status: {
confirmed: boolean
block_height: number
block_hash: string
block_time: number
}
}
}
166 changes: 83 additions & 83 deletions packages/adapters/bitcoin/tests/BitcoinAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
import { beforeEach, describe, it, vi, type Mock, expect } from 'vitest'
import { BitcoinAdapter } from '../src'
import type { BitcoinApi } from '../src/utils/BitcoinApi'
import { bitcoin, mainnet } from '@reown/appkit/networks'
import { mockUTXO } from './mocks/mockUTXO'

function mockBitcoinApi(): { [K in keyof BitcoinApi.Interface]: Mock<BitcoinApi.Interface[K]> } {
return {
getUTXOs: vi.fn(async () => [])
}
}

describe('BitcoinAdapter', () => {
let adapter: BitcoinAdapter
let api: ReturnType<typeof mockBitcoinApi>

beforeEach(() => {
api = mockBitcoinApi()
adapter = new BitcoinAdapter({ api })
})

describe('getBalance', () => {
it('should return the balance', async () => {
api.getUTXOs.mockResolvedValueOnce([
mockUTXO({ value: 10000 }),
mockUTXO({ value: 20000 }),
mockUTXO({ value: 30000 }),
mockUTXO({ value: 10000000000 })
])

const balance = await adapter.getBalance({
address: 'mock_address',
chainId: bitcoin.id,
caipNetwork: bitcoin
})

expect(balance).toEqual({
balance: '100.0006',
symbol: 'BTC'
})
})

it('should return empty balance if no UTXOs', async () => {
api.getUTXOs.mockResolvedValueOnce([])

const balance = await adapter.getBalance({
address: 'mock_address',
chainId: bitcoin.id,
caipNetwork: bitcoin
})

expect(balance).toEqual({
balance: '0',
symbol: 'BTC'
})
})

it('should return empty balance if chain is not bip122', async () => {
const balance = await adapter.getBalance({
address: 'mock_address',
chainId: mainnet.id,
caipNetwork: mainnet as any
})

expect(balance).toEqual({
balance: '0',
symbol: bitcoin.nativeCurrency.symbol
})
})

it('should return empty balance if chain is not provided', async () => {
const balance = await adapter.getBalance({
address: 'mock_address',
chainId: 'mock_chain_id'
})

expect(balance).toEqual({
balance: '0',
symbol: bitcoin.nativeCurrency.symbol
})
})
})
})
import { beforeEach, describe, it, vi, type Mock, expect } from 'vitest'
import { BitcoinAdapter } from '../src'
import type { BitcoinApi } from '../src/utils/BitcoinApi'
import { bitcoin, mainnet } from '@reown/appkit/networks'
import { mockUTXO } from './mocks/mockUTXO'

function mockBitcoinApi(): { [K in keyof BitcoinApi.Interface]: Mock<BitcoinApi.Interface[K]> } {
return {
getUTXOs: vi.fn(async () => [])
}
}

describe('BitcoinAdapter', () => {
let adapter: BitcoinAdapter
let api: ReturnType<typeof mockBitcoinApi>

beforeEach(() => {
api = mockBitcoinApi()
adapter = new BitcoinAdapter({ api })
})

describe('getBalance', () => {
it('should return the balance', async () => {
api.getUTXOs.mockResolvedValueOnce([
mockUTXO({ value: 10000 }),
mockUTXO({ value: 20000 }),
mockUTXO({ value: 30000 }),
mockUTXO({ value: 10000000000 })
])

const balance = await adapter.getBalance({
address: 'mock_address',
chainId: bitcoin.id,
caipNetwork: bitcoin
})

expect(balance).toEqual({
balance: '100.0006',
symbol: 'BTC'
})
})

it('should return empty balance if no UTXOs', async () => {
api.getUTXOs.mockResolvedValueOnce([])

const balance = await adapter.getBalance({
address: 'mock_address',
chainId: bitcoin.id,
caipNetwork: bitcoin
})

expect(balance).toEqual({
balance: '0',
symbol: 'BTC'
})
})

it('should return empty balance if chain is not bip122', async () => {
const balance = await adapter.getBalance({
address: 'mock_address',
chainId: mainnet.id,
caipNetwork: mainnet as any
})

expect(balance).toEqual({
balance: '0',
symbol: bitcoin.nativeCurrency.symbol
})
})

it('should return empty balance if chain is not provided', async () => {
const balance = await adapter.getBalance({
address: 'mock_address',
chainId: 'mock_chain_id'
})

expect(balance).toEqual({
balance: '0',
symbol: bitcoin.nativeCurrency.symbol
})
})
})
})
32 changes: 16 additions & 16 deletions packages/adapters/bitcoin/tests/mocks/mockUTXO.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { BitcoinApi } from '../../src/utils/BitcoinApi.js'

export function mockUTXO(replaces: Partial<BitcoinApi.UTXO> = {}): BitcoinApi.UTXO {
return {
txid: 'mock_txid',
vout: 0,
value: 1000,
status: {
confirmed: true,
block_height: 1,
block_hash: 'mock_block_hash',
block_time: 1
},
...replaces
}
}
import type { BitcoinApi } from '../../src/utils/BitcoinApi.js'

export function mockUTXO(replaces: Partial<BitcoinApi.UTXO> = {}): BitcoinApi.UTXO {
return {
txid: 'mock_txid',
vout: 0,
value: 1000,
status: {
confirmed: true,
block_height: 1,
block_hash: 'mock_block_hash',
block_time: 1
},
...replaces
}
}
Loading

0 comments on commit 90ff117

Please sign in to comment.