diff --git a/src/types/dbTypes.ts b/src/types/dbTypes.ts new file mode 100644 index 0000000..6f5a395 --- /dev/null +++ b/src/types/dbTypes.ts @@ -0,0 +1,27 @@ +import { Device } from '../models/Device' +import { DeviceState, PushEvent, PushEventState } from './pushTypes' + +export interface DeviceRow { + device: Device + + updateEvents: (events: PushEvent[]) => Promise + updateState: (state: DeviceState) => Promise +} + +export interface PushEventRow { + event: PushEvent + + updateState: (state: PushEventState) => Promise +} + +export interface DbConnection { + // Background event watchers: + streamAddressBalanceEvents: () => Promise> + streamPriceEvents: () => Promise> + streamTxConfirmEvents: () => Promise> + + // Queries: + getDeviceById: (deviceId: string) => Promise + getEventsByDeviceId: (deviceId: string) => Promise + getEventsByLoginId: (loginId: Uint8Array) => Promise +} diff --git a/src/types/pushTypes.ts b/src/types/pushTypes.ts index 979fcb8..e03f506 100644 --- a/src/types/pushTypes.ts +++ b/src/types/pushTypes.ts @@ -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 +}