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
37 changes: 37 additions & 0 deletions miniapps/biobridge/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,41 @@ describe('Forge App', () => {
)
})
})

it('should use bio_selectAccount for external chain account', async () => {
mockBio.request.mockImplementation(({ method, params }: { method: string; params?: Array<{ chain?: string }> }) => {
if (method === 'bio_closeSplashScreen') return Promise.resolve(null)
if (method === 'bio_selectAccount') {
const chain = params?.[0]?.chain
if (chain === 'ethereum') {
return Promise.resolve({ address: '0xexternal-bio', chain: 'ethereum' })
}
return Promise.resolve({ address: 'bfmeta123', chain: 'bfmeta' })
}
return Promise.resolve(null)
})

render(<App />)

await waitFor(() => {
expect(screen.getByTestId('connect-button')).toBeInTheDocument()
})

fireEvent.click(screen.getByTestId('connect-button'))

await waitFor(() => {
expect(screen.getByText(/支付/)).toBeInTheDocument()
expect(screen.getByText('0xexternal-bio')).toBeInTheDocument()
})

expect(mockBio.request).toHaveBeenCalledWith(
expect.objectContaining({
method: 'bio_selectAccount',
params: [{ chain: 'ethereum' }],
})
)
expect(mockEthereum.request).not.toHaveBeenCalledWith(
expect.objectContaining({ method: 'eth_requestAccounts' })
)
})
})
52 changes: 4 additions & 48 deletions miniapps/biobridge/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { useState, useCallback, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import type { BioAccount } from '@biochain/bio-sdk';
import { normalizeChainId } from '@biochain/bio-sdk';
import { getChainType, getEvmChainIdFromApi } from '@/lib/chain';
import { parseAmount } from '@/lib/fee';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
Expand Down Expand Up @@ -170,53 +169,10 @@ export default function App() {
setError(null);
try {
const externalChain = activeOption.externalChain;
const chainType = getChainType(externalChain);

let extAcc: BioAccount;

if (chainType === 'evm') {
if (!window.ethereum) {
throw new Error('Ethereum provider not available');
}
const evmChainId = getEvmChainIdFromApi(externalChain);
if (evmChainId) {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: evmChainId }],
});
}
const accounts = await window.ethereum.request<string[]>({
method: 'eth_requestAccounts',
});
if (!accounts || accounts.length === 0) {
throw new Error('No accounts returned');
}
extAcc = {
address: accounts[0],
chain: normalizeChainId(externalChain),
publicKey: '',
};
} else if (chainType === 'tron') {
if (!window.tronLink) {
throw new Error('TronLink provider not available');
}
const result = await window.tronLink.request<{ code: number; message: string; data: { base58: string } }>({
method: 'tron_requestAccounts',
});
if (!result || result.code !== 200) {
throw new Error('TRON connection failed');
}
extAcc = {
address: result.data.base58,
chain: 'tron',
publicKey: '',
};
} else {
extAcc = await window.bio.request<BioAccount>({
method: 'bio_selectAccount',
params: [{ chain: normalizeChainId(externalChain) }],
});
}
const extAcc = await window.bio.request<BioAccount>({
method: 'bio_selectAccount',
params: [{ chain: normalizeChainId(externalChain) }],
});
setExternalAccount(extAcc);

const intAcc = await window.bio.request<BioAccount>({
Expand Down