Skip to content

Commit 483b80f

Browse files
committed
feat: support create method in AppClient
Main driver behind this change is allowing the factory to use an app client to reduce duplicated code
1 parent 4f1d5d0 commit 483b80f

File tree

3 files changed

+140
-313
lines changed

3 files changed

+140
-313
lines changed

src/types/app-client.ts

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { AppSpec, arc32ToArc56 } from './app-spec'
6161
import {
6262
AppCallMethodCall,
6363
AppCallParams,
64+
AppCreateMethodCall,
6465
AppDeleteMethodCall,
6566
AppDeleteParams,
6667
AppMethodCall,
@@ -260,9 +261,9 @@ export interface FundAppAccountParams {
260261
/** Source maps for an Algorand app */
261262
export interface AppSourceMaps {
262263
/** The source map of the approval program */
263-
approvalSourceMap: SourceMapExport
264+
approvalSourceMap: algosdk.ProgramSourceMap
264265
/** The source map of the clear program */
265-
clearSourceMap: SourceMapExport
266+
clearSourceMap: algosdk.ProgramSourceMap
266267
}
267268

268269
export interface SourceMapExport {
@@ -353,7 +354,14 @@ export type CallOnComplete = {
353354

354355
/** AppClient common parameters for a bare app call */
355356
export type AppClientBareCallParams = Expand<
356-
Omit<CommonAppCallParams, 'appId' | 'sender' | 'onComplete'> & {
357+
Omit<CommonAppCallParams, 'appId' | 'sender'> & {
358+
/** The address of the account sending the transaction, if undefined then the app client's defaultSender is used. */
359+
sender?: Address | string
360+
}
361+
>
362+
363+
export type AppClientBareCreateParams = Expand<
364+
Omit<CommonAppCallParams, 'appId' | 'sender'> & {
357365
/** The address of the account sending the transaction, if undefined then the app client's defaultSender is used. */
358366
sender?: Address | string
359367
}
@@ -1193,6 +1201,15 @@ export class AppClient {
11931201
OnApplicationComplete.UpdateApplicationOC,
11941202
) as AppUpdateParams
11951203
},
1204+
create: async (params?: AppClientBareCallParams & AppClientCompilationParams) => {
1205+
return this.getBareParams(
1206+
{
1207+
...params,
1208+
...(await this.compile(params)),
1209+
},
1210+
params?.onComplete,
1211+
) as AppUpdateParams
1212+
},
11961213
/** Return params for an opt-in call */
11971214
optIn: (params?: AppClientBareCallParams) => {
11981215
return this.getBareParams(params, OnApplicationComplete.OptInOC) as AppCallParams
@@ -1222,6 +1239,9 @@ export class AppClient {
12221239
update: async (params?: AppClientBareCallParams & AppClientCompilationParams) => {
12231240
return this._algorand.createTransaction.appUpdate(await this.params.bare.update(params))
12241241
},
1242+
create: async (params?: AppClientBareCallParams & AppClientCompilationParams) => {
1243+
return this._algorand.createTransaction.appCreate(await this.params.bare.update(params))
1244+
},
12251245
/** Returns a transaction for an opt-in call */
12261246
optIn: (params?: AppClientBareCallParams) => {
12271247
return this._algorand.createTransaction.appCall(this.params.bare.optIn(params))
@@ -1255,6 +1275,16 @@ export class AppClient {
12551275
...(compiled as Partial<AppCompilationResult>),
12561276
}
12571277
},
1278+
create: async (params?: AppClientBareCallParams & AppClientCompilationParams & SendParams) => {
1279+
const compiled = await this.compile(params)
1280+
const results = {
1281+
...(await this._algorand.send.appCreate(await this.params.bare.create(params))),
1282+
...(compiled as Partial<AppCompilationResult>),
1283+
}
1284+
1285+
this._appId = results.appId
1286+
return results
1287+
},
12581288
/** Signs and sends an opt-in call */
12591289
optIn: (params?: AppClientBareCallParams & SendParams) => {
12601290
return this._algorand.send.appCall(this.params.bare.optIn(params))
@@ -1307,6 +1337,19 @@ export class AppClient {
13071337
OnApplicationComplete.UpdateApplicationOC,
13081338
)) satisfies AppUpdateMethodCall
13091339
},
1340+
create: async (params: AppClientMethodCallParams & AppClientCompilationParams) => {
1341+
if (params.onComplete === OnApplicationComplete.ClearStateOC) {
1342+
throw new Error(`Cannot create with an OnComplete value of ${params.onComplete}`)
1343+
}
1344+
1345+
return (await this.getABIParams(
1346+
{
1347+
...params,
1348+
...(await this.compile(params)),
1349+
},
1350+
params.onComplete,
1351+
)) satisfies AppCreateMethodCall
1352+
},
13101353
/**
13111354
* Return params for an opt-in ABI call
13121355
* @param params The parameters for the opt-in ABI method call
@@ -1364,6 +1407,24 @@ export class AppClient {
13641407
...(compiled as Partial<AppCompilationResult>),
13651408
}
13661409
},
1410+
/**
1411+
* Sign and send transactions for an update ABI call, including deploy-time TEAL template replacements and compilation if provided
1412+
* @param params The parameters for the update ABI method call
1413+
* @returns The result of sending the update ABI method call
1414+
*/
1415+
create: async (params: AppClientMethodCallParams & AppClientCompilationParams & SendParams) => {
1416+
const compiled = await this.compile(params)
1417+
const results = {
1418+
...(await this.processMethodCallReturn(
1419+
this._algorand.send.appCreateMethodCall(await this.params.create({ ...params })),
1420+
getArc56Method(params.method, this._appSpec),
1421+
)),
1422+
...(compiled as Partial<AppCompilationResult>),
1423+
}
1424+
1425+
this._appId = results.appId
1426+
return results
1427+
},
13671428
/**
13681429
* Sign and send transactions for an opt-in ABI call
13691430
* @param params The parameters for the opt-in ABI method call
@@ -1477,6 +1538,9 @@ export class AppClient {
14771538
update: async (params: AppClientMethodCallParams & AppClientCompilationParams) => {
14781539
return this._algorand.createTransaction.appUpdateMethodCall(await this.params.update(params))
14791540
},
1541+
create: async (params: AppClientMethodCallParams & AppClientCompilationParams) => {
1542+
return this._algorand.createTransaction.appCreateMethodCall(await this.params.update(params))
1543+
},
14801544
/**
14811545
* Return transactions for an opt-in ABI call
14821546
* @param params The parameters for the opt-in ABI method call
@@ -1534,7 +1598,7 @@ export class AppClient {
15341598
private getBareParams<
15351599
TParams extends { sender?: Address | string; signer?: TransactionSigner | TransactionSignerAccount } | undefined,
15361600
TOnComplete extends OnApplicationComplete,
1537-
>(params: TParams, onComplete: TOnComplete) {
1601+
>(params: TParams, onComplete?: TOnComplete) {
15381602
return {
15391603
...params,
15401604
appId: this._appId,
@@ -1552,7 +1616,7 @@ export class AppClient {
15521616
args?: AppClientMethodCallParams['args']
15531617
},
15541618
TOnComplete extends OnApplicationComplete,
1555-
>(params: TParams, onComplete: TOnComplete) {
1619+
>(params: TParams, onComplete?: TOnComplete) {
15561620
const sender = this.getSender(params.sender)
15571621
const method = getArc56Method(params.method, this._appSpec)
15581622
const args = await this.getABIArgsWithDefaultValues(params.method, params.args, sender)

src/types/app-deployer.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from './composer'
2626
import { Expand } from './expand'
2727
import { ConfirmedTransactionResult, SendParams } from './transaction'
28+
import { AppClient } from './app-client'
2829

2930
/** Params to specify an update transaction for an app deployment */
3031
export type DeployAppUpdateParams = Expand<Omit<AppUpdateParams, 'appId' | 'approvalProgram' | 'clearStateProgram'>>
@@ -252,18 +253,18 @@ export class AppDeployer {
252253
)
253254
const result = await ('method' in updateParams
254255
? this._transactionSender.appUpdateMethodCall({
256+
...updateParams,
257+
...sendParams,
255258
appId: existingApp.appId,
256259
approvalProgram,
257260
clearStateProgram,
258-
...updateParams,
259-
...sendParams,
260261
})
261262
: this._transactionSender.appUpdate({
263+
...updateParams,
264+
...sendParams,
262265
appId: existingApp.appId,
263266
approvalProgram,
264267
clearStateProgram,
265-
...updateParams,
266-
...sendParams,
267268
}))
268269
const appMetadata: AppMetadata = {
269270
appId: existingApp.appId,
@@ -300,9 +301,9 @@ export class AppDeployer {
300301
}
301302
const createIndex = await composer.count()
302303
if ('method' in deleteParams) {
303-
composer.addAppDeleteMethodCall({ appId: existingApp.appId, ...deleteParams })
304+
composer.addAppDeleteMethodCall({ ...deleteParams, appId: existingApp.appId })
304305
} else {
305-
composer.addAppDelete({ appId: existingApp.appId, ...deleteParams })
306+
composer.addAppDelete({ ...deleteParams, appId: existingApp.appId })
306307
}
307308
const result = await composer.send({ ...sendParams })
308309
const confirmation = result.confirmations.at(createIndex - 1)!

0 commit comments

Comments
 (0)