on f1ff0cf help sign in
bridge/src/lib/need.js
40 lines 1.0 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 = (cache, point) => {
26 if (!(point in cache)) {
27 throw BRIDGE_ERROR.MISSING_POINT;
28 }
30 return cache[point];
31};
32export const keystore = obj => {
33 const ks = needBuilder(BRIDGE_ERROR.MISSING_KEYSTORE)();
34 return ks.value.matchWith({
35 Ok: result => result.value,
36 Error: _ => {
37 throw BRIDGE_ERROR.MISSING_KEYSTORE;
38 },
39 });
40};