-
Notifications
You must be signed in to change notification settings - Fork 110
/
viewFactory.ts
48 lines (45 loc) · 1.57 KB
/
viewFactory.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
import { RpcClientInterface } from '@taquito/rpc';
import { ContractAbstraction, ContractProvider, Wallet, TzReadProvider } from '@taquito/taquito';
import { ViewImplementation, ViewImplementationType } from '../metadata-interface';
import { MichelsonStorageView } from './michelson-storage-view';
export class ViewFactory {
getView(
viewName: string,
rpc: RpcClientInterface,
readProvider: TzReadProvider,
contract: ContractAbstraction<ContractProvider | Wallet>,
viewImplementation: ViewImplementation
) {
if (this.isMichelsonStorageView(viewImplementation)) {
const viewValues = viewImplementation[ViewImplementationType.MICHELSON_STORAGE];
if (!viewValues.returnType || !viewValues.code) {
console.warn(
`${viewName} is missing mandatory code or returnType property therefore it will be skipped.`
);
return;
}
return () => {
const view = new MichelsonStorageView(
viewName,
contract,
rpc,
readProvider,
viewValues.returnType as any,
viewValues.code as any,
viewValues.parameter as any
);
return view;
};
}
}
getImplementationType(viewImplementation: ViewImplementation) {
return Object.keys(viewImplementation)[0];
}
private isMichelsonStorageView(
viewImplementation: ViewImplementation
): viewImplementation is { [ViewImplementationType.MICHELSON_STORAGE]: any } {
return (
this.getImplementationType(viewImplementation) === ViewImplementationType.MICHELSON_STORAGE
);
}
}