Skip to content

Commit

Permalink
Add rewards notification builder
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Oct 30, 2024
1 parent 4673f72 commit 9b146e3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {PermissionsAndroid} from 'react-native'
import {notificationManager} from './notification-manager'
import {parseNotificationId} from './notifications'
import {usePrimaryTokenPriceChangedNotification} from './primary-token-price-changed-notification'
import {useTransactionReceivedNotifications} from './transaction-received-notification'
import {useRewardsUpdatedNotifications} from './rewards-updated-notification'
import {useTransactionReceivedNotifications} from './transaction-received-notification'

let initialized = false

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {mountAsyncStorage, useAsyncStorage} from '@yoroi/common'
import {App, Notifications as NotificationTypes} from '@yoroi/types'
import * as BackgroundFetch from 'expo-background-fetch'
import * as TaskManager from 'expo-task-manager'
import * as React from 'react'
import {mountAsyncStorage, useAsyncStorage} from '@yoroi/common'
import {Subject} from 'rxjs'

import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
import {walletManager} from '../../../WalletManager/wallet-manager'
import {notificationManager} from './notification-manager'
import {Notifications as NotificationTypes} from '@yoroi/types'
import {generateNotificationId} from './notifications'
import {Subject} from 'rxjs'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
import {buildProcessedNotificationsStorage} from './storage'

const backgroundTaskId = 'yoroi-rewards-updated-notifications-background-fetch'
const storageKey = 'rewards-updated-notification-history'
const backgroundSyncInMinutes = 60 * 10

// Check is needed for hot reloading, as task can not be defined twice
Expand All @@ -34,6 +37,35 @@ const registerBackgroundFetchAsync = () => {
})
}

const buildNotifications = async (appStorage: App.Storage) => {
const walletIds = [...walletManager.walletMetas.keys()]
const notifications: NotificationTypes.RewardsUpdatedEvent[] = []

for (const walletId of walletIds) {
const wallet = walletManager.getWalletById(walletId)
if (!wallet) continue
const storage = buildProcessedNotificationsStorage(appStorage.join(`wallet/${walletId}/${storageKey}/`))
const stakingInfo = await wallet.getStakingInfo()
if (stakingInfo.status !== 'staked') continue
const {rewards} = stakingInfo

if (await storage.isEmpty()) {
await storage.setValues([rewards])
}

const [latestReward] = await storage.getValues()

if (latestReward === rewards) {
continue
}

await storage.setValues([rewards])
notifications.push(createRewardsUpdatedNotification())
}

return notifications
}

const unregisterBackgroundFetchAsync = () => {
return BackgroundFetch.unregisterTaskAsync(backgroundTaskId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const buildProcessedNotificationsStorage = (storage: App.Storage) => {
await storage.setItem('processed', newProcessed)
}

const setValues = async (values: string[]) => {
await storage.setItem('processed', values)
}

const includes = async (value: string) => {
const processed = await getValues()
return processed.includes(value)
Expand All @@ -20,10 +24,17 @@ export const buildProcessedNotificationsStorage = (storage: App.Storage) => {
await storage.setItem('processed', [])
}

const isEmpty = async () => {
const processed = await getValues()
return processed.length === 0
}

return {
getValues,
addValues,
includes,
clear,
setValues,
isEmpty,
}
}
1 change: 1 addition & 0 deletions packages/types/src/notifications/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type NotificationGroup = 'transaction-history' | 'portfolio'
export type NotificationEvent =
| NotificationTransactionReceivedEvent
| NotificationPrimaryTokenPriceChangedEvent
| NotificationRewardsUpdatedEvent

type NotificationEventId = number

Expand Down

0 comments on commit 9b146e3

Please sign in to comment.