on master help sign in
Merge pull request #976 from urbit/t/configurable-roller-endpoint
dcbd38btomholford
README.md+16
@@ -54,3 +54,19 @@ A proper "offline mode" for the latest iteration of Bridge is still being worked
54## Development
54## Development
55
55
56See [development.md](DEVELOPMENT.md).
56See [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 = {
106 MAINNET: 'roller.urbit.org',
106 MAINNET: 'roller.urbit.org',
107};
107};
108
108
109+const ROLLER_PATH = '/v1/roller';
110+
109const POINT_DOMINIONS = {
111const POINT_DOMINIONS = {
110 L1: 'l1',
112 L1: 'l1',
111 L2: 'l2',
113 L2: 'l2',
@@ -250,6 +252,7 @@ export {
250 ZOD,
252 ZOD,
251 PROGRESS_ANIMATION_DELAY_MS,
253 PROGRESS_ANIMATION_DELAY_MS,
252 ROLLER_HOSTS,
254 ROLLER_HOSTS,
255+ ROLLER_PATH,
253 POINT_DOMINIONS,
256 POINT_DOMINIONS,
254 POINT_PROXIES,
257 POINT_PROXIES,
255 DEFAULT_FADE_TIMEOUT,
258 DEFAULT_FADE_TIMEOUT,
src/lib/flags.js+5
@@ -6,3 +6,8 @@ export const isRopsten = process.env.REACT_APP_ROPSTEN === 'true';
6export const isMainnet = process.env.REACT_APP_MAINNET === 'true';
6export const isMainnet = process.env.REACT_APP_MAINNET === 'true';
7export const versionLabel =
7export const versionLabel =
8 process.env.REACT_APP_BRIDGE_VERSION || `${version}${isRopsten ? 'r' : ''}`;
8 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';
22
22
23import { getUpdatedPointMessage, isPlanet, toL1Details } from './utils/point';
23import { getUpdatedPointMessage, isPlanet, toL1Details } from './utils/point';
24import { convertToInt } from './convertToInt';
24import { convertToInt } from './convertToInt';
25−import { isDevelopment, isMainnet, isRopsten } from './flags';
25+import { isDevelopment } from './flags';
26import {
26import {
27 generateInviteWallet,
27 generateInviteWallet,
28 getPendingSpawns,
28 getPendingSpawns,
@@ -30,14 +30,13 @@ import {
30 registerProxyAddress,
30 registerProxyAddress,
31 isL2Spawn,
31 isL2Spawn,
32} from './utils/roller';
32} from './utils/roller';
33−import { POINT_DOMINIONS, ROLLER_HOSTS, TEN_SECONDS } from './constants';
33+import { POINT_DOMINIONS, TEN_SECONDS } from './constants';
34
34
35import {
35import {
36 Config,
36 Config,
37 Ship,
37 Ship,
38 Proxy,
38 Proxy,
39 RollerRPCAPI,
39 RollerRPCAPI,
40− Options,
41 EthAddress,
40 EthAddress,
42 UnspawnedPoints,
41 UnspawnedPoints,
43} from '@urbit/roller-api';
42} from '@urbit/roller-api';
@@ -50,6 +49,7 @@ import { showNotification } from './utils/notifications';
50import { useWalletConnect } from './useWalletConnect';
49import { useWalletConnect } from './useWalletConnect';
51import { PendingL1Txn } from './types/PendingL1Transaction';
50import { PendingL1Txn } from './types/PendingL1Transaction';
52import { TRANSACTION_PROGRESS } from './reticket';
51import { TRANSACTION_PROGRESS } from './reticket';
52+import { useRollerOptions } from './useRollerOptions';
53
53
54interface UpdateParams {
54interface UpdateParams {
55 point: number;
55 point: number;
@@ -108,25 +108,7 @@ export default function useRoller() {
108 } = useRollerStore();
108 } = useRollerStore();
109 const [config, setConfig] = useState<Config | null>(null);
109 const [config, setConfig] = useState<Config | null>(null);
110
110
111− const options: Options = useMemo(() => {
111+ const { options } = useRollerOptions();
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− }, []);
130
112
131 const api = useMemo(() => {
113 const api = useMemo(() => {
132 return new RollerRPCAPI(options);
114 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+};