forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-simulation.ts
More file actions
107 lines (96 loc) · 3.71 KB
/
Copy pathuse-simulation.ts
File metadata and controls
107 lines (96 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use client'
import { useCallback, useEffect, useRef, useState } from 'react'
import type { Plan } from '@/lib/api'
import { type BrowserSimulation, type NativeWords, simulatePlan } from '@/lib/simulate'
/**
* The page's own simulation, run in the browser when the plan is opened.
*
* Why here and not only on the service: the stored run describes the block
* the plan was built against, and somebody opening a link is reading minutes
* later. Balances move, allowances get spent, a token pauses. The run that
* matters to a person about to sign is the one against the chain as it is,
* and the browser is where "as it is" means now.
*
* It starts once per plan version and can be asked again — before submitting,
* or by the person. It never blocks the page: the plan renders immediately
* from what the service stored, and this replaces it when it lands.
*/
export type SimulationState =
| { kind: 'idle' }
| { kind: 'running' }
| { kind: 'done'; run: BrowserSimulation }
/** The chain would not answer. Not a verdict, and never shown as one. */
| { kind: 'unavailable' }
/**
* The live run as a component takes it: the state it is in, what it found,
* and how to ask again. Declared beside the hook so a component can name the
* shape without importing the hook itself.
*/
export interface LiveSimulation {
kind: SimulationState['kind']
run: BrowserSimulation | null
again: () => void
}
export interface UseSimulation {
state: SimulationState
/** The live run, or null while there is none. */
run: BrowserSimulation | null
/** Run it again, traced, and show the result. */
again: () => Promise<BrowserSimulation | null>
/**
* The check immediately before the wallet opens.
*
* Untraced, because it only needs to know whether the calls still succeed,
* and tracing is about six requests fired at the exact moment the wallet is
* about to be asked to sign. It does not publish either: replacing a traced
* run with an untraced one would wipe the asset rows off the page the
* instant somebody pressed Sign.
*/
recheck: () => Promise<BrowserSimulation | null>
}
export function useSimulation(
plan: Plan | null,
chainId: string | null,
native: NativeWords | null,
): UseSimulation {
const [state, setState] = useState<SimulationState>({ kind: 'idle' })
// Keyed on the hash, so a plan that is replaced by a new version simulates
// again and one that merely re-renders does not.
const started = useRef<string | null>(null)
const live = useRef(true)
useEffect(() => {
live.current = true
return () => {
live.current = false
}
}, [])
const go = useCallback(async (): Promise<BrowserSimulation | null> => {
if (!plan || !chainId || !native) return null
setState({ kind: 'running' })
try {
const run = await simulatePlan(plan, chainId, native)
if (live.current) setState({ kind: 'done', run })
return run
} catch {
// A chain that will not answer says nothing about the plan. The page
// falls back to what the service stored and says which it is showing.
if (live.current) setState({ kind: 'unavailable' })
return null
}
}, [plan, chainId, native])
const recheck = useCallback(async (): Promise<BrowserSimulation | null> => {
if (!plan || !chainId || !native) return null
try {
return await simulatePlan(plan, chainId, native, { trace: false })
} catch {
return null
}
}, [plan, chainId, native])
useEffect(() => {
if (!plan || !chainId || !native) return
if (started.current === plan.planHash) return
started.current = plan.planHash
void go()
}, [plan, chainId, native, go])
return { state, run: state.kind === 'done' ? state.run : null, again: go, recheck }
}