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 RollerState {
25 nextQuotaTime: number;
26 pendingTransactions: PendingTransaction[];
30 pendingL1ByPoint: PendingL1;
32 recentlyCompleted: number;
36export interface RollerActions {
37 setLoading: (loading: boolean) => void;
38 setModalText: (modalText: string) => void;
39 setNextQuotaTime: (nextQuotaTime: number) => void;
40 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
41 setPoint: (point: number) => void;
42 setPoints: (points: Points) => void;
43 updatePoint: (point: Point) => void;
44 storePendingL1Txn: (txn: PendingL1Txn) => void;
45 deletePendingL1Txn: (txn: PendingL1Txn) => void;
46 setEthBalance: (ethBalance: BN) => void;
47 resetStore: () => void;
50const initialRollerState: RollerState = {
52 nextQuotaTime: new Date().getTime() + 24 * HOUR,
53 pendingTransactions: [],
62export const useRollerStore = create<RollerState & RollerActions>(set => ({
63 ...initialRollerState,
64 setLoading: (loading: boolean) => set(() => ({ loading })),
65 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
66 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
68 const oldPending = state.pendingTransactions.length;
69 if (oldPending > 0 && pendingTransactions.length === 0) {
73 recentlyCompleted: oldPending,
76 return { ...state, pendingTransactions };
78 setPoint: (point: number) =>
79 set(state => ({ point: state.points[point] || EMPTY_POINT })),
80 setPoints: (points: Points) =>
81 set(({ point }) => getPointsAndList(points, point)),
82 updatePoint: (newPoint: Point) =>
83 set(({ point, points }) => {
84 const newPoints: Points = Object.assign({}, points);
85 newPoints[newPoint.value] = newPoint;
86 return getPointsAndList(newPoints, point);
88 setModalText: (modalText?: string) => set(() => ({ modalText })),
89 storePendingL1Txn: (txn: PendingL1Txn) =>
90 set(({ pendingL1ByPoint }) => {
91 const newPending = Object.assign({}, pendingL1ByPoint);
92 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
93 return { pendingL1ByPoint: newPending };
95 deletePendingL1Txn: (txn: PendingL1Txn) =>
96 set(({ pendingL1ByPoint }) => {
97 const newPending = Object.assign({}, pendingL1ByPoint);
98 newPending[txn.point] =
99 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
100 return { pendingL1ByPoint: newPending };
102 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
104 set(initialRollerState);
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