on master help sign in
bridge/src/store/rollerStore.ts
106 lines 3.6 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 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) => ({
12 points,
13 pointList: Object.values(points).sort((a, b) => a.value - b.value),
14 point: point.isDefault ? point : points[point.value],
15});
17export const EMPTY_POINT = new Point({
18 value: -1,
19 details: toL1Details({}),
20 address: '',
21});
23export interface RollerState {
24 loading: boolean;
25 nextQuotaTime: number;
26 pendingTransactions: PendingTransaction[];
27 point: Point;
28 points: Points;
29 pointList: Point[];
30 pendingL1ByPoint: PendingL1;
31 modalText?: string;
32 recentlyCompleted: number;
33 ethBalance: BN;
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 = {
51 loading: false,
52 nextQuotaTime: new Date().getTime() + 24 * HOUR,
53 pendingTransactions: [],
54 point: EMPTY_POINT,
55 pointList: [],
56 points: {},
57 pendingL1ByPoint: {},
58 recentlyCompleted: 0,
59 ethBalance: toBN(0),
60};
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[]) =>
67 set(state => {
68 const oldPending = state.pendingTransactions.length;
69 if (oldPending > 0 && pendingTransactions.length === 0) {
70 return {
71 ...state,
72 pendingTransactions,
73 recentlyCompleted: oldPending,
74 };
75 }
76 return { ...state, pendingTransactions };
77 }),
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);
87 }),
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 };
94 }),
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 };
101 }),
102 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
103 resetStore: () => {
104 set(initialRollerState);
105 },
106}));