import {utils} from 'ethers'; import {BN} from '../bignumber'; import {Uint256} from '../types'; export function unreachable(x: never): never { return x; } export const exists = (t: T | undefined): t is T => !!t; const throwError = (fn: (t1: any) => boolean, t: any) => { throw new Error(`not valid, ${fn.name} failed on ${t}`); }; type TypeGuard = (t1: T | S) => t1 is T; export function checkThat(t: T | S, isTypeT: TypeGuard): T { if (!isTypeT(t)) { throwError(isTypeT, t); // Typescrypt doesn't know that throwError throws an error. throw 'Unreachable'; } return t; } export function createDestination(address: string): string { return utils.hexZeroPad(address, 32); } export function formatAmount(amount: Uint256): Uint256 { return utils.hexZeroPad(BN.from(amount), 32) as Uint256; } export function arrayToRecord( array: Array, idProperty: K ): Record { return array.reduce((obj, item) => { obj[item[idProperty]] = item; return obj; }, {} as any); } export function recordToArray(record: Record): Array { return Object.keys(record) .map(k => record[k]) .filter(e => e !== undefined) as Array; }