-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
203 additions
and
203 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
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 | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
}) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.