@algorandfoundation/algokit-client-generator 2.6.0 → 2.6.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.mjs +4 -0
- package/cli.cjs +75 -0
- package/cli.d.ts +0 -1
- package/cli.mjs +54 -0
- package/client/app-types.cjs +152 -0
- package/client/app-types.d.ts +0 -1
- package/client/app-types.mjs +131 -0
- package/client/call-client.cjs +332 -0
- package/client/call-client.d.ts +0 -1
- package/client/{call-client.js → call-client.mjs} +107 -135
- package/client/call-composer-types.cjs +141 -0
- package/client/call-composer-types.d.ts +0 -1
- package/client/{call-composer-types.js → call-composer-types.mjs} +33 -58
- package/client/call-composer.cjs +145 -0
- package/client/call-composer.d.ts +0 -1
- package/client/call-composer.mjs +124 -0
- package/client/call-factory.cjs +138 -0
- package/client/call-factory.d.ts +0 -1
- package/client/{call-factory.js → call-factory.mjs} +31 -56
- package/client/deploy-types.cjs +94 -0
- package/client/deploy-types.d.ts +0 -1
- package/client/deploy-types.mjs +91 -0
- package/client/generate.cjs +37 -0
- package/client/generate.d.ts +0 -1
- package/client/generate.mjs +35 -0
- package/client/generator-context.cjs +37 -0
- package/client/generator-context.d.ts +0 -1
- package/client/generator-context.mjs +16 -0
- package/client/helpers/{get-call-config-summary.js → get-call-config-summary.cjs} +7 -25
- package/client/helpers/get-call-config-summary.d.ts +1 -2
- package/client/helpers/get-call-config-summary.mjs +54 -0
- package/client/helpers/{get-equivalent-type.js → get-equivalent-type.cjs} +20 -20
- package/client/helpers/get-equivalent-type.d.ts +0 -1
- package/client/helpers/get-equivalent-type.mjs +56 -0
- package/client/{imports.js → imports.cjs} +3 -4
- package/client/imports.d.ts +0 -1
- package/client/imports.mjs +27 -0
- package/client/{utility-types.js → utility-types.cjs} +26 -25
- package/client/utility-types.d.ts +0 -1
- package/client/utility-types.mjs +47 -0
- package/dev.d.ts +0 -1
- package/index.cjs +13 -0
- package/index.d.ts +0 -1
- package/index.mjs +3 -0
- package/output/{writer.js → writer.cjs} +44 -38
- package/output/writer.d.ts +0 -1
- package/output/writer.mjs +139 -0
- package/package.json +7 -4
- package/schema/application.schema.json.cjs +374 -0
- package/schema/application.schema.json.mjs +363 -0
- package/schema/contract.schema.json.cjs +159 -0
- package/schema/contract.schema.json.mjs +148 -0
- package/schema/load.cjs +25 -0
- package/schema/load.d.ts +0 -1
- package/schema/load.mjs +22 -0
- package/tests/approval-tests.spec.d.ts +0 -1
- package/tests/util.d.ts +1 -0
- package/util/boom.cjs +7 -0
- package/util/boom.d.ts +0 -1
- package/util/boom.mjs +5 -0
- package/util/color-console.cjs +18 -0
- package/util/color-console.d.ts +0 -1
- package/util/color-console.mjs +16 -0
- package/util/not-falsy.d.ts +0 -1
- package/util/sanitization.cjs +17 -0
- package/util/sanitization.d.ts +0 -1
- package/util/sanitization.mjs +11 -0
- package/bin/cli.js +0 -4
- package/cli.d.ts.map +0 -1
- package/cli.js +0 -79
- package/cli.js.map +0 -1
- package/client/app-types.d.ts.map +0 -1
- package/client/app-types.js +0 -161
- package/client/app-types.js.map +0 -1
- package/client/call-client.d.ts.map +0 -1
- package/client/call-client.js.map +0 -1
- package/client/call-composer-types.d.ts.map +0 -1
- package/client/call-composer-types.js.map +0 -1
- package/client/call-composer.d.ts.map +0 -1
- package/client/call-composer.js +0 -151
- package/client/call-composer.js.map +0 -1
- package/client/call-factory.d.ts.map +0 -1
- package/client/call-factory.js.map +0 -1
- package/client/deploy-types.d.ts.map +0 -1
- package/client/deploy-types.js +0 -95
- package/client/deploy-types.js.map +0 -1
- package/client/generate.d.ts.map +0 -1
- package/client/generate.js +0 -37
- package/client/generate.js.map +0 -1
- package/client/generator-context.d.ts.map +0 -1
- package/client/generator-context.js +0 -41
- package/client/generator-context.js.map +0 -1
- package/client/helpers/get-call-config-summary.d.ts.map +0 -1
- package/client/helpers/get-call-config-summary.js.map +0 -1
- package/client/helpers/get-equivalent-type.d.ts.map +0 -1
- package/client/helpers/get-equivalent-type.js.map +0 -1
- package/client/imports.d.ts.map +0 -1
- package/client/imports.js.map +0 -1
- package/client/utility-types.d.ts.map +0 -1
- package/client/utility-types.js.map +0 -1
- package/dev.d.ts.map +0 -1
- package/dev.js +0 -5
- package/dev.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js +0 -12
- package/index.js.map +0 -1
- package/output/writer.d.ts.map +0 -1
- package/output/writer.js.map +0 -1
- package/schema/application.schema.json +0 -354
- package/schema/contract.schema.json +0 -139
- package/schema/load.d.ts.map +0 -1
- package/schema/load.js +0 -51
- package/schema/load.js.map +0 -1
- package/tests/approval-tests.spec.d.ts.map +0 -1
- package/tests/approval-tests.spec.js +0 -24
- package/tests/approval-tests.spec.js.map +0 -1
- package/util/boom.d.ts.map +0 -1
- package/util/boom.js +0 -8
- package/util/boom.js.map +0 -1
- package/util/color-console.d.ts.map +0 -1
- package/util/color-console.js +0 -20
- package/util/color-console.js.map +0 -1
- package/util/not-falsy.d.ts.map +0 -1
- package/util/not-falsy.js +0 -6
- package/util/not-falsy.js.map +0 -1
- package/util/sanitization.d.ts.map +0 -1
- package/util/sanitization.js +0 -17
- package/util/sanitization.js.map +0 -1
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var algokit = require('@algorandfoundation/algokit-utils');
|
|
4
|
+
var writer = require('../output/writer.cjs');
|
|
5
|
+
var sanitization = require('../util/sanitization.cjs');
|
|
6
|
+
var getCallConfigSummary = require('./helpers/get-call-config-summary.cjs');
|
|
7
|
+
var deployTypes = require('./deploy-types.cjs');
|
|
8
|
+
var callComposer = require('./call-composer.cjs');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var algokit__namespace = /*#__PURE__*/_interopNamespaceDefault(algokit);
|
|
28
|
+
|
|
29
|
+
function* callClient(ctx) {
|
|
30
|
+
const { app, name } = ctx;
|
|
31
|
+
yield* writer.jsDoc(`A client to make calls to the ${app.contract.name} smart contract`);
|
|
32
|
+
yield `export class ${sanitization.makeSafeTypeIdentifier(app.contract.name)}Client {`;
|
|
33
|
+
yield writer.IncIndent;
|
|
34
|
+
yield* writer.jsDoc(`The underlying \`ApplicationClient\` for when you want to have more flexibility`);
|
|
35
|
+
yield 'public readonly appClient: ApplicationClient';
|
|
36
|
+
yield writer.NewLine;
|
|
37
|
+
yield `private readonly sender: SendTransactionFrom | undefined`;
|
|
38
|
+
yield writer.NewLine;
|
|
39
|
+
yield* writer.jsDoc({
|
|
40
|
+
description: `Creates a new instance of \`${sanitization.makeSafeTypeIdentifier(app.contract.name)}Client\``,
|
|
41
|
+
params: {
|
|
42
|
+
appDetails: 'appDetails The details to identify the app to deploy',
|
|
43
|
+
algod: 'An algod client instance',
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
yield `constructor(appDetails: AppDetails, private algod: Algodv2) {`;
|
|
47
|
+
yield writer.IncIndent;
|
|
48
|
+
yield `this.sender = appDetails.sender`;
|
|
49
|
+
yield 'this.appClient = algokit.getAppClient({';
|
|
50
|
+
yield* writer.indent('...appDetails,', 'app: APP_SPEC');
|
|
51
|
+
yield '}, algod)';
|
|
52
|
+
yield writer.DecIndent;
|
|
53
|
+
yield '}';
|
|
54
|
+
yield writer.NewLine;
|
|
55
|
+
yield* writer.jsDoc({
|
|
56
|
+
description: 'Checks for decode errors on the AppCallTransactionResult and maps the return value to the specified generic type',
|
|
57
|
+
params: {
|
|
58
|
+
result: 'The AppCallTransactionResult to be mapped',
|
|
59
|
+
returnValueFormatter: 'An optional delegate to format the return value if required',
|
|
60
|
+
},
|
|
61
|
+
returns: 'The smart contract response with an updated return value',
|
|
62
|
+
});
|
|
63
|
+
yield* writer.inline(`protected mapReturnValue<TReturn, TResult extends AppCallTransactionResult = AppCallTransactionResult>`, `(result: AppCallTransactionResult, returnValueFormatter?: (value: any) => TReturn): `, `AppCallTransactionResultOfType<TReturn> & TResult {`);
|
|
64
|
+
yield writer.IncIndent;
|
|
65
|
+
yield `if(result.return?.decodeError) {`;
|
|
66
|
+
yield* writer.indent(`throw result.return.decodeError`);
|
|
67
|
+
yield `}`;
|
|
68
|
+
yield `const returnValue = result.return?.returnValue !== undefined && returnValueFormatter !== undefined`;
|
|
69
|
+
yield writer.IncIndent;
|
|
70
|
+
yield `? returnValueFormatter(result.return.returnValue)`;
|
|
71
|
+
yield `: result.return?.returnValue as TReturn | undefined`;
|
|
72
|
+
yield `return { ...result, return: returnValue } as AppCallTransactionResultOfType<TReturn> & TResult`;
|
|
73
|
+
yield writer.DecIndent;
|
|
74
|
+
yield writer.DecIndentAndCloseBlock;
|
|
75
|
+
yield writer.NewLine;
|
|
76
|
+
yield* writer.jsDoc({
|
|
77
|
+
description: 'Calls the ABI method with the matching signature using an onCompletion code of NO_OP',
|
|
78
|
+
params: {
|
|
79
|
+
typedCallParams: 'An object containing the method signature, args, and any other relevant parameters',
|
|
80
|
+
returnValueFormatter: 'An optional delegate which when provided will be used to map non-undefined return values to the target type',
|
|
81
|
+
},
|
|
82
|
+
returns: 'The result of the smart contract call',
|
|
83
|
+
});
|
|
84
|
+
yield `public async call<TSignature extends keyof ${name}['methods']>(typedCallParams: TypedCallParams<TSignature>, returnValueFormatter?: (value: any) => MethodReturn<TSignature>) {`;
|
|
85
|
+
yield writer.IncIndent;
|
|
86
|
+
yield `return this.mapReturnValue<MethodReturn<TSignature>>(await this.appClient.call(typedCallParams), returnValueFormatter)`;
|
|
87
|
+
yield writer.DecIndentAndCloseBlock;
|
|
88
|
+
yield writer.NewLine;
|
|
89
|
+
yield* opMethods(ctx);
|
|
90
|
+
yield* clearState(ctx);
|
|
91
|
+
yield* noopMethods(ctx);
|
|
92
|
+
yield* getStateMethods(ctx);
|
|
93
|
+
yield* callComposer.composeMethod(ctx);
|
|
94
|
+
yield writer.DecIndentAndCloseBlock;
|
|
95
|
+
}
|
|
96
|
+
function* opMethods(ctx) {
|
|
97
|
+
const { app, callConfig, name } = ctx;
|
|
98
|
+
yield* writer.jsDoc({
|
|
99
|
+
description: `Idempotently deploys the ${app.contract.name} smart contract.`,
|
|
100
|
+
params: {
|
|
101
|
+
params: 'The arguments for the contract calls and any additional parameters for the call',
|
|
102
|
+
},
|
|
103
|
+
returns: 'The deployment result',
|
|
104
|
+
});
|
|
105
|
+
yield `public deploy(params: ${name}DeployArgs & AppClientDeployCoreParams = {}): ReturnType<ApplicationClient['deploy']> {`;
|
|
106
|
+
yield writer.IncIndent;
|
|
107
|
+
if (callConfig.createMethods.length)
|
|
108
|
+
yield `const createArgs = params.createCall?.(${name}CallFactory.create)`;
|
|
109
|
+
if (callConfig.updateMethods.length)
|
|
110
|
+
yield `const updateArgs = params.updateCall?.(${name}CallFactory.update)`;
|
|
111
|
+
if (callConfig.deleteMethods.length)
|
|
112
|
+
yield `const deleteArgs = params.deleteCall?.(${name}CallFactory.delete)`;
|
|
113
|
+
yield `return this.appClient.deploy({`;
|
|
114
|
+
yield writer.IncIndent;
|
|
115
|
+
yield `...params,`;
|
|
116
|
+
if (callConfig.updateMethods.length)
|
|
117
|
+
yield 'updateArgs,';
|
|
118
|
+
if (callConfig.deleteMethods.length)
|
|
119
|
+
yield 'deleteArgs,';
|
|
120
|
+
if (callConfig.createMethods.length) {
|
|
121
|
+
yield 'createArgs,';
|
|
122
|
+
yield `createOnCompleteAction: createArgs?.onCompleteAction,`;
|
|
123
|
+
}
|
|
124
|
+
yield writer.DecIndent;
|
|
125
|
+
yield `})`;
|
|
126
|
+
yield writer.DecIndentAndCloseBlock;
|
|
127
|
+
yield writer.NewLine;
|
|
128
|
+
yield* operationMethod(ctx, `Creates a new instance of the ${app.contract.name} smart contract`, callConfig.createMethods, 'create', true);
|
|
129
|
+
yield* operationMethod(ctx, `Updates an existing instance of the ${app.contract.name} smart contract`, callConfig.updateMethods, 'update', true);
|
|
130
|
+
yield* operationMethod(ctx, `Deletes an existing instance of the ${app.contract.name} smart contract`, callConfig.deleteMethods, 'delete');
|
|
131
|
+
yield* operationMethod(ctx, `Opts the user into an existing instance of the ${app.contract.name} smart contract`, callConfig.optInMethods, 'optIn');
|
|
132
|
+
yield* operationMethod(ctx, `Makes a close out call to an existing instance of the ${app.contract.name} smart contract`, callConfig.closeOutMethods, 'closeOut');
|
|
133
|
+
}
|
|
134
|
+
function* operationMethod({ app, methodSignatureToUniqueName, name }, description, methods, verb, includeCompilation) {
|
|
135
|
+
let responseTypeGenericParam;
|
|
136
|
+
switch (verb) {
|
|
137
|
+
case 'create':
|
|
138
|
+
responseTypeGenericParam = ', AppCreateCallTransactionResult';
|
|
139
|
+
break;
|
|
140
|
+
case 'update':
|
|
141
|
+
responseTypeGenericParam = ', AppUpdateCallTransactionResult';
|
|
142
|
+
break;
|
|
143
|
+
default:
|
|
144
|
+
responseTypeGenericParam = '';
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (methods.length) {
|
|
148
|
+
yield* writer.jsDoc(`Gets available ${verb} methods`);
|
|
149
|
+
yield `public get ${verb}() {`;
|
|
150
|
+
yield writer.IncIndent;
|
|
151
|
+
yield `const $this = this`;
|
|
152
|
+
yield `return {`;
|
|
153
|
+
yield writer.IncIndent;
|
|
154
|
+
for (const methodSig of methods) {
|
|
155
|
+
const onComplete = verb === 'create' ? deployTypes.getCreateOnCompleteOptions(methodSig, app) : undefined;
|
|
156
|
+
if (methodSig === getCallConfigSummary.BARE_CALL) {
|
|
157
|
+
yield* writer.jsDoc({
|
|
158
|
+
description: `${description} using a bare call.`,
|
|
159
|
+
params: {
|
|
160
|
+
args: `The arguments for the bare call`,
|
|
161
|
+
},
|
|
162
|
+
returns: `The ${verb} result`,
|
|
163
|
+
});
|
|
164
|
+
yield `async bare(args: BareCallArgs & AppClientCallCoreParams ${includeCompilation ? '& AppClientCompilationParams ' : ''}& CoreAppCallArgs${onComplete?.type ? ` & ${onComplete.type}` : ''}${onComplete?.isOptional !== false ? ' = {}' : ''}) {`;
|
|
165
|
+
yield* writer.indent(`return $this.mapReturnValue<undefined${responseTypeGenericParam}>(await $this.appClient.${verb}(args))`);
|
|
166
|
+
yield '},';
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const uniqueName = methodSignatureToUniqueName[methodSig];
|
|
170
|
+
const method = app.contract.methods.find((m) => algokit__namespace.getABIMethodSignature(m) === methodSig);
|
|
171
|
+
yield* writer.jsDoc({
|
|
172
|
+
description: `${description} using the ${methodSig} ABI method.`,
|
|
173
|
+
params: {
|
|
174
|
+
args: `The arguments for the smart contract call`,
|
|
175
|
+
params: `Any additional parameters for the call`,
|
|
176
|
+
},
|
|
177
|
+
returns: `The ${verb} result${method?.returns?.desc ? `: ${method.returns.desc}` : ''}`,
|
|
178
|
+
});
|
|
179
|
+
yield `async ${sanitization.makeSafeMethodIdentifier(uniqueName)}(args: MethodArgs<'${methodSig}'>, params: AppClientCallCoreParams${includeCompilation ? ' & AppClientCompilationParams' : ''}${onComplete?.type ? ` & ${onComplete.type}` : ''}${onComplete?.isOptional !== false ? ' = {}' : ''}) {`;
|
|
180
|
+
yield* writer.indent(`return $this.mapReturnValue<MethodReturn<'${methodSig}'>${responseTypeGenericParam}>(await $this.appClient.${verb}(${name}CallFactory.${verb}.${sanitization.makeSafeMethodIdentifier(uniqueName)}(args, params)))`);
|
|
181
|
+
yield '},';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
yield writer.DecIndentAndCloseBlock;
|
|
185
|
+
yield writer.DecIndentAndCloseBlock;
|
|
186
|
+
yield writer.NewLine;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function* clearState({ app }) {
|
|
190
|
+
yield* writer.jsDoc({
|
|
191
|
+
description: `Makes a clear_state call to an existing instance of the ${app.contract.name} smart contract.`,
|
|
192
|
+
params: {
|
|
193
|
+
args: `The arguments for the bare call`,
|
|
194
|
+
},
|
|
195
|
+
returns: `The clear_state result`,
|
|
196
|
+
});
|
|
197
|
+
yield `public clearState(args: BareCallArgs & AppClientCallCoreParams & CoreAppCallArgs = {}) {`;
|
|
198
|
+
yield writer.IncIndent;
|
|
199
|
+
yield `return this.appClient.clearState(args)`;
|
|
200
|
+
yield writer.DecIndentAndCloseBlock;
|
|
201
|
+
yield writer.NewLine;
|
|
202
|
+
}
|
|
203
|
+
function* noopMethods({ app, name, callConfig, methodSignatureToUniqueName }) {
|
|
204
|
+
for (const method of app.contract.methods) {
|
|
205
|
+
const methodSignature = algokit__namespace.getABIMethodSignature(method);
|
|
206
|
+
const methodName = sanitization.makeSafeMethodIdentifier(methodSignatureToUniqueName[methodSignature]);
|
|
207
|
+
// Skip methods which don't support a no_op call config
|
|
208
|
+
if (!callConfig.callMethods.includes(methodSignature))
|
|
209
|
+
continue;
|
|
210
|
+
yield* writer.jsDoc({
|
|
211
|
+
description: `Calls the ${algokit__namespace.getABIMethodSignature(method)} ABI method.`,
|
|
212
|
+
abiDescription: method.desc,
|
|
213
|
+
params: {
|
|
214
|
+
args: `The arguments for the contract call`,
|
|
215
|
+
params: `Any additional parameters for the call`,
|
|
216
|
+
},
|
|
217
|
+
returns: `The result of the call${method?.returns?.desc ? `: ${method.returns.desc}` : ''}`,
|
|
218
|
+
});
|
|
219
|
+
yield `public ${methodName}(args: MethodArgs<'${methodSignature}'>, params: AppClientCallCoreParams & CoreAppCallArgs = {}) {`;
|
|
220
|
+
yield writer.IncIndent;
|
|
221
|
+
const outputTypeName = app.hints?.[methodSignature]?.structs?.output?.name;
|
|
222
|
+
yield `return this.call(${name}CallFactory.${methodName}(args, params)${outputTypeName === undefined ? '' : `, ${sanitization.makeSafeTypeIdentifier(outputTypeName)}`})`;
|
|
223
|
+
yield writer.DecIndent;
|
|
224
|
+
yield '}';
|
|
225
|
+
yield writer.NewLine;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function* getStateMethods({ app, name }) {
|
|
229
|
+
const globalStateValues = app.schema.global?.declared && Object.values(app.schema.global?.declared);
|
|
230
|
+
const localStateValues = app.schema.local?.declared && Object.values(app.schema.local?.declared);
|
|
231
|
+
if (globalStateValues?.length || localStateValues?.length) {
|
|
232
|
+
yield* writer.jsDoc({
|
|
233
|
+
description: 'Extracts a binary state value out of an AppState dictionary',
|
|
234
|
+
params: {
|
|
235
|
+
state: 'The state dictionary containing the state value',
|
|
236
|
+
key: 'The key of the state value',
|
|
237
|
+
},
|
|
238
|
+
returns: 'A BinaryState instance containing the state value, or undefined if the key was not found',
|
|
239
|
+
});
|
|
240
|
+
yield `private static getBinaryState(state: AppState, key: string): BinaryState | undefined {`;
|
|
241
|
+
yield writer.IncIndent;
|
|
242
|
+
yield `const value = state[key]`;
|
|
243
|
+
yield `if (!value) return undefined`;
|
|
244
|
+
yield `if (!('valueRaw' in value))`;
|
|
245
|
+
yield* writer.indent(`throw new Error(\`Failed to parse state value for \${key}; received an int when expected a byte array\`)`);
|
|
246
|
+
yield `return {`;
|
|
247
|
+
yield writer.IncIndent;
|
|
248
|
+
yield `asString(): string {`;
|
|
249
|
+
yield* writer.indent(`return value.value`);
|
|
250
|
+
yield `},`;
|
|
251
|
+
yield `asByteArray(): Uint8Array {`;
|
|
252
|
+
yield* writer.indent(`return value.valueRaw`);
|
|
253
|
+
yield `}`;
|
|
254
|
+
yield writer.DecIndentAndCloseBlock;
|
|
255
|
+
yield writer.DecIndentAndCloseBlock;
|
|
256
|
+
yield writer.NewLine;
|
|
257
|
+
yield* writer.jsDoc({
|
|
258
|
+
description: 'Extracts a integer state value out of an AppState dictionary',
|
|
259
|
+
params: {
|
|
260
|
+
state: 'The state dictionary containing the state value',
|
|
261
|
+
key: 'The key of the state value',
|
|
262
|
+
},
|
|
263
|
+
returns: 'An IntegerState instance containing the state value, or undefined if the key was not found',
|
|
264
|
+
});
|
|
265
|
+
yield `private static getIntegerState(state: AppState, key: string): IntegerState | undefined {`;
|
|
266
|
+
yield writer.IncIndent;
|
|
267
|
+
yield `const value = state[key]`;
|
|
268
|
+
yield `if (!value) return undefined`;
|
|
269
|
+
yield `if ('valueRaw' in value)`;
|
|
270
|
+
yield* writer.indent(`throw new Error(\`Failed to parse state value for \${key}; received a byte array when expected a number\`)`);
|
|
271
|
+
yield `return {`;
|
|
272
|
+
yield writer.IncIndent;
|
|
273
|
+
yield `asBigInt() {`;
|
|
274
|
+
yield* writer.indent(`return typeof value.value === 'bigint' ? value.value : BigInt(value.value)`);
|
|
275
|
+
yield `},`;
|
|
276
|
+
yield `asNumber(): number {`;
|
|
277
|
+
yield* writer.indent(`return typeof value.value === 'bigint' ? Number(value.value) : value.value`);
|
|
278
|
+
yield `},`;
|
|
279
|
+
yield writer.DecIndentAndCloseBlock;
|
|
280
|
+
yield writer.DecIndentAndCloseBlock;
|
|
281
|
+
yield writer.NewLine;
|
|
282
|
+
}
|
|
283
|
+
if (globalStateValues?.length) {
|
|
284
|
+
yield* writer.jsDoc(`Returns the smart contract's global state wrapped in a strongly typed accessor with options to format the stored value`);
|
|
285
|
+
yield `public async getGlobalState(): Promise<${name}['state']['global']> {`;
|
|
286
|
+
yield writer.IncIndent;
|
|
287
|
+
yield `const state = await this.appClient.getGlobalState()`;
|
|
288
|
+
yield `return {`;
|
|
289
|
+
yield writer.IncIndent;
|
|
290
|
+
for (const stateValue of globalStateValues) {
|
|
291
|
+
yield `get ${stateValue.key}() {`;
|
|
292
|
+
if (stateValue.type === 'uint64') {
|
|
293
|
+
yield* writer.indent(`return ${name}Client.getIntegerState(state, '${stateValue.key}')`);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
yield* writer.indent(`return ${name}Client.getBinaryState(state, '${stateValue.key}')`);
|
|
297
|
+
}
|
|
298
|
+
yield '},';
|
|
299
|
+
}
|
|
300
|
+
yield writer.DecIndentAndCloseBlock;
|
|
301
|
+
yield writer.DecIndentAndCloseBlock;
|
|
302
|
+
yield writer.NewLine;
|
|
303
|
+
}
|
|
304
|
+
if (localStateValues?.length) {
|
|
305
|
+
yield* writer.jsDoc({
|
|
306
|
+
description: `Returns the smart contract's local state wrapped in a strongly typed accessor with options to format the stored value`,
|
|
307
|
+
params: {
|
|
308
|
+
account: `The address of the account for which to read local state from`,
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
yield `public async getLocalState(account: string | SendTransactionFrom): Promise<${name}['state']['local']> {`;
|
|
312
|
+
yield writer.IncIndent;
|
|
313
|
+
yield `const state = await this.appClient.getLocalState(account)`;
|
|
314
|
+
yield `return {`;
|
|
315
|
+
yield writer.IncIndent;
|
|
316
|
+
for (const stateValue of localStateValues) {
|
|
317
|
+
yield `get ${stateValue.key}() {`;
|
|
318
|
+
if (stateValue.type === 'uint64') {
|
|
319
|
+
yield* writer.indent(`return ${name}Client.getIntegerState(state, '${stateValue.key}')`);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
yield* writer.indent(`return ${name}Client.getBinaryState(state, '${stateValue.key}')`);
|
|
323
|
+
}
|
|
324
|
+
yield '},';
|
|
325
|
+
}
|
|
326
|
+
yield writer.DecIndentAndCloseBlock;
|
|
327
|
+
yield writer.DecIndentAndCloseBlock;
|
|
328
|
+
yield writer.NewLine;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
exports.callClient = callClient;
|
package/client/call-client.d.ts
CHANGED