on 9155372 help sign in
bridge/src/store/rollerStore.ts
95 lines 3.4 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 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;
34 setLoading: (loading: boolean) => void;
35 setModalText: (modalText: string) => void;
36 setNextQuotaTime: (nextQuotaTime: number) => void;
37 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
38 setPoint: (point: number) => void;
39 setPoints: (points: Points) => void;
40 updatePoint: (point: Point) => void;
41 storePendingL1Txn: (txn: PendingL1Txn) => void;
42 deletePendingL1Txn: (txn: PendingL1Txn) => void;
43 setEthBalance: (ethBalance: BN) => void;
46export const useRollerStore = create<RollerStore>(set => ({
47 loading: false,
48 nextQuotaTime: new Date().getTime() + 24 * HOUR,
49 pendingTransactions: [],
50 point: EMPTY_POINT,
51 pointList: [],
52 points: {},
53 pendingL1ByPoint: {},
54 recentlyCompleted: 0,
55 ethBalance: toBN(0),
56 setLoading: (loading: boolean) => set(() => ({ loading })),
57 setNextQuotaTime: (nextQuotaTime: number) => set(() => ({ nextQuotaTime })),
58 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
59 set(state => {
60 const oldPending = state.pendingTransactions.length;
61 if (oldPending > 0 && pendingTransactions.length === 0) {
62 return {
63 ...state,
64 pendingTransactions,
65 recentlyCompleted: oldPending,
66 };
67 }
68 return { ...state, pendingTransactions };
69 }),
70 setPoint: (point: number) =>
71 set(state => ({ point: state.points[point] || EMPTY_POINT })),
72 setPoints: (points: Points) =>
73 set(({ point }) => getPointsAndList(points, point)),
74 updatePoint: (newPoint: Point) =>
75 set(({ point, points }) => {
76 const newPoints: Points = Object.assign({}, points);
77 newPoints[newPoint.value] = newPoint;
78 return getPointsAndList(newPoints, point);
79 }),
80 setModalText: (modalText?: string) => set(() => ({ modalText })),
81 storePendingL1Txn: (txn: PendingL1Txn) =>
82 set(({ pendingL1ByPoint }) => {
83 const newPending = Object.assign({}, pendingL1ByPoint);
84 newPending[txn.point] = [...(pendingL1ByPoint[txn.point] || []), txn];
85 return { pendingL1ByPoint: newPending };
86 }),
87 deletePendingL1Txn: (txn: PendingL1Txn) =>
88 set(({ pendingL1ByPoint }) => {
89 const newPending = Object.assign({}, pendingL1ByPoint);
90 newPending[txn.point] =
91 pendingL1ByPoint[txn.point]?.filter(({ id }) => id !== txn.id) || [];
92 return { pendingL1ByPoint: newPending };
93 }),
94 setEthBalance: (ethBalance: BN) => set(() => ({ ethBalance })),
95}));