on 2d3487f help sign in
bridge/src/store/roller.ts
86 lines 3.0 kB history raw
1import create from 'zustand';
2import { L2Point, PendingTransaction, Ownership } from '@urbit/roller-api';
4import { HOUR, isL2, isL2Spawn } from 'lib/utils/roller';
5import { increaseProxyNonce, setProxyNonce } from 'lib/utils/nonce';
6import { Invite } from 'lib/types/Invite';
8export interface RollerStore {
9 nextBatchTime: number;
10 nextRoll: string;
11 pendingTransactions: PendingTransaction[];
12 currentPoint: L2Point | null;
13 currentL2: boolean;
14 currentL2Spawn: boolean;
15 nonces: {
16 [point: number]: Ownership;
17 };
18 increaseNonce: (point: number, proxy: string) => void;
19 setNonce: (
20 point: number,
21 owner: Ownership,
22 proxy: string,
23 nonce: number
24 ) => void;
25 setNonces: (point: number, owner: Ownership) => void;
26 invites: Invite[];
27 recentlyCompleted: number;
28 setNextBatchTime: (nextBatchTime: number) => void;
29 setNextRoll: (nextRoll: string) => void;
30 setPendingTransactions: (pendingTransactions: PendingTransaction[]) => void;
31 setCurrentPoint: (currentPoint: L2Point) => void;
32 setInvites: (invites: Invite[]) => void;
35export const useRollerStore = create<RollerStore>(set => ({
36 nextBatchTime: new Date().getTime() + HOUR,
37 nextRoll: '0h 00m 00s',
38 pendingTransactions: [],
39 currentPoint: null,
40 currentL2: false,
41 currentL2Spawn: false,
42 invites: [],
43 recentlyCompleted: 0,
44 nonces: {},
45 increaseNonce: (point: number, proxy: string) =>
46 set(state => {
47 const owner = increaseProxyNonce(state.nonces[point], proxy);
48 console.log(point, proxy, owner);
49 if (!owner) throw new Error("Can't increase nonce for this proxy");
50 return { ...state, nonces: { ...state.nonces, [point]: owner } };
51 }),
52 setNonce: (point: number, owner: Ownership, proxy: string, nonce: number) =>
53 set(state => {
54 const updatedOwner = setProxyNonce(owner, proxy, nonce);
55 if (!updatedOwner) throw new Error("Can't set nonce for this proxy");
56 return {
57 ...state,
58 nonces: { ...state.nonces, [point]: updatedOwner },
59 };
60 }),
61 setNonces: (point: number, owner: Ownership) =>
62 set(state => {
63 return { ...state, nonces: { ...state.nonces, [point]: owner } };
64 }),
65 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
66 setNextRoll: (nextRoll: string) => set(() => ({ nextRoll })),
67 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
68 set(state => {
69 const oldPending = state.pendingTransactions.length;
70 if (oldPending > 0 && pendingTransactions.length === 0) {
71 return {
72 ...state,
73 pendingTransactions,
74 recentlyCompleted: oldPending,
75 };
76 }
77 return { ...state, pendingTransactions };
78 }),
79 setCurrentPoint: (currentPoint: L2Point) =>
80 set(() => ({
81 currentPoint,
82 currentL2: isL2(currentPoint?.dominion),
83 currentL2Spawn: isL2Spawn(currentPoint?.dominion),
84 })),
85 setInvites: (invites: Invite[]) => set(() => ({ invites })),
86}));