-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathsdkReduxConfig.ts
More file actions
128 lines (109 loc) · 4.93 KB
/
sdkReduxConfig.ts
File metadata and controls
128 lines (109 loc) · 4.93 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {Framework} from '@superfluid-finance/sdk-core';
import {Signer} from 'ethers';
import {memoize} from 'lodash';
// NOTE: This file is marked for side-effects inside the package.json for efficient tree-shaking.
import {RpcApiSliceEmpty} from './reduxSlices/rtkQuery/rpcApiSlice/rpcApiSlice';
import {SubgraphApiSliceEmpty} from './reduxSlices/rtkQuery/subgraphApiSlice/subgraphApiSlice';
import {TransactionTrackerSlice} from './reduxSlices/transactionTrackerSlice/transactionTrackerSlice';
interface FrameworkLocator {
getFramework: (chainId: number) => Promise<Framework>;
setFramework: (chainId: number, frameworkOrFactory: Framework | (() => Promise<Framework>)) => void;
}
interface RpcApiSliceLocator {
getRpcApiSlice: () => RpcApiSliceEmpty;
setRpcApiSlice: (api: RpcApiSliceEmpty) => void;
}
interface SubgraphApiSliceLocator {
getSubgraphApiSlice: () => SubgraphApiSliceEmpty;
setSubgraphApiSlice: (api: SubgraphApiSliceEmpty) => void;
}
interface TransactionTrackerSliceLocator {
getTransactionTrackerSlice: () => TransactionTrackerSlice;
setTransactionTrackerSlice: (slice: TransactionTrackerSlice) => void;
}
/**
* NOTE: The reason memoization is used is to avoid multiple instantiations by the factory functions.
*/
export default class SdkReduxConfig
implements FrameworkLocator, RpcApiSliceLocator, SubgraphApiSliceLocator, TransactionTrackerSliceLocator
{
rpcApiSlice: RpcApiSliceEmpty | undefined;
subgraphApiSlice: SubgraphApiSliceEmpty | undefined;
transactionTrackerSlice: TransactionTrackerSlice | undefined;
memoizedFrameworkFactories = new Map<number, () => Promise<Framework>>();
memoizedSignerFactories = new Map<number, () => Promise<Signer>>();
static getOrCreateSingleton(): SdkReduxConfig {
if (!globalThis.sdkReduxConfig) {
globalThis.sdkReduxConfig = new SdkReduxConfig();
}
return globalThis.sdkReduxConfig;
}
getRpcApiSlice(): RpcApiSliceEmpty {
if (!this.rpcApiSlice) {
throw Error('The RpcApiSlice has not been set. Are you sure you initialized SDK-Redux properly?');
}
return this.rpcApiSlice;
}
getSubgraphApiSlice(): SubgraphApiSliceEmpty {
if (!this.subgraphApiSlice) {
throw Error('The SubgraphApiSlice has not been set. Are you sure you initialized SDK-Redux properly?');
}
return this.subgraphApiSlice;
}
getFramework(chainId: number): Promise<Framework> {
const frameworkFactory = this.memoizedFrameworkFactories.get(chainId);
if (!frameworkFactory)
throw Error(
`Don't know how to get Superfluid Framework. :( Please set up a *framework* source for chain [${chainId}].`
);
return frameworkFactory();
}
getTransactionTrackerSlice(): TransactionTrackerSlice {
if (!this.transactionTrackerSlice) {
throw Error(
'The TransactionTrackerSlice has not been set. Are you sure you initialized SDK-Redux properly?'
);
}
return this.transactionTrackerSlice;
}
setRpcApiSlice(slice: RpcApiSliceEmpty): void {
if (this.rpcApiSlice) {
console.log(
"Warning! RpcApiSlice was already set and will be overriden. This shouldn't be happening in production."
);
}
this.rpcApiSlice = slice;
}
setSubgraphApiSlice(slice: SubgraphApiSliceEmpty): void {
if (this.subgraphApiSlice) {
console.log(
"Warning! SubgraphApiSlice was already set and will be overriden. This shouldn't be happening in production."
);
}
this.subgraphApiSlice = slice;
}
setFramework(chainId: number, instanceOrFactory: Framework | (() => Promise<Framework>)) {
const frameworkFactory = isFramework(instanceOrFactory)
? () => Promise.resolve(instanceOrFactory)
: instanceOrFactory;
this.memoizedFrameworkFactories.set(chainId, memoize(frameworkFactory));
}
setTransactionTrackerSlice(slice: TransactionTrackerSlice): void {
if (this.transactionTrackerSlice) {
console.log(
"Warning! TransactionTrackerSlice was already set and will be overriden. This shouldn't be happening in production."
);
}
this.transactionTrackerSlice = slice;
}
}
export const getConfig = SdkReduxConfig.getOrCreateSingleton;
export const getRpcApiSlice = () => getConfig().getRpcApiSlice();
export const getSubgraphApiSlice = () => getConfig().getSubgraphApiSlice();
export const getTransactionTrackerSlice = () => getConfig().getTransactionTrackerSlice();
export const getFramework = (chainId: number) => getConfig().getFramework(chainId);
export const getSubgraphClient = (chainId: number) =>
getConfig()
.getFramework(chainId)
.then((x) => x.query.subgraphClient);
const isFramework = (value: any): value is Framework => !!value.cfaV1;