on ebdc206 help sign in
bridge/src/store/rollerStore.ts
137 lines 5.0 kB history raw
1import create from 'zustand';
2import { PendingTransaction } from '@urbit/roller-api';
3import { toBN } from 'web3-utils';
4import BN from 'bn.js';
6import { HOUR } from 'lib/utils/roller';
7import { Invite, Invites } from 'lib/types/Invite';
8import Point, { Points } from 'lib/types/Point';
9import { toL1Details } from 'lib/utils/point';
10import { PendingL1, PendingL1Txn } from 'lib/types/PendingL1Transaction';
12const getPointsAndList = (points: Points, point: Point) => ({
13 points,
14 pointList: Object.values(points).sort((a, b) => a.value - b.value),
15 point: point.isDefault ? point : points[point.value],
16});
18export const EMPTY_POINT = new Point({
19 value: -1,
20 details: toL1Details({}),
21 address: '',
22});
24export interface RollerStore {
25 loading: boolean;
26 nextBatchTime: number;
27 nextQuotaTime: number;
28 pendingTransactions: PendingTransaction[];
29 point: Point;
30 points: Points;
31 pointList: Point[];
32 pendingL1ByPoint: PendingL1;
33 invites: Invites;
34 modalText?: string;
35 recentlyCompleted: number;
36 inviteGeneratingNum: number;
37 invitesLoading: boolean;
38 ethBalance: BN;
39 setLoading: (loading: boolean) => void;
40 removeInvite: (point: number, planet: number) => void;
41 setInvites: (points: number, invites: Invite[]) => void;
42 setInviteGeneratingNum: (numGenerating: number) => void;
43 setInvitesLoading: (invitesLoading: boolean) => void;
44 setModalText: (modalText: string) => void;
45 setNextBatchTime: (nextBatchTime: number) => void;
46 setNextQuotaTime: (nextQuotaTime: number) => void;
47 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
48 setPoint: (point: number) => void;
49 setPoints: (points: Points) => void;
50 updateInvite: (point: number, invite: Invite) => void;
51 updatePoint: (point: Point) => void;
52 storePendingL1Txn: (txn: PendingL1Txn) => void;
53 deletePendingL1Txn: (txn: PendingL1Txn) => void;
54 setEthBalance: (ethBalance: BN) => void;
57export const useRollerStore = create<RollerStore>(set => ({
58 loading: false,
59 nextBatchTime: new Date().getTime() + HOUR,
60 nextQuotaTime: new Date().getTime() + 24 * HOUR,
61 pendingTransactions: [],
62 point: EMPTY_POINT,
63 pointList: [],
64 points: {},
65 pendingL1ByPoint: {},
66 invites: {},
67 recentlyCompleted: 0,
68 inviteGeneratingNum: 0,
69 invitesLoading: false,
70 ethBalance: toBN(0),
71 setLoading: (loading: boolean) => set(() => ({ loading })),
72 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
73 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
74 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
75 set(state => {
76 const oldPending = state.pendingTransactions.length;
77 if (oldPending > 0 && pendingTransactions.length === 0) {
78 return {
79 ...state,
80 pendingTransactions,
81 recentlyCompleted: oldPending,
82 };
83 }
84 return { ...state, pendingTransactions };
85 }),
86 setPoint: (point: number) =>
87 set(state => ({ point: state.points[point] || EMPTY_POINT })),
88 setPoints: (points: Points) =>
89 set(({ point }) => getPointsAndList(points, point)),
90 setInvites: (point: number, invites: Invite[]) =>
91 set(state => {
92 const newInvites: Invites = {};
93 newInvites[point] = invites;
94 return { invites: Object.assign(state.invites, newInvites) };
95 }),
96 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
97 set(() => ({ inviteGeneratingNum })),
98 updateInvite: (point: number, invite: Invite) =>
99 set(state => {
100 const newInvites: Invites = {};
101 newInvites[point] = state.invites[point]?.map(i =>
102 i.planet === invite.planet ? invite : i
103 );
104 return { invites: Object.assign(state.invites, newInvites) };
105 }),
106 removeInvite: (point: number, planet: number) =>
107 set(state => {
108 const newInvites: Invites = {};
109 newInvites[point] = state.invites[point]?.filter(
110 i => i.planet !== planet
111 );
112 return { invites: Object.assign(state.invites, newInvites) };
113 }),
114 setInvitesLoading: (invitesLoading: boolean) =>
115 set(() => ({ invitesLoading })),
116 updatePoint: (newPoint: Point) =>
117 set(({ point, points }) => {
118 const newPoints: Points = Object.assign({}, points);
119 newPoints[newPoint.value] = newPoint;
120 return getPointsAndList(newPoints, point);
121 }),
122 setModalText: (modalText?: string) => set(() => ({ modalText })),
123 storePendingL1Txn: (txn: PendingL1Txn) =>
124 set(({ pendingL1ByPoint }) => {
125 const newPending = Object.assign({}, pendingL1ByPoint);
126 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
127 return { pendingL1ByPoint: newPending };
128 }),
129 deletePendingL1Txn: (txn: PendingL1Txn) =>
130 set(({ pendingL1ByPoint }) => {
131 const newPending = Object.assign({}, pendingL1ByPoint);
132 newPending[txn.point] =
133 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
134 return { pendingL1ByPoint: newPending };
135 }),
136 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
137}));