on master help sign in
Update to getInvites logic
200d8b8Will Galebach
src/lib/useRoller.ts+114−112
@@ -222,132 +222,134 @@ export default function useRoller() {
222 }
222 }
223 }, [api, setPendingTransactions, pointCursor]);
223 }, [api, setPendingTransactions, pointCursor]);
224
224
225− const getInvites = useCallback(async () => {
225+ const getInvites = useCallback(
226+ async (isL2: boolean) => {
226− try {
227+ try {
227− const curPoint: string = need.point(pointCursor);
228+ const curPoint: string = need.point(pointCursor);
228− const invites = getStoredInvites(curPoint);
229+ const invites = getStoredInvites(curPoint);
229− const availableInvites = invites.available;
230+ const availableInvites = invites.available;
230−
231+
231− const pendingTransactions = await api.getPendingByShip(curPoint);
232+ const pendingTransactions = await api.getPendingByShip(curPoint);
232− console.log('PENDING', pendingTransactions);
233+ console.log('PENDING', pendingTransactions);
233− setPendingTransactions(pendingTransactions);
234+ setPendingTransactions(pendingTransactions);
234−
235+
235− const stillPending = invites.pending.filter(invite => {
236+ const stillPending = invites.pending.filter(invite => {
236− const completed = !pendingTransactions.find(
237+ const completed = !pendingTransactions.find(
237− p => p?.rawTx?.tx?.tx?.data?.ship === invite.planet
238+ p => p?.rawTx?.tx?.tx?.data?.ship === invite.planet
238− );
239+ );
239−
240− if (completed) {
241− availableInvites.push({ ...invite, status: 'available' });
242− }
243
240
244− return !completed;
241+ if (completed) {
245− });
242+ availableInvites.push({ ...invite, status: 'available' });
243+ }
246
244
247− setStoredInvites(curPoint, {
245+ return !completed;
248− available: availableInvites,
246+ });
249− pending: stillPending,
250− claimed: invites.claimed,
251− });
252− setInvites(availableInvites);
253
247
254− let possibleMissingInvites: number[] = [];
248+ setStoredInvites(curPoint, {
249+ available: availableInvites,
250+ pending: stillPending,
251+ claimed: invites.claimed,
252+ });
253+ setInvites(availableInvites);
255
254
256− if (currentL2) {
257− const allSpawned = await api.getSpawned(Number(curPoint));
258− const ownedPoints = maybeGetResult(controlledPoints, 'ownedPoints', []);
259− possibleMissingInvites = ownedPoints.filter(
255+ let possibleMissingInvites: number[] = [];
260− (p: number) =>
261− allSpawned.includes(p) &&
262− azimuth.azimuth.getPointSize(p) === azimuth.azimuth.PointSize.Planet
263− );
264− } else {
265− const maybeOutgoingPoints = controlledPoints.chain((points: any) =>
266− points.matchWith({
267− Error: () => Nothing(),
268− Ok: (c: any) => {
269− const points = c.value.ownedPoints.map((point: number) =>
270− getDetails(point).chain((details: any) =>
271− Just({ point: point, has: hasTransferProxy(details) })
272− )
273− );
274− if (points.every((p: any) => Just.hasInstance(p))) {
275− const outgoing = points
276− .filter((p: any) => p.value.has)
277− .map((p: any) => p.value.point);
278− return Just(outgoing);
279− } else {
280− return Nothing();
281− }
282− },
283− })
284− );
285
256
286− possibleMissingInvites = maybeOutgoingPoints
257+ if (isL2) {
287− .getOrElse([])
258+ const allSpawned = await api.getSpawned(Number(curPoint));
288− .filter(
259+ const ownedPoints = maybeGetResult(controlledPoints, 'ownedPoints', []);
260+ possibleMissingInvites = allSpawned.filter(
289 (p: number) =>
261 (p: number) =>
290− azimuth.azimuth.getPointSize(p) ===
262+ ownedPoints.includes(p) &&
291− azimuth.azimuth.PointSize.Planet
263+ azimuth.azimuth.getPointSize(p) === azimuth.azimuth.PointSize.Planet
264+ );
265+ } else {
266+ const maybeOutgoingPoints = controlledPoints.chain((points: any) =>
267+ points.matchWith({
268+ Error: () => Nothing(),
269+ Ok: (c: any) => {
270+ const points = c.value.ownedPoints.map((point: number) =>
271+ getDetails(point).chain((details: any) =>
272+ Just({ point: point, has: hasTransferProxy(details) })
273+ )
274+ );
275+ if (points.every((p: any) => Just.hasInstance(p))) {
276+ const outgoing = points
277+ .filter((p: any) => p.value.has)
278+ .map((p: any) => p.value.point);
279+ return Just(outgoing);
280+ } else {
281+ return Nothing();
282+ }
283+ },
284+ })
292 );
285 );
293− }
294
286
295− // Iterate over all spawned and controlled planets
287+ possibleMissingInvites = maybeOutgoingPoints
296− // If the planet is not in available invites, generate the ticket and add it
288+ .getOrElse([])
297− const _authToken = authToken.getOrElse(null);
289+ .filter(
298− const _contracts = contracts.getOrElse(null);
290+ (p: number) =>
291+ azimuth.azimuth.getPointSize(p) ===
292+ azimuth.azimuth.PointSize.Planet
293+ );
294+ }
299
295
300− const newClaimed = availableInvites.filter(
296+ // Iterate over all spawned and controlled planets
301− ({ planet }) => !possibleMissingInvites.includes(planet)
297+ // If the planet is not in available invites, generate the ticket and add it
302− );
298+ const _authToken = authToken.getOrElse(null);
299+ const _contracts = contracts.getOrElse(null);
303
300
301+ const newClaimed = availableInvites.filter(
302+ ({ planet }) => !possibleMissingInvites.includes(planet)
303+ );
304+
304− if (_authToken && _contracts) {
305+ if (_authToken && _contracts) {
305− for (let i = 0; i < possibleMissingInvites.length; i++) {
306+ for (let i = 0; i < possibleMissingInvites.length; i++) {
306− const planet = possibleMissingInvites[i];
307+ const planet = possibleMissingInvites[i];
307−
308+
308− if (!availableInvites.find(hasPoint(planet))) {
309+ if (!availableInvites.find(hasPoint(planet))) {
309− console.log('MISSING IN AVAILABLE', planet);
310+ console.log('MISSING IN AVAILABLE', planet);
310− const {
311+ const {
311− ticket,
312+ ticket,
312− owner,
313+ owner,
313− } = await wg.generateTemporaryDeterministicWallet(
314+ } = await wg.generateTemporaryDeterministicWallet(
314− planet,
315+ planet,
315− _authToken
316+ _authToken
316− );
317+ );
317
318
318− availableInvites.push({
319+ availableInvites.push({
319− ticket,
320+ ticket,
320− status: 'available',
321+ status: 'available',
321− planet,
322+ planet,
322− hash: '',
323+ hash: '',
323− owner: owner.keys.address,
324+ owner: owner.keys.address,
324− });
325+ });
326+ }
325 }
327 }
326− }
327
328
328− setStoredInvites(curPoint, {
329+ setStoredInvites(curPoint, {
329− available: availableInvites.filter(({ planet }) =>
330+ available: availableInvites.filter(({ planet }) =>
330− possibleMissingInvites.includes(planet)
331+ possibleMissingInvites.includes(planet)
331− ),
332+ ),
332− pending: stillPending,
333+ pending: stillPending,
333− claimed: invites.claimed.concat(newClaimed),
334+ claimed: invites.claimed.concat(newClaimed),
334− });
335+ });
335− setInvites(availableInvites);
336+ setInvites(availableInvites);
337+ }
338+ } catch (error) {
339+ console.warn('ERROR GETTING INVITES', error);
336 }
340 }
337− } catch (error) {
341+ },
338− console.warn('ERROR GETTING INVITES', error);
339− }
340− }, [
342+ [
341− api,
343+ api,
342− pointCursor,
344+ pointCursor,
343− setInvites,
345+ setInvites,
344− setPendingTransactions,
346+ setPendingTransactions,
345− authToken,
347+ authToken,
346− contracts,
348+ contracts,
347− controlledPoints,
349+ controlledPoints,
348− currentL2,
349− getDetails,
350+ getDetails,
351+ ]
350− ]);
352+ );
351
353
352 // On load, get initial config
354 // On load, get initial config
353 useEffect(() => {
355 useEffect(() => {
src/store/roller.ts+3−2
@@ -4,6 +4,8 @@ import { L2Point, PendingTransaction } from '@urbit/roller-api/build';
4import { HOUR } from 'lib/utils/roller';
4import { HOUR } from 'lib/utils/roller';
5import { Invite } from 'types/Invite';
5import { Invite } from 'types/Invite';
6
6
7+export const isL2 = (dom?: string) => dom === 'l2' || dom === 'spawn';
8+
7export interface RollerStore {
9export interface RollerStore {
8 nextBatchTime: number;
10 nextBatchTime: number;
9 nextRoll: string;
11 nextRoll: string;
@@ -44,8 +46,7 @@ export const useRollerStore = create<RollerStore>(set => ({
44 setCurrentPoint: (currentPoint: L2Point) =>
46 setCurrentPoint: (currentPoint: L2Point) =>
45 set(() => ({
47 set(() => ({
46 currentPoint,
48 currentPoint,
47− currentL2:
48− currentPoint?.dominion === 'l2' || currentPoint?.dominion === 'spawn',
49+ currentL2: isL2(currentPoint?.dominion),
49 })),
50 })),
50 setInvites: (invites: Invite[]) => set(() => ({ invites })),
51 setInvites: (invites: Invite[]) => set(() => ({ invites })),
51}));
52}));
src/views/Point.js+4−4
@@ -8,7 +8,7 @@ import { azimuth } from 'azimuth-js';
8import { usePointCursor } from 'store/pointCursor';
8import { usePointCursor } from 'store/pointCursor';
9import { useWallet } from 'store/wallet';
9import { useWallet } from 'store/wallet';
10import { usePointCache } from 'store/pointCache';
10import { usePointCache } from 'store/pointCache';
11−import { useRollerStore } from 'store/roller';
11+import { isL2, useRollerStore } from 'store/roller';
12
12
13import View from 'components/View';
13import View from 'components/View';
14import Greeting from 'components/Greeting';
14import Greeting from 'components/Greeting';
@@ -142,7 +142,7 @@ export default function Point() {
142 const pointInfo = await api.getPoint(Number(point));
142 const pointInfo = await api.getPoint(Number(point));
143 setCurrentPoint(pointInfo);
143 setCurrentPoint(pointInfo);
144
144
145− getInvites();
145+ getInvites(isL2(pointInfo.dominion));
146 };
146 };
147
147
148 if (!loaded) {
148 if (!loaded) {
@@ -313,7 +313,7 @@ export default function Point() {
313 onClick={goBitcoin}>
313 onClick={goBitcoin}>
314 Bitcoin
314 Bitcoin
315 </Grid.Item> */}
315 </Grid.Item> */}
316− {/* {isParent && (
316+ {isParent && (
317 <>
317 <>
318 <Grid.Item
318 <Grid.Item
319 full
319 full
@@ -324,7 +324,7 @@ export default function Point() {
324 </Grid.Item>
324 </Grid.Item>
325 <Grid.Divider />
325 <Grid.Divider />
326 </>
326 </>
327− )} */}
327+ )}
328 {senateButton}
328 {senateButton}
329 </Grid>
329 </Grid>
330 </View>
330 </View>