-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.types.ts
156 lines (140 loc) · 5.13 KB
/
shared.types.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
ContractInterfaceFunctionAccess,
ContractInterfaceFunctionArg,
ContractInterfaceFunctionOutput,
} from "@hirosystems/clarinet-sdk-wasm";
import {
boolCV,
bufferCV,
ClarityValue,
intCV,
listCV,
optionalCVOf,
principalCV,
responseErrorCV,
responseOkCV,
stringAsciiCV,
stringUtf8CV,
tupleCV,
uintCV,
} from "@stacks/transactions";
import fc from "fast-check";
import { ImplementedTraitType, ImportedTraitType } from "./traits.types";
// Types used for Clarity Value conversion.
type ImportedTraitReferenceFunctionArg = {
type: {
trait_reference: ImportedTraitType;
};
name: string;
};
/**
* The type of the function interface, after the contract interface is
* "enriched" with additional information about trait references.
*/
export type EnrichedContractInterfaceFunction = {
args: (ContractInterfaceFunctionArg | ImportedTraitReferenceFunctionArg)[];
name: string;
access: ContractInterfaceFunctionAccess;
outputs: ContractInterfaceFunctionOutput;
};
export type ResponseStatus = "ok" | "error";
export type TupleData<T extends ClarityValue = ClarityValue> = {
[key: string]: T;
};
export type BaseTypesToCV = {
int128: (arg: number) => ReturnType<typeof intCV>;
uint128: (arg: number) => ReturnType<typeof uintCV>;
bool: (arg: boolean) => ReturnType<typeof boolCV>;
principal: (arg: string) => ReturnType<typeof principalCV>;
};
export type ComplexTypesToCV = {
buffer: (arg: string) => ReturnType<typeof bufferCV>;
"string-ascii": (arg: string) => ReturnType<typeof stringAsciiCV>;
"string-utf8": (arg: string) => ReturnType<typeof stringUtf8CV>;
list: (type: ClarityValue[]) => ReturnType<typeof listCV>;
tuple: (tupleData: TupleData) => ReturnType<typeof tupleCV>;
optional: (arg: ClarityValue | null) => ReturnType<typeof optionalCVOf>;
response: (
status: ResponseStatus,
value: ClarityValue
) => ReturnType<typeof responseOkCV | typeof responseErrorCV>;
trait_reference: (trait: string) => ReturnType<typeof principalCV>;
};
// Types used for argument generation.
/** The base Clarity parameter types, as found in the contract interface. */
export type BaseType =
| "int128"
| "uint128"
| "bool"
| "principal"
| "trait_reference";
/**
* The base Clarity parameter types, after the contract interface is "enriched"
* with additional information about trait references. The "trait_reference" is
* no longer a base type after "enrichment".
*/
export type EnrichedBaseType = "int128" | "uint128" | "bool" | "principal";
/** The complex Clarity parameter types, as found in the contract interface. */
type ComplexType =
| { buffer: { length: number } }
| { "string-ascii": { length: number } }
| { "string-utf8": { length: number } }
| { list: { type: ParameterType; length: number } }
| { tuple: { name: string; type: ParameterType }[] }
| { optional: ParameterType }
| { response: { ok: ParameterType; error: ParameterType } };
/**
* The complex Clarity parameter types, after the contract interface is
* "enriched" with additional information about trait references. The
* "trait_reference" is a complex type after enrichment.
*/
type EnrichedComplexType =
| { buffer: { length: number } }
| { "string-ascii": { length: number } }
| { "string-utf8": { length: number } }
| { list: { type: EnrichedParameterType; length: number } }
| { tuple: { name: string; type: EnrichedParameterType }[] }
| { optional: EnrichedParameterType }
| { response: { ok: EnrichedParameterType; error: EnrichedParameterType } }
| { trait_reference: ImportedTraitType };
/** The Clarity parameter types as found in the contract interface. */
export type ParameterType = BaseType | ComplexType;
/** The Clarity parameter types after the contract interface is "enriched". */
export type EnrichedParameterType = EnrichedBaseType | EnrichedComplexType;
export type BaseTypesToArbitrary = {
int128: ReturnType<typeof fc.integer>;
uint128: ReturnType<typeof fc.nat>;
bool: ReturnType<typeof fc.boolean>;
principal: (addresses: string[]) => ReturnType<typeof fc.constantFrom>;
};
export type ComplexTypesToArbitrary = {
buffer: (length: number) => fc.Arbitrary<string>;
"string-ascii": (length: number) => fc.Arbitrary<string>;
"string-utf8": (length: number) => fc.Arbitrary<string>;
list: (
type: EnrichedParameterType,
length: number,
addresses: string[],
projectTraitImplementations: Record<string, ImplementedTraitType[]>
) => fc.Arbitrary<any[]>;
tuple: (
items: { name: string; type: EnrichedParameterType }[],
addresses: string[],
projectTraitImplementations: Record<string, ImplementedTraitType[]>
) => fc.Arbitrary<object>;
optional: (
type: EnrichedParameterType,
addresses: string[],
projectTraitImplementations: Record<string, ImplementedTraitType[]>
) => fc.Arbitrary<any>;
response: (
okType: EnrichedParameterType,
errType: EnrichedParameterType,
addresses: string[],
projectTraitImplementations: Record<string, ImplementedTraitType[]>
) => fc.Arbitrary<any>;
trait_reference: (
traitData: ImportedTraitType,
projectTraitImplementations: Record<string, ImplementedTraitType[]>
) => fc.Arbitrary<any>;
};