on a3bbc03 help sign in
bridge/src/lib/need.js
41 lines 1.1 kB history raw
1// need.js: +need but worse
2//
4import { BRIDGE_ERROR } from './error';
6const needBuilder = error => obj => {
7 if (!obj) {
8 throw error;
9 }
11 return obj.matchWith({
12 Nothing: () => {
13 throw error;
14 },
15 Just: p => p.value,
16 });
17};
19export const web3 = needBuilder(BRIDGE_ERROR.MISSING_WEB3);
20export const contracts = needBuilder(BRIDGE_ERROR.MISSING_CONTRACTS);
21export const wallet = needBuilder(BRIDGE_ERROR.MISSING_WALLET);
22export const addressFromWallet = obj => wallet(obj).address;
23export const pointCursor = needBuilder(BRIDGE_ERROR.MISSING_POINT);
24export const pointCache = needBuilder(BRIDGE_ERROR.MISSING_POINT);
25export const fromPointCache = (obj, point) => {
26 const cache = pointCache(obj);
27 if (!(point in cache)) {
28 throw BRIDGE_ERROR.MISSING_POINT;
29 }
31 return cache[point];
32};
33export const keystore = obj => {
34 const ks = needBuilder(BRIDGE_ERROR.MISSING_KEYSTORE)();
35 return ks.value.matchWith({
36 Ok: result => result.value,
37 Error: _ => {
38 throw BRIDGE_ERROR.MISSING_KEYSTORE;
39 },
40 });
41};