Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ opencode.json
.idea/
.expo/

# Drizzle Kit snapshots
**/migrations/meta/*_snapshot.json

# Documentation
docs/ui-automation-test-ids.md
2 changes: 2 additions & 0 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
"@perawallet/wallet-core-blockchain": "workspace:*",
"@perawallet/wallet-core-config": "workspace:*",
"@perawallet/wallet-core-contacts": "workspace:*",
"@perawallet/wallet-core-database": "workspace:*",
"@perawallet/wallet-core-currencies": "workspace:*",
"@perawallet/wallet-core-device": "workspace:*",
"@perawallet/wallet-core-kms": "workspace:*",
"@perawallet/wallet-core-messages": "workspace:*",
"@perawallet/wallet-core-multisig": "workspace:*",
"@perawallet/wallet-core-polling": "workspace:*",
"@perawallet/wallet-core-sync": "workspace:*",
"@perawallet/wallet-core-projects": "workspace:*",
"@perawallet/wallet-core-remote-config": "workspace:*",
"@perawallet/wallet-core-security": "workspace:*",
Expand Down
10 changes: 8 additions & 2 deletions apps/mobile/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import React, { useEffect, useState } from 'react'
import './i18n'
import { Text } from 'react-native'
import { QueryProvider } from './providers/QueryProvider'
import { QueryProvider, queryClient } from './providers/QueryProvider'
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
import { Persister } from '@tanstack/react-query-persist-client'
import {
algorandSafeQuerySerialize,
algorandSafeQueryParse,
} from '@perawallet/wallet-core-blockchain'
import { initializeDatabase } from '@perawallet/wallet-core-database'
import { initializeSyncService } from '@perawallet/wallet-core-sync'
import { createCrashReportingErrorReporter } from '@perawallet/wallet-extension-platform'
import {
getProvider,
Expand Down Expand Up @@ -71,9 +73,13 @@ const AppContent = () => {

useEffect(() => {
if (!bootstrapped) {
provider.initialize().then(({ token }) => {
provider.initialize().then(async ({ token }) => {
setFcmToken(token ?? null)

await initializeDatabase(provider.database)

initializeSyncService({ queryClient })

updateQueryHeaders()

const reactQueryPersistor = createAsyncStoragePersister({
Expand Down
67 changes: 24 additions & 43 deletions apps/mobile/src/components/RootComponent/RootComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ import { useToast } from '@hooks/useToast'
import { useIsDarkMode } from '@hooks/useIsDarkMode'
import { useDevice } from '@perawallet/wallet-core-device'
import { useNetwork } from '@perawallet/wallet-core-blockchain'
import { usePolling } from '@perawallet/wallet-core-polling'
import {
useAllAccounts,
useActiveAccountBalanceInvalidator,
} from '@perawallet/wallet-core-accounts'
import { useAllAccounts } from '@perawallet/wallet-core-accounts'
import { logger } from '@perawallet/wallet-core-shared'
import { useNetworkStatus, useNetworkStatusListener } from '@modules/network'
import { WebViewOverlay } from '@modules/webview'
Expand All @@ -44,6 +40,7 @@ import {
getAppStatePlatform,
getPollingTransitionAction,
} from '@utils/app-state'
import { getSyncService } from '@perawallet/wallet-core-sync'

export type RootComponentProps = {
fcmToken: string | null
Expand Down Expand Up @@ -108,45 +105,36 @@ export const RootComponent = ({ fcmToken }: RootComponentProps) => {
const theme = getTheme(isDarkMode ? 'dark' : 'light')
const { network } = useNetwork()
const { registerDevice } = useDevice()
const { invalidateActiveAccount } = useActiveAccountBalanceInvalidator()
const handlePollingRefresh = useCallback(() => {
invalidateActiveAccount()
}, [invalidateActiveAccount])
const { startPolling, stopPolling } = usePolling({
onRefresh: handlePollingRefresh,
})
const accounts = useAllAccounts()

const appState = useRef(AppState.currentState)
const appStatePlatform = useRef(getAppStatePlatform()).current

const runSyncAction = useCallback((action: 'start' | 'stop') => {
try {
const syncService = getSyncService()
if (action === 'start') {
syncService.start()
} else {
syncService.stop()
}
} catch (error) {
logger.error('Sync action failed in RootComponent', {
source: 'RootComponent',
action,
error,
})
}
}, [])

useEffect(() => {
//TODO we should move the registerDevice stuff into the wallet-core somewhere somehow - maybe in setAccounts or something
const addresses = accounts?.map(account => account.address) ?? []
const runPollingAction = async (action: 'start' | 'stop') => {
try {
if (action === 'start') {
await startPolling()
} else {
await stopPolling()
}
} catch (error) {
// Prevent polling failures from bubbling to top-level handlers.
logger.error(
'Polling action failed in RootComponent listener',
{
source: 'RootComponent',
action,
error,
},
)
}
}

registerDevice(addresses)

if (!addresses.length) {
void runPollingAction('stop')
runSyncAction('stop')
} else if (config.pollingEnabled) {
const subscription = AppState.addEventListener(
'change',
Expand All @@ -159,28 +147,21 @@ export const RootComponent = ({ fcmToken }: RootComponentProps) => {
)

if (action === 'start') {
void runPollingAction('start')
runSyncAction('start')
} else if (action === 'stop') {
void runPollingAction('stop')
runSyncAction('stop')
}

appState.current = nextAppState
},
)

return () => {
void runPollingAction('stop')
runSyncAction('stop')
subscription.remove()
}
}
}, [
appStatePlatform,
network,
accounts,
registerDevice,
startPolling,
stopPolling,
])
}, [appStatePlatform, network, accounts, registerDevice, runSyncAction])

return (
<ThemeProvider theme={theme}>
Expand Down
22 changes: 22 additions & 0 deletions apps/mobile/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,28 @@ vi.mock('@perawallet/wallet-core-swaps', () => ({

vi.mock('@perawallet/wallet-core-polling', () => ({
usePolling: vi.fn(),
usePollingStore: {
getState: vi.fn(() => ({
lastRefreshedRound: null,
setLastRefreshedRound: vi.fn(),
})),
},
sendShouldRefreshRequest: vi.fn(() =>
Promise.resolve({ refresh: false, round: null }),
),
}))

vi.mock('@perawallet/wallet-core-sync', () => ({
initializeSyncService: vi.fn(() => ({
start: vi.fn(),
stop: vi.fn(),
isRunning: vi.fn(() => false),
})),
getSyncService: vi.fn(() => ({
start: vi.fn(),
stop: vi.fn(),
isRunning: vi.fn(() => false),
})),
}))

vi.mock('@perawallet/wallet-core-kms', () => ({
Expand Down
2 changes: 2 additions & 0 deletions extensions/platform-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@react-native-firebase/remote-config": "^23.8.6",
"@notifee/react-native": "^9.1.8",
"@sbaiahmed1/react-native-biometrics": "^0.11.0",
"drizzle-orm": "catalog:",
"expo-sqlite": "~14.0.0",
"expo-application": "^7.0.8",
"expo-device": "^8.0.10",
"expo-localization": "^17.0.8",
Expand Down
106 changes: 106 additions & 0 deletions extensions/platform-react-native/src/__tests__/database.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2022-2025 Pera Wallet, LDA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { RNDatabaseService } from '../services/database'

const mockCloseAsync = vi.fn()
const mockDb = { closeAsync: mockCloseAsync }
const mockOpenDatabaseAsync = vi.fn().mockResolvedValue(mockDb)

vi.mock('expo-sqlite', () => ({
openDatabaseAsync: (...args: unknown[]) => mockOpenDatabaseAsync(...args),
}))

const mockDrizzleInstance = {} as Record<string, unknown>
vi.mock('drizzle-orm/expo-sqlite', () => ({
drizzle: () => mockDrizzleInstance,
}))

describe('RNDatabaseService', () => {
let service: RNDatabaseService

beforeEach(() => {
service = new RNDatabaseService()
vi.clearAllMocks()
})

describe('open', () => {
it('opens a new database on first call', async () => {
const result = await service.open('test.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledWith('test.db')
expect(result.driver).toBe(mockDb)
})

it('returns cached database on subsequent calls', async () => {
await service.open('test.db')
await service.open('test.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledTimes(1)
})

it('opens separate databases for different names', async () => {
const secondMockDb = { closeAsync: vi.fn() }
mockOpenDatabaseAsync
.mockResolvedValueOnce(mockDb)
.mockResolvedValueOnce(secondMockDb)

const first = await service.open('first.db')
const second = await service.open('second.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledTimes(2)
expect(first.driver).toBe(mockDb)
expect(second.driver).toBe(secondMockDb)
})
})

describe('getDatabase', () => {
it('returns a drizzle database instance', async () => {
const result = await service.getDatabase('test.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledWith('test.db')
expect(result).toBe(mockDrizzleInstance)
})

it('reuses the same underlying connection', async () => {
await service.getDatabase('test.db')
await service.getDatabase('test.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledTimes(1)
})
})

describe('close', () => {
it('closes an open database', async () => {
await service.open('test.db')
await service.close('test.db')

expect(mockCloseAsync).toHaveBeenCalledOnce()
})

it('does nothing for an unknown database name', async () => {
await service.close('unknown.db')

expect(mockCloseAsync).not.toHaveBeenCalled()
})

it('removes database from cache so next open creates a new one', async () => {
await service.open('test.db')
await service.close('test.db')

await service.open('test.db')

expect(mockOpenDatabaseAsync).toHaveBeenCalledTimes(2)
})
})
})
2 changes: 2 additions & 0 deletions extensions/platform-react-native/src/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import type { PlatformServices } from '@perawallet/wallet-extension-platform'
import {
RNDatabaseService,
RNKeyValueStorageService,
RNFirebaseService,
RNBiometricsService,
Expand All @@ -37,6 +38,7 @@ export const platformServices: PlatformServices = {
remoteConfig: firebaseService,
secureStorage: new RNSecureStorageService(),
keyValueStorage,
database: new RNDatabaseService(),
deviceInfo: new RNDeviceInfoStorageService(),
}

Expand Down
59 changes: 59 additions & 0 deletions extensions/platform-react-native/src/services/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2022-2025 Pera Wallet, LDA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/

import { openDatabaseAsync, type SQLiteDatabase } from 'expo-sqlite'
import { drizzle } from 'drizzle-orm/expo-sqlite'
import type {
DatabaseService,
DatabaseDriver,
DrizzleDatabase,
} from '@perawallet/wallet-extension-platform'

class ExpoSQLiteDatabaseDriver implements DatabaseDriver {
constructor(readonly driver: SQLiteDatabase) {}
}

export class RNDatabaseService implements DatabaseService {
private databases = new Map<string, SQLiteDatabase>()

async open(name: string): Promise<DatabaseDriver> {
const db = await this.getOrOpen(name)

return new ExpoSQLiteDatabaseDriver(db)
}

async getDatabase(name: string): Promise<DrizzleDatabase> {
const db = await this.getOrOpen(name)

return drizzle(db)
}

async close(name: string): Promise<void> {
const db = this.databases.get(name)

if (db) {
await db.closeAsync()
this.databases.delete(name)
}
}

private async getOrOpen(name: string): Promise<SQLiteDatabase> {
let db = this.databases.get(name)

if (!db) {
db = await openDatabaseAsync(name)
this.databases.set(name, db)
}

return db
}
}
Loading
Loading