on be08dee help sign in
bridge/src/store/rollerStore.ts
99 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 RollerStore {
24 loading: boolean;
25 nextBatchTime: number;
26 nextQuotaTime: number;
27 pendingTransactions: PendingTransaction[];
28 point: Point;
29 points: Points;
30 pointList: Point[];
31 pendingL1ByPoint: PendingL1;
32 modalText?: string;
33 recentlyCompleted: number;
34 ethBalance: BN;
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 => ({
49 loading: false,
50 nextBatchTime: new Date().getTime() + HOUR,
51 nextQuotaTime: new Date().getTime() + 24 * HOUR,
52 pendingTransactions: [],
53 point: EMPTY_POINT,
54 pointList: [],
55 points: {},
56 pendingL1ByPoint: {},
57 recentlyCompleted: 0,
58 ethBalance: toBN(0),
59 setLoading: (loading: boolean) => set(() => ({ loading })),
60 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
61 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
62 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
63 set(state => {
64 const oldPending = state.pendingTransactions.length;
65 if (oldPending > 0 && pendingTransactions.length === 0) {
66 return {
67 ...state,
68 pendingTransactions,
69 recentlyCompleted: oldPending,
70 };
71 }
72 return { ...state, pendingTransactions };
73 }),
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);
83 }),
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 };
90 }),
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 };
97 }),
98 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
99}));