-
Notifications
You must be signed in to change notification settings - Fork 110
/
parameter.ts
137 lines (126 loc) · 4.18 KB
/
parameter.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
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
129
130
131
132
133
134
135
136
137
import { createToken } from '../tokens/createToken';
import { Token, Semantic, TokenValidationError, SemanticEncoding } from '../tokens/token';
import { OrToken } from '../tokens/or';
import { OptionToken } from '../tokens/option';
import { ScriptResponse, MichelsonV1ExpressionExtended, MichelsonV1Expression } from '@taquito/rpc';
import { TokenSchema } from './types';
import { InvalidRpcResponseError, ParameterEncodingError } from './errors';
/**
* @warn Our current smart contract abstraction feature is currently in preview. It's API is not final, and it may not cover every use case (yet). We will greatly appreciate any feedback on this feature.
*/
export class ParameterSchema {
private root: Token;
/**
*
* @description Create an instance of ParameterSchema from a contract script
*
* @param val contract script obtained from the RPC
* @returns ParameterSchema
* @throws {InvalidRpcResponseError} If the RPC response is invalid
*/
static fromRPCResponse(val: { script: ScriptResponse }) {
if (!val) {
throw new InvalidRpcResponseError(val, 'the RPC response is empty');
}
if (!val.script) {
throw new InvalidRpcResponseError(val, 'the RPC response has no script');
}
if (!Array.isArray(val.script.code)) {
throw new InvalidRpcResponseError(val, 'The response.script.code should be an array');
}
const parameter = val.script.code.find(
(x) => 'prim' in x && x.prim === 'parameter'
) as MichelsonV1ExpressionExtended;
if (!parameter) {
throw new InvalidRpcResponseError(
val,
`The response.script.code should have an element of type {prim: "parameter"}`
);
}
if (!Array.isArray(parameter.args)) {
throw new InvalidRpcResponseError(
val,
`The response.script.code has an element of type {prim: "parameter"}, but its args is not an array`
);
}
return new ParameterSchema(parameter.args[0]);
}
/**
* @description Check if the Contract parameter is multiple entry point or not
*/
get isMultipleEntryPoint() {
return (
this.root instanceof OrToken ||
(this.root instanceof OptionToken && this.root.subToken() instanceof OrToken)
);
}
/**
* @description Check if the Contract parameter has an annotation or not
*/
get hasAnnotation() {
if (this.isMultipleEntryPoint) {
return Object.keys(this.ExtractSchema())[0] !== '0';
} else {
return true;
}
}
/**
* @description Return the schema of the parameter of a specific entry point
* @throws {@link InvalidTokenError}
*/
constructor(val: MichelsonV1Expression) {
this.root = createToken(val, 0);
}
/**
* @description Returns the javascript object equivalent of the Micheline value provided
*/
Execute(val: any, semantics?: Semantic) {
return this.root.Execute(val, semantics);
}
/**
* @description Returns a micheline formatted object for the values provided
* @throws {@link TokenValidationError}
* @throws {@link ParameterEncodingError}
*/
Encode(...args: any[]) {
try {
return this.root.Encode(args.reverse());
} catch (ex) {
if (ex instanceof TokenValidationError) {
throw ex;
}
throw new ParameterEncodingError('Unable to encode parameter', this.root, args, ex);
}
}
/**
* @description Returns a micheline formatted object for the javascript object provided
* @throws {@link TokenValidationError}
* @throws {@link ParameterEncodingError}
*/
EncodeObject(value?: any, semantics?: SemanticEncoding) {
try {
return this.root.EncodeObject(value, semantics);
} catch (ex) {
if (ex instanceof TokenValidationError) {
throw ex;
}
throw new ParameterEncodingError('Unable to encode parameter object', this.root, value, ex);
}
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
ExtractSchema() {
return this.root.ExtractSchema();
}
/**
* @description Produce a schema grouping together all the entry points of a contract.
*/
generateSchema(): TokenSchema {
return this.root.generateSchema();
}
ExtractSignatures() {
return this.root.ExtractSignature();
}
}