raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
131 lines (130 loc) • 6.67 kB
TypeScript
/// <reference types="pouchdb-core" />
/// <reference types="lodash" />
import type { Observable } from 'rxjs';
import type { RaidenState } from '../state';
import type { RaidenTransfer } from '../transfers/state';
import type { Migrations, RaidenDatabase, RaidenDatabaseConstructor, RaidenDatabaseMeta, RaidenDatabaseOptions } from './types';
/**
* Create observable of PouchDB.changes stream, with proper teardown
*
* @param db - Database to monitor for changes
* @param options - db.changes options
* @returns Observable of changes responses
*/
export declare function changes$<T = {}>(db: RaidenDatabase, options?: PouchDB.Core.ChangesOptions): Observable<PouchDB.Core.ChangesResponseChange<T>>;
/**
* [[dbStateEpic]] stores each key of RaidenState as independent value on the database, prefixed
* with 'state.', to make it cheaper to save changes touching only a subset of the state.
* 'channels' (being a special hotpath of complex objects) are split as one entry per channel.
* This function reads this format, fetching the multiple rows from database and composing an
* object which should be decodable by [[RaidenState]] codec.
*
* @param db - Database to query state from
* @returns mapping object potentially decodable to RaidenState
*/
export declare function getRaidenState(db: RaidenDatabase): Promise<any | undefined>;
/**
* Stores each key of RaidenState as independent value on the database, prefixed * with 'state.',
* to make it cheaper to save changes touching only a subset of the state.
* 'channels' (being a special hotpath of complex objects) are split as one entry per channel.
* Used to store initial state (on empty db)
*
* @param db - Database to store state into
* @param state - State to persist
*/
export declare function putRaidenState(db: RaidenDatabase, state: RaidenState): Promise<void>;
/**
* @param migrations - Migrations mapping
* @returns Sorted versions array according with migrations
*/
export declare function sortMigrations(migrations: Migrations): number[];
/**
* @param migrations - Migrations mapping
* @returns Latest/current db version from migrations
*/
export declare function latestVersion(migrations?: Migrations): number;
/**
* @param db - Raiden database
* @returns Version of db passed as param
*/
export declare function databaseVersion(db: RaidenDatabase): number;
/**
* @param opts - Default database options
* @returns Constructor function for RaidenStorage
*/
export declare function getDatabaseConstructorFromOptions(opts?: RaidenDatabaseOptions): Promise<RaidenDatabaseConstructor>;
/**
* Detects current version on storage, and migrate it to latest version if needed, resolving to the
* initialized database instance. May reject if migration fails.
*
* @param this - RaidenStorage constructor, as static factory param
* @param name - Database name (to be suffixed with versions)
* @param migrations - Map of migrations, indexed by target version number, starting with 1;
* Each migration is an async function which receives each entry/row of the previous db and
* the old db instance (in case one needs to fetch some data from some other row), and
* resolves to an array of new documents (without `_rev`) to be put in the upgraded database.
* To remove an entry, simply return empty array, or just return [doc] to migrate as is.
* @param cleanOld - Whether to clean/remove successfully migrated databases or leave it
* @returns Promise to instance of currentVersion of database
*/
export declare function migrateDatabase(this: RaidenDatabaseConstructor, name: string, migrations?: Migrations, cleanOld?: boolean): Promise<RaidenDatabase>;
/**
* @param db - Raiden database to fetch meta from
* @returns Promise which resolves to meta information from database
*/
export declare function databaseMeta(db: RaidenDatabase): Promise<RaidenDatabaseMeta>;
/**
* Replace current database with data from a given state dump; the dump must not be older than
* the state in storage.
*
* @param this - RaidenStorage constructor, as static factory param
* @param data - (possibly async) iterable which yields state entries; must start with '_meta'
* @param name - Database name (to be suffixed with versions)
* @param migrations - Map of migrations, indexed by target version number, starting with 1;
* Each migration is an async function which receives each entry/row of the previous db and
* the old db instance (in case one needs to fetch some data from some other row), and
* resolves to an array of new documents (without `_rev`) to be put in the upgraded database.
* To remove an entry, simply return empty array, or just return [doc] to migrate as is.
* @param cleanOld - Weather to clean/remove successfully migrated databases
* @returns Promise to instance of currentVersion of database
*/
export declare function replaceDatabase(this: RaidenDatabaseConstructor, data: Iterable<any> | AsyncIterable<any>, name: string, migrations?: Migrations, cleanOld?: boolean): ReturnType<typeof migrateDatabase>;
/**
* Creates an async generator which yields database entries documents.
* Can be dumped to a JSON array or streamed. Will throw if database changes while dumping, to
* invalidate previous dump. Caller must ensure the database can't change while dumping or handle
* the exception to restart.
*
* @param db - Database to dump
* @param opts - Options
* @param opts.batch - Size of batches to fetch and yield
* @yields Each document in database
*/
export declare function dumpDatabase(db: RaidenDatabase, { batch }?: {
batch?: number;
}): AsyncGenerator<RaidenDatabaseMeta | import("lodash").Omit<PouchDB.Core.ExistingDocument<PouchDB.Core.AllDocsMeta>, "_rev">, void, undefined>;
/**
* Efficently get transfers from database when paging with offset and limit
*
* @param db - Database instance
* @param filter - Filter options
* @param filter.pending - true: only pending; false: only completed; undefined: all
* @param filter.token - filter by token address
* @param filter.partner - filter by partner address
* @param filter.end - filter by initiator or target address
* @param opts - Changes options
* @param opts.offset - Offset to skip entries
* @param opts.limit - Limit number of entries
* @param opts.desc - Set to true to get new transfers first
* @returns Promise to array of results
*/
export declare function getTransfers(db: RaidenDatabase, filter?: {
pending?: boolean;
token?: string;
partner?: string;
end?: string;
}, { offset: skip, desc, ...opts }?: {
offset?: number;
limit?: number;
desc?: boolean;
}): Promise<RaidenTransfer[]>;