-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlledAddressClient.ts
402 lines (384 loc) · 16.9 KB
/
ControlledAddressClient.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* eslint-disable */
/**
* This file was automatically generated by @algorandfoundation/algokit-client-generator.
* DO NOT MODIFY IT BY HAND.
* requires: @algorandfoundation/algokit-utils: ^2
*/
import * as algokit from '@algorandfoundation/algokit-utils'
import type {
ABIAppCallArg,
AppCallTransactionResult,
AppCallTransactionResultOfType,
AppCompilationResult,
AppReference,
AppState,
CoreAppCallArgs,
RawAppCallArgs,
TealTemplateParams,
} from '@algorandfoundation/algokit-utils/types/app'
import type {
AppClientCallCoreParams,
AppClientCompilationParams,
AppClientDeployCoreParams,
AppDetails,
ApplicationClient,
} from '@algorandfoundation/algokit-utils/types/app-client'
import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
import type { SendTransactionResult, TransactionToSign, SendTransactionFrom, SendTransactionParams } from '@algorandfoundation/algokit-utils/types/transaction'
import type { ABIResult, TransactionWithSigner } from 'algosdk'
import { Algodv2, OnApplicationComplete, Transaction, AtomicTransactionComposer, modelsv2 } from 'algosdk'
export const APP_SPEC: AppSpec = {
"hints": {
"new()address": {
"call_config": {
"delete_application": "CREATE"
}
}
},
"bare_call_config": {
"no_op": "NEVER",
"opt_in": "NEVER",
"close_out": "NEVER",
"update_application": "NEVER",
"delete_application": "NEVER"
},
"schema": {
"local": {
"declared": {},
"reserved": {}
},
"global": {
"declared": {},
"reserved": {}
}
},
"state": {
"global": {
"num_byte_slices": 0,
"num_uints": 0
},
"local": {
"num_byte_slices": 0,
"num_uints": 0
}
},
"source": {
"approval": "I3ByYWdtYSB2ZXJzaW9uIDEwCgovLyBUaGlzIFRFQUwgd2FzIGdlbmVyYXRlZCBieSBURUFMU2NyaXB0IHYwLjkwLjMKLy8gaHR0cHM6Ly9naXRodWIuY29tL2FsZ29yYW5kZm91bmRhdGlvbi9URUFMU2NyaXB0CgovLyBUaGlzIGNvbnRyYWN0IGlzIGNvbXBsaWFudCB3aXRoIGFuZC9vciBpbXBsZW1lbnRzIHRoZSBmb2xsb3dpbmcgQVJDczogWyBBUkM0IF0KCi8vIFRoZSBmb2xsb3dpbmcgdGVuIGxpbmVzIG9mIFRFQUwgaGFuZGxlIGluaXRpYWwgcHJvZ3JhbSBmbG93Ci8vIFRoaXMgcGF0dGVybiBpcyB1c2VkIHRvIG1ha2UgaXQgZWFzeSBmb3IgYW55b25l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cKCWNvbmNhdAoJbG9nCglpbnQgMQoJcmV0dXJuCgovLyBuZXcoKTogQWRkcmVzcwpuZXc6Cglwcm90byAwIDEKCgkvLyBjb250cmFjdHMvYXJjNTkuYWxnby50czoyMwoJLy8gc2VuZFBheW1lbnQoewoJLy8gICAgICAgcmVrZXlUbzogdGhpcy50eG4uc2VuZGVyLAoJLy8gICAgIH0pCglpdHhuX2JlZ2luCglpbnQgcGF5CglpdHhuX2ZpZWxkIFR5cGVFbnVtCgoJLy8gY29udHJhY3RzL2FyYzU5LmFsZ28udHM6MjQKCS8vIHJla2V5VG86IHRoaXMudHhuLnNlbmRlcgoJdHhuIFNlbmRlcgoJaXR4bl9maWVsZCBSZWtleVRvCgoJLy8gRmVlIGZpZWxkIG5vdCBzZXQsIGRlZmF1bHRpbmcgdG8gMAoJaW50IDAKCWl0eG5fZmllbGQgRmVlCgoJLy8gU3VibWl0IGlubmVyIHRyYW5zYWN0aW9uCglpdHhuX3N1Ym1pdAoKCS8vIGNvbnRyYWN0cy9hcmM1OS5hbGdvLnRzOjI3CgkvLyByZXR1cm4gdGhpcy5hcHAuYWRkcmVzczsKCWdsb2JhbCBDdXJyZW50QXBwbGljYXRpb25BZGRyZXNzCglyZXRzdWIKCipjcmVhdGVfRGVsZXRlQXBwbGljYXRpb246CgltZXRob2QgIm5ldygpYWRkcmVzcyIKCXR4bmEgQXBwbGljYXRpb25BcmdzIDAKCW1hdGNoICphYmlfcm91dGVfbmV3CgllcnI=",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDEw"
},
"contract": {
"name": "ControlledAddress",
"desc": "",
"methods": [
{
"name": "new",
"args": [],
"returns": {
"type": "address"
}
}
]
}
}
/**
* Defines an onCompletionAction of 'no_op'
*/
export type OnCompleteNoOp = { onCompleteAction?: 'no_op' | OnApplicationComplete.NoOpOC }
/**
* Defines an onCompletionAction of 'opt_in'
*/
export type OnCompleteOptIn = { onCompleteAction: 'opt_in' | OnApplicationComplete.OptInOC }
/**
* Defines an onCompletionAction of 'close_out'
*/
export type OnCompleteCloseOut = { onCompleteAction: 'close_out' | OnApplicationComplete.CloseOutOC }
/**
* Defines an onCompletionAction of 'delete_application'
*/
export type OnCompleteDelApp = { onCompleteAction: 'delete_application' | OnApplicationComplete.DeleteApplicationOC }
/**
* Defines an onCompletionAction of 'update_application'
*/
export type OnCompleteUpdApp = { onCompleteAction: 'update_application' | OnApplicationComplete.UpdateApplicationOC }
/**
* A state record containing a single unsigned integer
*/
export type IntegerState = {
/**
* Gets the state value as a BigInt.
*/
asBigInt(): bigint
/**
* Gets the state value as a number.
*/
asNumber(): number
}
/**
* A state record containing binary data
*/
export type BinaryState = {
/**
* Gets the state value as a Uint8Array
*/
asByteArray(): Uint8Array
/**
* Gets the state value as a string
*/
asString(): string
}
export type AppCreateCallTransactionResult = AppCallTransactionResult & Partial<AppCompilationResult> & AppReference
export type AppUpdateCallTransactionResult = AppCallTransactionResult & Partial<AppCompilationResult>
export type AppClientComposeCallCoreParams = Omit<AppClientCallCoreParams, 'sendParams'> & {
sendParams?: Omit<SendTransactionParams, 'skipSending' | 'atc' | 'skipWaiting' | 'maxRoundsToWaitForConfirmation' | 'populateAppCallResources'>
}
export type AppClientComposeExecuteParams = Pick<SendTransactionParams, 'skipWaiting' | 'maxRoundsToWaitForConfirmation' | 'populateAppCallResources' | 'suppressLog'>
/**
* Defines the types of available calls and state of the ControlledAddress smart contract.
*/
export type ControlledAddress = {
/**
* Maps method signatures / names to their argument and return types.
*/
methods:
& Record<'new()address' | 'new', {
argsObj: {
}
argsTuple: []
returns: string
}>
}
/**
* Defines the possible abi call signatures
*/
export type ControlledAddressSig = keyof ControlledAddress['methods']
/**
* Defines an object containing all relevant parameters for a single call to the contract. Where TSignature is undefined, a bare call is made
*/
export type TypedCallParams<TSignature extends ControlledAddressSig | undefined> = {
method: TSignature
methodArgs: TSignature extends undefined ? undefined : Array<ABIAppCallArg | undefined>
} & AppClientCallCoreParams & CoreAppCallArgs
/**
* Defines the arguments required for a bare call
*/
export type BareCallArgs = Omit<RawAppCallArgs, keyof CoreAppCallArgs>
/**
* Maps a method signature from the ControlledAddress smart contract to the method's arguments in either tuple of struct form
*/
export type MethodArgs<TSignature extends ControlledAddressSig> = ControlledAddress['methods'][TSignature]['argsObj' | 'argsTuple']
/**
* Maps a method signature from the ControlledAddress smart contract to the method's return type
*/
export type MethodReturn<TSignature extends ControlledAddressSig> = ControlledAddress['methods'][TSignature]['returns']
/**
* A factory for available 'create' calls
*/
export type ControlledAddressCreateCalls = (typeof ControlledAddressCallFactory)['create']
/**
* Defines supported create methods for this smart contract
*/
export type ControlledAddressCreateCallParams =
| (TypedCallParams<'new()address'> & (OnCompleteDelApp))
/**
* Defines arguments required for the deploy method.
*/
export type ControlledAddressDeployArgs = {
deployTimeParams?: TealTemplateParams
/**
* A delegate which takes a create call factory and returns the create call params for this smart contract
*/
createCall?: (callFactory: ControlledAddressCreateCalls) => ControlledAddressCreateCallParams
}
/**
* Exposes methods for constructing all available smart contract calls
*/
export abstract class ControlledAddressCallFactory {
/**
* Gets available create call factories
*/
static get create() {
return {
/**
* Constructs a create call for the ControlledAddress smart contract using the new()address ABI method
*
* @param args Any args for the contract call
* @param params Any additional parameters for the call
* @returns A TypedCallParams object for the call
*/
new(args: MethodArgs<'new()address'>, params: AppClientCallCoreParams & CoreAppCallArgs & AppClientCompilationParams & (OnCompleteDelApp)) {
return {
method: 'new()address' as const,
methodArgs: Array.isArray(args) ? args : [],
...params,
}
},
}
}
}
/**
* A client to make calls to the ControlledAddress smart contract
*/
export class ControlledAddressClient {
/**
* The underlying `ApplicationClient` for when you want to have more flexibility
*/
public readonly appClient: ApplicationClient
private readonly sender: SendTransactionFrom | undefined
/**
* Creates a new instance of `ControlledAddressClient`
*
* @param appDetails appDetails The details to identify the app to deploy
* @param algod An algod client instance
*/
constructor(appDetails: AppDetails, private algod: Algodv2) {
this.sender = appDetails.sender
this.appClient = algokit.getAppClient({
...appDetails,
app: APP_SPEC
}, algod)
}
/**
* Checks for decode errors on the AppCallTransactionResult and maps the return value to the specified generic type
*
* @param result The AppCallTransactionResult to be mapped
* @param returnValueFormatter An optional delegate to format the return value if required
* @returns The smart contract response with an updated return value
*/
protected mapReturnValue<TReturn, TResult extends AppCallTransactionResult = AppCallTransactionResult>(result: AppCallTransactionResult, returnValueFormatter?: (value: any) => TReturn): AppCallTransactionResultOfType<TReturn> & TResult {
if(result.return?.decodeError) {
throw result.return.decodeError
}
const returnValue = result.return?.returnValue !== undefined && returnValueFormatter !== undefined
? returnValueFormatter(result.return.returnValue)
: result.return?.returnValue as TReturn | undefined
return { ...result, return: returnValue } as AppCallTransactionResultOfType<TReturn> & TResult
}
/**
* Calls the ABI method with the matching signature using an onCompletion code of NO_OP
*
* @param typedCallParams An object containing the method signature, args, and any other relevant parameters
* @param returnValueFormatter An optional delegate which when provided will be used to map non-undefined return values to the target type
* @returns The result of the smart contract call
*/
public async call<TSignature extends keyof ControlledAddress['methods']>(typedCallParams: TypedCallParams<TSignature>, returnValueFormatter?: (value: any) => MethodReturn<TSignature>) {
return this.mapReturnValue<MethodReturn<TSignature>>(await this.appClient.call(typedCallParams), returnValueFormatter)
}
/**
* Idempotently deploys the ControlledAddress smart contract.
*
* @param params The arguments for the contract calls and any additional parameters for the call
* @returns The deployment result
*/
public deploy(params: ControlledAddressDeployArgs & AppClientDeployCoreParams = {}): ReturnType<ApplicationClient['deploy']> {
const createArgs = params.createCall?.(ControlledAddressCallFactory.create)
return this.appClient.deploy({
...params,
createArgs,
createOnCompleteAction: createArgs?.onCompleteAction,
})
}
/**
* Gets available create methods
*/
public get create() {
const $this = this
return {
/**
* Creates a new instance of the ControlledAddress smart contract using the new()address ABI method.
*
* @param args The arguments for the smart contract call
* @param params Any additional parameters for the call
* @returns The create result
*/
async new(args: MethodArgs<'new()address'>, params: AppClientCallCoreParams & AppClientCompilationParams & (OnCompleteDelApp)) {
return $this.mapReturnValue<MethodReturn<'new()address'>, AppCreateCallTransactionResult>(await $this.appClient.create(ControlledAddressCallFactory.create.new(args, params)))
},
}
}
/**
* Makes a clear_state call to an existing instance of the ControlledAddress smart contract.
*
* @param args The arguments for the bare call
* @returns The clear_state result
*/
public clearState(args: BareCallArgs & AppClientCallCoreParams & CoreAppCallArgs = {}) {
return this.appClient.clearState(args)
}
public compose(): ControlledAddressComposer {
const client = this
const atc = new AtomicTransactionComposer()
let promiseChain:Promise<unknown> = Promise.resolve()
const resultMappers: Array<undefined | ((x: any) => any)> = []
return {
clearState(args?: BareCallArgs & AppClientComposeCallCoreParams & CoreAppCallArgs) {
promiseChain = promiseChain.then(() => client.clearState({...args, sendParams: {...args?.sendParams, skipSending: true, atc}}))
resultMappers.push(undefined)
return this
},
addTransaction(txn: TransactionWithSigner | TransactionToSign | Transaction | Promise<SendTransactionResult>, defaultSender?: SendTransactionFrom) {
promiseChain = promiseChain.then(async () => atc.addTransaction(await algokit.getTransactionWithSigner(txn, defaultSender ?? client.sender)))
return this
},
async atc() {
await promiseChain
return atc
},
async simulate(options?: SimulateOptions) {
await promiseChain
const result = await atc.simulate(client.algod, new modelsv2.SimulateRequest({ txnGroups: [], ...options }))
return {
...result,
returns: result.methodResults?.map((val, i) => resultMappers[i] !== undefined ? resultMappers[i]!(val.returnValue) : val.returnValue)
}
},
async execute(sendParams?: AppClientComposeExecuteParams) {
await promiseChain
const result = await algokit.sendAtomicTransactionComposer({ atc, sendParams }, client.algod)
return {
...result,
returns: result.returns?.map((val, i) => resultMappers[i] !== undefined ? resultMappers[i]!(val.returnValue) : val.returnValue)
}
}
} as unknown as ControlledAddressComposer
}
}
export type ControlledAddressComposer<TReturns extends [...any[]] = []> = {
/**
* Makes a clear_state call to an existing instance of the ControlledAddress smart contract.
*
* @param args The arguments for the bare call
* @returns The typed transaction composer so you can fluently chain multiple calls or call execute to execute all queued up transactions
*/
clearState(args?: BareCallArgs & AppClientComposeCallCoreParams & CoreAppCallArgs): ControlledAddressComposer<[...TReturns, undefined]>
/**
* Adds a transaction to the composer
*
* @param txn One of: A TransactionWithSigner object (returned as is), a TransactionToSign object (signer is obtained from the signer property), a Transaction object (signer is extracted from the defaultSender parameter), an async SendTransactionResult returned by one of algokit utils helpers (signer is obtained from the defaultSender parameter)
* @param defaultSender The default sender to be used to obtain a signer where the object provided to the transaction parameter does not include a signer.
*/
addTransaction(txn: TransactionWithSigner | TransactionToSign | Transaction | Promise<SendTransactionResult>, defaultSender?: SendTransactionFrom): ControlledAddressComposer<TReturns>
/**
* Returns the underlying AtomicTransactionComposer instance
*/
atc(): Promise<AtomicTransactionComposer>
/**
* Simulates the transaction group and returns the result
*/
simulate(options?: SimulateOptions): Promise<ControlledAddressComposerSimulateResult<TReturns>>
/**
* Executes the transaction group and returns the results
*/
execute(sendParams?: AppClientComposeExecuteParams): Promise<ControlledAddressComposerResults<TReturns>>
}
export type SimulateOptions = Omit<ConstructorParameters<typeof modelsv2.SimulateRequest>[0], 'txnGroups'>
export type ControlledAddressComposerSimulateResult<TReturns extends [...any[]]> = {
returns: TReturns
methodResults: ABIResult[]
simulateResponse: modelsv2.SimulateResponse
}
export type ControlledAddressComposerResults<TReturns extends [...any[]]> = {
returns: TReturns
groupId: string
txIds: string[]
transactions: Transaction[]
}