Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/services/bioforest-api/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest'

import { BioForestApiClient, BioForestApiError } from './client'

describe('BioForestApiClient.getBalance', () => {
it('uses the provided magic from genesisBlock after trim', async () => {
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
const fetchFn: typeof fetch = async (input, init) => {
requests.push({ url: String(input), init })
return new Response(
JSON.stringify({
success: true,
result: { amount: '100' },
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
},
)
}

const client = new BioForestApiClient({
rpcUrl: 'https://walletapi.bfmeta.info',
chainId: 'bfm',
fetch: fetchFn,
})

await client.getBalance('b-test', 'BFM', ' LLLQL ')
const request = requests[0]
expect(request?.url).toBe('https://walletapi.bfmeta.info/wallet/bfm/address/balance')
expect(request?.init?.method).toBe('POST')
expect(request?.init?.body).toBe(
JSON.stringify({
address: 'b-test',
magic: 'LLLQL',
assetType: 'BFM',
}),
)
})

it('throws when magic is empty', async () => {
let called = false
const fetchFn: typeof fetch = async () => {
called = true
return new Response(JSON.stringify({ success: true, result: { amount: '0' } }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}

const client = new BioForestApiClient({
rpcUrl: 'https://walletapi.bfmeta.info',
chainId: 'bfm',
fetch: fetchFn,
})

await expect(client.getBalance('b-test', 'BFM', ' ')).rejects.toBeInstanceOf(BioForestApiError)
expect(called).toBe(false)
})
})
14 changes: 10 additions & 4 deletions src/services/bioforest-api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface BioForestApiClientConfig {
* const block = await client.getLastBlock()
* console.log(`Height: ${block.height}`)
*
* const balance = await client.getBalance('bXXX...', 'BFM')
* const magic = '...from genesisBlock.magic'
* const balance = await client.getBalance('bXXX...', 'BFM', magic)
* console.log(`Balance: ${balance.amount}`)
* ```
*/
Expand Down Expand Up @@ -172,12 +173,17 @@ export class BioForestApiClient {
* Get account balance for a specific asset
* @param address - Account address
* @param assetType - Asset type (e.g., 'BFM')
* @param magic - Chain magic (default: 'nxOGQ' for mainnet)
* @param magic - Chain magic (必须来自 default-config 的 genesisBlock.magic)
*/
async getBalance(address: string, assetType: string, magic = 'nxOGQ'): Promise<BalanceInfo> {
async getBalance(address: string, assetType: string, magic: string): Promise<BalanceInfo> {
const chainMagic = magic.trim()
if (!chainMagic) {
throw new BioForestApiError('Chain magic is required. Use default-config genesisBlock.magic.')
}

return this.post<BalanceInfo>('/address/balance', {
address,
magic,
magic: chainMagic,
assetType,
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/bioforest-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
* const block = await client.getLastBlock()
*
* // Get balance
* const balance = await client.getBalance('bXXX...', 'BFM')
* const magic = '...from genesisBlock.magic'
* const balance = await client.getBalance('bXXX...', 'BFM', magic)
*
* // Check pay password status
* const hasTwoStepSecret = await client.hasTwoStepSecret('bXXX...')
Expand Down
Loading