on c635e69 help sign in
bridge/src/store/rollerStore.ts
149 lines 5.5 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 addInvite: (point: number, invite: Invite) => void;
40 setLoading: (loading: boolean) => void;
41 removeInvite: (point: number, planet: number) => void;
42 setInvites: (points: number, invites: Invite[]) => void;
43 setInviteGeneratingNum: (numGenerating: number) => void;
44 setInvitesLoading: (invitesLoading: boolean) => void;
45 setModalText: (modalText: string) => void;
46 setNextBatchTime: (nextBatchTime: number) => void;
47 setNextQuotaTime: (nextQuotaTime: number) => void;
48 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
49 setPoint: (point: number) => void;
50 setPoints: (points: Points) => void;
51 updateInvite: (point: number, invite: Invite) => void;
52 updatePoint: (point: Point) => void;
53 storePendingL1Txn: (txn: PendingL1Txn) => void;
54 deletePendingL1Txn: (txn: PendingL1Txn) => void;
55 setEthBalance: (ethBalance: BN) => void;
58export const useRollerStore = create<RollerStore>(set => ({
59 loading: false,
60 nextBatchTime: new Date().getTime() + HOUR,
61 nextQuotaTime: new Date().getTime() + 24 * HOUR,
62 pendingTransactions: [],
63 point: EMPTY_POINT,
64 pointList: [],
65 points: {},
66 pendingL1ByPoint: {},
67 invites: {},
68 recentlyCompleted: 0,
69 inviteGeneratingNum: 0,
70 invitesLoading: false,
71 ethBalance: toBN(0),
72 setLoading: (loading: boolean) => set(() => ({ loading })),
73 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
74 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
75 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
76 set(state => {
77 const oldPending = state.pendingTransactions.length;
78 if (oldPending > 0 && pendingTransactions.length === 0) {
79 return {
80 ...state,
81 pendingTransactions,
82 recentlyCompleted: oldPending,
83 };
84 }
85 return { ...state, pendingTransactions };
86 }),
87 setPoint: (point: number) =>
88 set(state => ({ point: state.points[point] || EMPTY_POINT })),
89 setPoints: (points: Points) =>
90 set(({ point }) => getPointsAndList(points, point)),
91 setInvites: (point: number, invites: Invite[]) =>
92 set(state => {
93 const newInvites: Invites = {};
94 newInvites[point] = invites;
95 return { invites: Object.assign(state.invites, newInvites) };
96 }),
97 setInviteGeneratingNum: (inviteGeneratingNum: number) =>
98 set(() => ({ inviteGeneratingNum })),
99 addInvite: (point: number, invite: Invite) => {
100 set(state => {
101 const newInvites: Invites = {};
102 const invites = state.invites[point];
104 if (!invites.find(inv => inv.planet === invite.planet)) {
105 newInvites[point] = [...invites, invite];
106 }
107 return { invites: Object.assign(state.invites, newInvites) };
108 });
109 },
110 updateInvite: (point: number, invite: Invite) =>
111 set(state => {
112 const newInvites: Invites = {};
113 newInvites[point] = state.invites[point]?.map(i =>
114 i.planet === invite.planet ? invite : i
115 );
116 return { invites: Object.assign(state.invites, newInvites) };
117 }),
118 removeInvite: (point: number, planet: number) =>
119 set(state => {
120 const newInvites: Invites = {};
121 newInvites[point] = state.invites[point]?.filter(
122 i => i.planet !== planet
123 );
124 return { invites: Object.assign(state.invites, newInvites) };
125 }),
126 setInvitesLoading: (invitesLoading: boolean) =>
127 set(() => ({ invitesLoading })),
128 updatePoint: (newPoint: Point) =>
129 set(({ point, points }) => {
130 const newPoints: Points = Object.assign({}, points);
131 newPoints[newPoint.value] = newPoint;
132 return getPointsAndList(newPoints, point);
133 }),
134 setModalText: (modalText?: string) => set(() => ({ modalText })),
135 storePendingL1Txn: (txn: PendingL1Txn) =>
136 set(({ pendingL1ByPoint }) => {
137 const newPending = Object.assign({}, pendingL1ByPoint);
138 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
139 return { pendingL1ByPoint: newPending };
140 }),
141 deletePendingL1Txn: (txn: PendingL1Txn) =>
142 set(({ pendingL1ByPoint }) => {
143 const newPending = Object.assign({}, pendingL1ByPoint);
144 newPending[txn.point] =
145 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
146 return { pendingL1ByPoint: newPending };
147 }),
148 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
149}));