on master help sign in
api: configurable Roller endpoint via env

Before this change, Bridge was configured to only use the L2 roller provided by Tlon. This change allows a Bridge user to use one or more runtime env vars to connect to a custom roller. Resolves #974

commit7f89035363e0b60f6f2b032ac2a89c3744417f5b authortomholford <tomholford@users.noreply.github.com> dateSun 20 Feb 2022, 10:41 (5 years ago) parent9875f17
5 files changed+87−22
README.md+16 src/lib/constants.ts+3 src/lib/flags.js+5 src/lib/useRoller.ts+4−22 src/lib/useRollerOptions.ts+59
README.md+16
@@ -54,3 +54,19 @@ A proper "offline mode" for the latest iteration of Bridge is still being worked
5454## Development
5555
5656See [development.md](DEVELOPMENT.md).
57+
58+
59+## Configurable Roller Endpoint
60+
61+By default, Bridge uses Tlon's L2 Roller, but can also be configured to use your own.
62+
63+For example:
64+
65+```sh
66+REACT_APP_ROLLER_HOST=my-personal-roller.net npm run pilot-mainnet
67+```
68+
69+The following are configurable, and will otherwise fallback to the defaults in `constants`:
70+- `REACT_APP_ROLLER_HOST` - host
71+- `REACT_APP_ROLLER_PORT` - port
72+- `REACT_APP_ROLLER_TRANSPORT_TYPE` - transport type (e.g., `http` or `https`)
src/lib/constants.ts+3
@@ -106,6 +106,8 @@ const ROLLER_HOSTS = {
106106 MAINNET: 'roller.urbit.org',
107107};
108108
109+const ROLLER_PATH = '/v1/roller';
110+
109111const POINT_DOMINIONS = {
110112 L1: 'l1',
111113 L2: 'l2',
@@ -250,6 +252,7 @@ export {
250252 ZOD,
251253 PROGRESS_ANIMATION_DELAY_MS,
252254 ROLLER_HOSTS,
255+ ROLLER_PATH,
253256 POINT_DOMINIONS,
254257 POINT_PROXIES,
255258 DEFAULT_FADE_TIMEOUT,
src/lib/flags.js+5
@@ -6,3 +6,8 @@ export const isRopsten = process.env.REACT_APP_ROPSTEN === 'true';
66export const isMainnet = process.env.REACT_APP_MAINNET === 'true';
77export const versionLabel =
88 process.env.REACT_APP_BRIDGE_VERSION || `${version}${isRopsten ? 'r' : ''}`;
9+export const providedRollerOptions = {
10+ host: process.env.REACT_APP_ROLLER_HOST,
11+ port: process.env.REACT_APP_ROLLER_PORT,
12+ type: process.env.REACT_APP_ROLLER_TRANSPORT_TYPE,
13+};
src/lib/useRoller.ts+4−22
@@ -22,7 +22,7 @@ import { usePointCache } from 'store/pointCache';
2222
2323import { getUpdatedPointMessage, isPlanet, toL1Details } from './utils/point';
2424import { convertToInt } from './convertToInt';
25−import { isDevelopment, isMainnet, isRopsten } from './flags';
25+import { isDevelopment } from './flags';
2626import {
2727 generateInviteWallet,
2828 getPendingSpawns,
@@ -30,14 +30,13 @@ import {
3030 registerProxyAddress,
3131 isL2Spawn,
3232} from './utils/roller';
33−import { POINT_DOMINIONS, ROLLER_HOSTS, TEN_SECONDS } from './constants';
33+import { POINT_DOMINIONS, TEN_SECONDS } from './constants';
3434
3535import {
3636 Config,
3737 Ship,
3838 Proxy,
3939 RollerRPCAPI,
40− Options,
4140 EthAddress,
4241 UnspawnedPoints,
4342} from '@urbit/roller-api';
@@ -50,6 +49,7 @@ import { showNotification } from './utils/notifications';
5049import { useWalletConnect } from './useWalletConnect';
5150import { PendingL1Txn } from './types/PendingL1Transaction';
5251import { TRANSACTION_PROGRESS } from './reticket';
52+import { useRollerOptions } from './useRollerOptions';
5353
5454interface UpdateParams {
5555 point: number;
@@ -108,25 +108,7 @@ export default function useRoller() {
108108 } = useRollerStore();
109109 const [config, setConfig] = useState<Config | null>(null);
110110
111− const options: Options = useMemo(() => {
112− const type = isMainnet || isRopsten ? 'https' : 'http';
113− const host = isMainnet
114− ? ROLLER_HOSTS.MAINNET
115− : isRopsten
116− ? ROLLER_HOSTS.ROPSTEN
117− : ROLLER_HOSTS.LOCAL;
118− const port = isMainnet || isRopsten ? 443 : 8080;
119− const path = '/v1/roller';
120−
121− return {
122− transport: {
123− type,
124− host,
125− port,
126− path,
127− },
128− };
129− }, []);
111+ const { options } = useRollerOptions();
130112
131113 const api = useMemo(() => {
132114 return new RollerRPCAPI(options);
src/lib/useRollerOptions.tsnew file+59
@@ -0,0 +1,59 @@
1+import { Options } from '@urbit/roller-api';
2+import { useMemo } from 'react';
3+import { ROLLER_HOSTS, ROLLER_PATH } from './constants';
4+import { convertToInt } from './convertToInt';
5+import { isMainnet, isRopsten, providedRollerOptions } from './flags';
6+
7+type TransportType =
8+ | 'websocket'
9+ | 'http'
10+ | 'https'
11+ | 'postmessagewindow'
12+ | 'postmessageiframe';
13+
14+/**
15+ * This hook produces a Roller API options config based on the runtime env.
16+ *
17+ * By default, it uses the hardcoded values from constants.ts, but optionally
18+ * a consumer of Bridge can provide a custom host, port, and transport type to
19+ * override these settings.
20+ *
21+ * @returns options (type Options from @urbit/roller-api)
22+ */
23+export const useRollerOptions = () => {
24+ const {
25+ host: providedHost,
26+ port: providedPort,
27+ type: providedType,
28+ } = providedRollerOptions;
29+
30+ const options: Options = useMemo(() => {
31+ const type =
32+ (providedType as TransportType | undefined) ||
33+ (isMainnet || isRopsten ? 'https' : 'http');
34+ const host =
35+ providedHost ||
36+ (isMainnet
37+ ? ROLLER_HOSTS.MAINNET
38+ : isRopsten
39+ ? ROLLER_HOSTS.ROPSTEN
40+ : ROLLER_HOSTS.LOCAL);
41+ const port =
42+ (providedPort && convertToInt(providedPort, 10)) ||
43+ (isMainnet || isRopsten ? 443 : 8080);
44+ const path = ROLLER_PATH;
45+
46+ return {
47+ transport: {
48+ type,
49+ host,
50+ port,
51+ path,
52+ },
53+ };
54+ }, [providedHost, providedPort, providedType]);
55+
56+ return {
57+ options,
58+ };
59+};