Skip to content

Commit

Permalink
type-madness
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Jul 29, 2022
1 parent 590eaa1 commit e058690
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/types/dbTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Device } from '../models/Device'
import { DeviceState, PushEvent, PushEventState } from './pushTypes'

export interface DeviceRow {
device: Device

updateEvents: (events: PushEvent[]) => Promise<void>
updateState: (state: DeviceState) => Promise<void>
}

export interface PushEventRow {
event: PushEvent

updateState: (state: PushEventState) => Promise<void>
}

export interface DbConnection {
// Background event watchers:
streamAddressBalanceEvents: () => Promise<AsyncIterableIterator<PushEventRow>>
streamPriceEvents: () => Promise<AsyncIterableIterator<PushEventRow>>
streamTxConfirmEvents: () => Promise<AsyncIterableIterator<PushEventRow>>

// Queries:
getDeviceById: (deviceId: string) => Promise<DeviceRow>
getEventsByDeviceId: (deviceId: string) => Promise<PushEventRow[]>
getEventsByLoginId: (loginId: Uint8Array) => Promise<PushEventRow[]>
}
90 changes: 90 additions & 0 deletions src/types/pushTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,93 @@ export interface ApiKey {
admin: boolean
adminsdk?: FirebaseAdminKey
}

/**
* Mutable state on a device object.
*/
export interface DeviceState {
enableLegacyPrices: boolean // For legacy v1 API.
rootLoginIds: Uint8Array[] // asArray(asBase64)
visited: Date
}

/**
* An app installed on a single phone.
*/
export interface Device extends DeviceState {
readonly appId: string
readonly created: Date
readonly deviceId: string
readonly deviceToken: string
}

//
// Events that devices or logins may subscribe to.
//

export interface AddressBalanceTrigger {
readonly type: 'address-balance'
readonly pluginId: string
readonly tokenId?: string
readonly address: string
readonly aboveAmount?: string // Satoshis or Wei or such
readonly belowAmount?: string // Satoshis or Wei or such
}

export interface PriceChangeTrigger {
readonly type: 'price-change'
readonly pluginId: string
readonly tokenId?: string
readonly dailyChange?: number // Percentage
readonly hourlyChange?: number // Percentage
}

export interface PriceLevelTrigger {
readonly type: 'price-level'
readonly currencyPair: string // From our rates server
readonly aboveRate?: number
readonly belowRate?: number
}

export interface TxConfirmTrigger {
readonly type: 'tx-confirm'
readonly pluginId: string
readonly confirmations: number
readonly txid: string
}

export type PushTrigger =
| AddressBalanceTrigger
| PriceChangeTrigger
| PriceLevelTrigger
| TxConfirmTrigger

/**
* Mutable flags that we can toggle on a push event.
*/
export interface PushEventState {
active: boolean // Watch the trigger when true
triggered?: Date
broadcasted?: Date
pushed?: Date
}

/**
* An action to perform once a trigger takes place.
*/
export interface PushEvent extends PushEventState {
readonly eventId?: string
readonly deviceId?: string
readonly loginId?: Uint8Array

readonly broadcast?: Array<{
pluginId: string
rawTx: Uint8Array // asBase64
}>
readonly push?: {
title?: string
body?: string
data: { [key: string]: string } // JSON to push to device
}
readonly trigger: PushTrigger
}

0 comments on commit e058690

Please sign in to comment.