on 2fe1d2f help sign in
bridge/src/lib/need.js
55 lines 1.2 kB history raw
1// need.js: +need but worse
2//
4import { BRIDGE_ERROR } from './error';
6const needBuilder = fn => obj => {
7 if (!obj) {
8 fn();
9 }
11 return obj.matchWith({
12 Nothing: fn,
13 Just: p => p.value,
14 });
15};
17export const web3 = needBuilder(() => {
18 throw new Error(BRIDGE_ERROR.MISSING_WEB3.message);
19});
21export const contracts = needBuilder(() => {
22 throw new Error(BRIDGE_ERROR.MISSING_CONTRACTS);
23});
25export const wallet = needBuilder(() => {
26 throw new Error(BRIDGE_ERROR.MISSING_WALLET);
27});
29export const addressFromWallet = obj => wallet(obj).address;
31export const point = needBuilder(() => {
32 throw new Error(BRIDGE_ERROR.MISSING_POINT);
33});
35export const pointCache = needBuilder(() => {
36 throw new Error(BRIDGE_ERROR.MISSING_POINT);
37});
39export const fromPointCache = (cache, point) => {
40 if (!(point in cache)) {
41 throw new Error(BRIDGE_ERROR.MISSING_POINT);
42 }
44 return cache[point];
45};
47export const keystore = obj => {
48 const ks = needBuilder(BRIDGE_ERROR.MISSING_KEYSTORE)(obj);
49 return ks.value.matchWith({
50 Ok: result => result.value,
51 Error: _ => {
52 throw new Error(BRIDGE_ERROR.MISSING_KEYSTORE);
53 },
54 });
55};