Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable bitcoin e2e wallet tests #3897

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 35 additions & 11 deletions apps/laboratory/src/components/AppKitHooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { Box, Button, Heading } from '@chakra-ui/react'

import { type AppKitNetwork, mainnet, polygon, solana, solanaTestnet } from '@reown/appkit/networks'
import { useAppKit, useAppKitAccount, useAppKitNetwork, useDisconnect } from '@reown/appkit/react'
import {
bitcoin,
bitcoinTestnet,
mainnet,
polygon,
solana,
solanaTestnet
} from '@reown/appkit/networks'
import {
type CaipNetwork,
useAppKit,
useAppKitAccount,
useAppKitNetwork,
useDisconnect
} from '@reown/appkit/react'

function getNetworkToSwitch(activeNetwork: CaipNetwork | undefined) {
if (!activeNetwork) {
return mainnet
}

switch (activeNetwork.chainNamespace) {
case 'bip122':
return activeNetwork.id === bitcoin.id ? bitcoinTestnet : bitcoin
case 'solana':
return activeNetwork.id === solana.id ? solanaTestnet : solana
default:
return activeNetwork.id === polygon.id ? mainnet : polygon
}
}

export function AppKitHooks() {
const { open } = useAppKit()
Expand All @@ -10,15 +38,11 @@ export function AppKitHooks() {
const { disconnect } = useDisconnect()

function handleSwitchNetwork() {
const isEIPNamespace = caipNetwork?.chainNamespace === 'eip155'
// eslint-disable-next-line no-nested-ternary
const networkToSwitch: AppKitNetwork = isEIPNamespace
? caipNetwork?.id === polygon.id
? mainnet
: polygon
: caipNetwork?.id === solana.id
? solanaTestnet
: solana
const networkToSwitch = getNetworkToSwitch(caipNetwork)

if (!networkToSwitch) {
return
}

switchNetwork(networkToSwitch)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { useState } from 'react'

import { Box, Button, Input, InputGroup, InputLeftAddon, useToast } from '@chakra-ui/react'
import { Box, Button, Input, InputGroup, InputLeftAddon } from '@chakra-ui/react'

import type { BitcoinConnector } from '@reown/appkit-adapter-bitcoin'
import { useAppKitAccount, useAppKitProvider } from '@reown/appkit/react'

import { useChakraToast } from '@/src/components/Toast'

export function BitcoinSendTransferTest() {
const { walletProvider } = useAppKitProvider<BitcoinConnector>('bip122')
const { address } = useAppKitAccount({ namespace: 'bip122' })

const toast = useToast()
const toast = useChakraToast()
const [loading, setLoading] = useState(false)
const [recipient, setRecipient] = useState<string>(address || '')
const [amount, setAmount] = useState<string>('1500')
Expand All @@ -18,8 +20,8 @@ export function BitcoinSendTransferTest() {
if (!walletProvider) {
toast({
title: 'No wallet provider',
status: 'error',
isClosable: true
description: 'Please connect your wallet',
type: 'error'
})
}

Expand All @@ -32,11 +34,11 @@ export function BitcoinSendTransferTest() {

toast({
title: `Transfer sent: ${signature}`,
status: 'success',
isClosable: true
description: 'Transfer sent successfully',
type: 'success'
})
} catch (error) {
toast({ title: 'Error', description: (error as Error).message, status: 'error' })
toast({ title: 'Error', description: (error as Error).message, type: 'error' })
} finally {
setLoading(false)
}
Expand Down
23 changes: 17 additions & 6 deletions apps/laboratory/src/components/Bitcoin/BitcoinSignMessageTest.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { useState } from 'react'

import { Box, Button, Input, InputGroup, InputLeftAddon, useToast } from '@chakra-ui/react'
import { Box, Button, Input, InputGroup, InputLeftAddon } from '@chakra-ui/react'

import type { BitcoinConnector } from '@reown/appkit-adapter-bitcoin'
import { useAppKitAccount, useAppKitProvider } from '@reown/appkit/react'

import { useChakraToast } from '@/src/components/Toast'
import { ConstantsUtil } from '@/src/utils/ConstantsUtil'

export function BitcoinSignMessageTest() {
const toast = useChakraToast()
const { walletProvider } = useAppKitProvider<BitcoinConnector>('bip122')
const { address } = useAppKitAccount({ namespace: 'bip122' })

const toast = useToast()
const [loading, setLoading] = useState(false)
const [message, setMessage] = useState<string>('Hello, World!')

async function onSignMessage() {
if (!walletProvider || !address) {
toast({
title: 'No connection detected',
status: 'error',
isClosable: true
description: 'Please connect your wallet',
type: 'error'
})

return
Expand All @@ -31,9 +34,17 @@ export function BitcoinSignMessageTest() {
address,
message
})
toast({ title: 'Signature', description: signature, status: 'success' })
toast({
title: ConstantsUtil.SigningSucceededToastTitle,
description: signature,
type: 'success'
})
} catch (error) {
toast({ title: 'Error', description: (error as Error).message, status: 'error' })
toast({
title: ConstantsUtil.SigningFailedToastTitle,
description: 'Failed to sign message',
type: 'error'
})
} finally {
setLoading(false)
}
Expand Down
26 changes: 9 additions & 17 deletions apps/laboratory/src/components/Bitcoin/BitcoinSignPSBTTest.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import { useState } from 'react'

import {
Box,
Button,
Checkbox,
Flex,
Input,
InputGroup,
InputLeftAddon,
useToast
} from '@chakra-ui/react'
import { Box, Button, Checkbox, Flex, Input, InputGroup, InputLeftAddon } from '@chakra-ui/react'

import type { BitcoinConnector } from '@reown/appkit-adapter-bitcoin'
import { useAppKitAccount, useAppKitNetwork, useAppKitProvider } from '@reown/appkit/react'

import { useChakraToast } from '@/src/components/Toast'
import { BitcoinUtil } from '@/src/utils/BitcoinUtil'

export function BitcoinSignPSBTTest() {
const { walletProvider } = useAppKitProvider<BitcoinConnector>('bip122')
const { address } = useAppKitAccount({ namespace: 'bip122' })
const { caipNetwork } = useAppKitNetwork()

const toast = useToast()
const toast = useChakraToast()
const [loading, setLoading] = useState(false)
const [recipient, setRecipient] = useState<string>(address || '')
const [amount, setAmount] = useState<string>('1500')
Expand All @@ -31,8 +23,8 @@ export function BitcoinSignPSBTTest() {
if (!walletProvider || !address || !caipNetwork) {
toast({
title: 'No connection detected',
status: 'error',
isClosable: true
description: 'Please connect your wallet',
type: 'error'
})

return
Expand All @@ -41,8 +33,8 @@ export function BitcoinSignPSBTTest() {
if (caipNetwork.chainNamespace !== 'bip122') {
toast({
title: 'The selected chain is not bip122',
status: 'error',
isClosable: true
description: 'Please switch to a bip122 network',
type: 'error'
})

return
Expand All @@ -65,9 +57,9 @@ export function BitcoinSignPSBTTest() {
params.broadcast = broadcast

const signature = await walletProvider.signPSBT(params)
toast({ title: 'PSBT Signature', description: signature.psbt, status: 'success' })
toast({ title: 'PSBT Signature', description: signature.psbt, type: 'success' })
} catch (error) {
toast({ title: 'Error', description: (error as Error).message, status: 'error' })
toast({ title: 'Error', description: (error as Error).message, type: 'error' })
} finally {
setLoading(false)
}
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/src/components/Bitcoin/BitcoinTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function BitcoinTests() {
return (
<Card data-testid="bip122-test-interactions" marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
<Heading size="md">Bitcoin Test Interactions</Heading>
</CardHeader>

<CardBody>
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/src/components/UPA/UpaTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function UpaTests() {
return (
<Card data-testid="upa-test-interactions" marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
<Heading size="md">UP Test Interactions</Heading>
</CardHeader>
<CardBody>
<Stack divider={<StackDivider />} spacing="4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function WagmiPermissionsAsyncTest() {
return (
<Card data-testid="eip155-test-interactions" marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
<Heading size="md">Wagmi Test Interactions</Heading>
</CardHeader>
<CardBody>
<Stack divider={<StackDivider />} spacing="4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function WagmiPermissionsSyncTest() {
return (
<Card data-testid="eip155-test-interactions" marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
<Heading size="md">Wagmi Test Interactions</Heading>
</CardHeader>
<CardBody>
<Stack divider={<StackDivider />} spacing="4">
Expand Down
15 changes: 13 additions & 2 deletions apps/laboratory/tests/shared/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { DESKTOP_DEVICES, MOBILE_DEVICES } from '../constants/devices'

const LIBRARIES = ['ethers', 'ethers5', 'wagmi', 'solana'] as const
const LIBRARIES = ['ethers', 'ethers5', 'wagmi', 'solana', 'bitcoin'] as const
const MULTICHAIN_LIBRARIES = [
'multichain-basic',
'multichain-ethers-solana',
Expand Down Expand Up @@ -61,6 +61,10 @@
'email-after-farcaster.spec.ts'
]

const BITCOIN_IGNORE_TESTS = SINGLE_ADAPTER_EVM_TESTS.filter(
test => !test.includes('wallet.spec.ts')
)

const SINGLE_ADAPTER_EVM_MOBILE_TESTS = ['mobile-wallet-features.spec.ts']

const SINGLE_ADAPTER_SOLANA_TESTS = [
Expand All @@ -85,7 +89,7 @@
// Desktop
const SINGLE_ADAPTER_EVM_TESTS_REGEX = createRegex(SINGLE_ADAPTER_EVM_TESTS)
const SINGLE_ADAPTER_SOLANA_TESTS_REGEX = createRegex(SINGLE_ADAPTER_SOLANA_TESTS)

const BITCOIN_IGNORE_TESTS_REGEX = createRegex(BITCOIN_IGNORE_TESTS)
// Mobile
const SINGLE_ADAPTER_EVM_MOBILE_REGEX = createRegex(SINGLE_ADAPTER_EVM_MOBILE_TESTS, false)
const SINGLE_ADAPTER_SOLANA_MOBILE_TESTS_REGEX = createRegex(
Expand All @@ -112,6 +116,12 @@
'Desktop Firefox/wagmi': {
testMatch: SINGLE_ADAPTER_EVM_TESTS_REGEX
},
'Desktop Chrome/bitcoin': {
testIgnore: BITCOIN_IGNORE_TESTS_REGEX
},
'Desktop Firefox/bitcoin': {
testIgnore: BITCOIN_IGNORE_TESTS_REGEX
},
'Desktop Chrome/solana': {
testMatch: SINGLE_ADAPTER_SOLANA_TESTS_REGEX,
testIgnore: /siwe-email\.spec\.ts|siwe-extension\.spec\.ts|multichain-.*\.spec\.ts/u
Expand Down Expand Up @@ -203,6 +213,7 @@
const multichainProjects = MULTICHAIN_PERMUTATIONS.map(createProject)

const projects = [...libraryDesktopProjects, ...libraryMobileProjects, ...multichainProjects]
console.log('projects', projects)

Check failure on line 216 in apps/laboratory/tests/shared/utils/project.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement

return projects
}
10 changes: 7 additions & 3 deletions apps/laboratory/tests/shared/validators/ModalValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ModalValidator {
})
}

async expectBalanceFetched(currency: 'SOL' | 'ETH') {
async expectBalanceFetched(currency: 'BTC' | 'SOL' | 'ETH') {
const accountButton = this.page.locator('appkit-account-button')
await expect(accountButton, `Account button should show balance as ${currency}`).toContainText(
`0.000 ${currency}`
Expand Down Expand Up @@ -307,9 +307,13 @@ export class ModalValidator {
await expect(switchNetworkButton).toBeVisible()
}

async expectOnrampButton() {
async expectOnrampButton(visible: boolean) {
const onrampButton = this.page.getByTestId('w3m-account-default-onramp-button')
await expect(onrampButton).toBeVisible()
if (visible) {
await expect(onrampButton).toBeVisible()
} else {
await expect(onrampButton).not.toBeVisible()
}
}

async expectWalletGuide(_library: string, guide: 'get-started' | 'explore') {
Expand Down
12 changes: 7 additions & 5 deletions apps/laboratory/tests/shared/validators/WalletValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ export class WalletValidator {
})
}

async expectReceivedSign({ chainName = 'Ethereum' }) {
async expectReceivedSign({ chainName = 'Ethereum', expectNetworkName = true }) {
await expect(
this.page.getByTestId('session-approve-button'),
'Session approve button should be visible'
).toBeVisible({
timeout: MAX_WAIT
})
await expect(
this.page.getByTestId('request-details-chain'),
'Request details should contain chain name'
).toContainText(chainName)
if (expectNetworkName) {
await expect(
this.page.getByTestId('request-details-chain'),
'Request details should contain chain name'
).toContainText(chainName)
}
}
}
Loading
Loading