-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstate.ts
50 lines (42 loc) · 1.12 KB
/
state.ts
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
import { produce, type Draft } from 'immer';
import type { Config } from '../config/schema';
import type {
PrivateKey,
ChainId,
SignedData,
BeaconId,
ProviderName,
SignedApiUrl,
AirnodeAddress,
DapiNameOrDataFeedId,
} from '../types';
interface GasState {
gasPrices: { price: bigint; timestamp: number }[];
sponsorLastUpdateTimestamp: Record<string, number>;
}
export interface State {
config: Config;
gasPrices: Record<ChainId, Record<ProviderName, GasState>>;
derivedSponsorWallets: Record<DapiNameOrDataFeedId, PrivateKey>;
signedDatas: Record<BeaconId, SignedData>;
signedApiUrls: Record<ChainId, Record<ProviderName, Record<AirnodeAddress, SignedApiUrl>>>;
}
let state: State | undefined;
export const getState = (): State => {
if (!state) {
throw new Error('State is undefined.');
}
return state;
};
export const setInitialState = (config: Config) => {
state = {
config,
gasPrices: {},
signedDatas: {},
signedApiUrls: {},
derivedSponsorWallets: {},
};
};
export const updateState = (updater: (draft: Draft<State>) => void) => {
state = produce(getState(), updater);
};