1import create from 'zustand';
2import { PendingTransaction } from '@urbit/roller-api';
3import { toBN } from 'web3-utils';
6import { HOUR } from 'lib/utils/roller';
7import Point, { Points } from 'lib/types/Point';
8import { toL1Details } from 'lib/utils/point';
9import { PendingL1, PendingL1Txn } from 'lib/types/PendingL1Transaction';
11const getPointsAndList = (points: Points, point: Point) => ({
13 pointList: Object.values(points).sort((a, b) => a.value - b.value),
14 point: point.isDefault ? point : points[point.value],
17export const EMPTY_POINT = new Point({
19 details: toL1Details({}),
23export interface RollerStore {
25 nextBatchTime: number;
26 nextQuotaTime: number;
27 pendingTransactions: PendingTransaction[];
31 pendingL1ByPoint: PendingL1;
33 recentlyCompleted: number;
35 setLoading: (loading: boolean) => void;
36 setModalText: (modalText: string) => void;
37 setNextBatchTime: (nextBatchTime: number) => void;
38 setNextQuotaTime: (nextQuotaTime: number) => void;
39 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
40 setPoint: (point: number) => void;
41 setPoints: (points: Points) => void;
42 updatePoint: (point: Point) => void;
43 storePendingL1Txn: (txn: PendingL1Txn) => void;
44 deletePendingL1Txn: (txn: PendingL1Txn) => void;
45 setEthBalance: (ethBalance: BN) => void;
48export const useRollerStore = create<RollerStore>(set => ({
50 nextBatchTime: new Date().getTime() + HOUR,
51 nextQuotaTime: new Date().getTime() + 24 * HOUR,
52 pendingTransactions: [],
59 setLoading: (loading: boolean) => set(() => ({ loading })),
60 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
61 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
62 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
64 const oldPending = state.pendingTransactions.length;
65 if (oldPending > 0 && pendingTransactions.length === 0) {
69 recentlyCompleted: oldPending,
72 return { ...state, pendingTransactions };
74 setPoint: (point: number) =>
75 set(state => ({ point: state.points[point] || EMPTY_POINT })),
76 setPoints: (points: Points) =>
77 set(({ point }) => getPointsAndList(points, point)),
78 updatePoint: (newPoint: Point) =>
79 set(({ point, points }) => {
80 const newPoints: Points = Object.assign({}, points);
81 newPoints[newPoint.value] = newPoint;
82 return getPointsAndList(newPoints, point);
84 setModalText: (modalText?: string) => set(() => ({ modalText })),
85 storePendingL1Txn: (txn: PendingL1Txn) =>
86 set(({ pendingL1ByPoint }) => {
87 const newPending = Object.assign({}, pendingL1ByPoint);
88 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
89 return { pendingL1ByPoint: newPending };
91 deletePendingL1Txn: (txn: PendingL1Txn) =>
92 set(({ pendingL1ByPoint }) => {
93 const newPending = Object.assign({}, pendingL1ByPoint);
94 newPending[txn.point] =
95 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
96 return { pendingL1ByPoint: newPending };
98 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list