By default, Algorand applications can be created, updated, and deleted. In TEALScript, applications can be created by default, but cannot be updated to deleted. The default createApplication method won't run any logic, but rather simply create the application on the chain.
To modify the logic executed upon applicaiton creation (for example, to set default storage values) your contract class must implement a method to override createApplication.
class Counter extends Contract {
counter = GlobalStateKey<number>();
createApplication(startingNumber: number): void {
this.counter.value = startingNumber
}
}
By defualt, TEALScript contracts cannot be updated. To allow a contract to be updated, a method that overrides updateApplication must be implemented.
class Counter extends Contract {
counter = GlobalStateKey<number>();
createApplication(startingNumber: number): void {
this.counter.value = startingNumber
}
updateApplication(): void {
assert(this.txn.sender === this.app.creator)
}
}
By defualt, TEALScript contracts cannot be deleted. To allow a contract to be deleted, a method that overrides deleteApplication must be implemented.
class Counter extends Contract {
counter = GlobalStateKey<number>();
createApplication(startingNumber: number): void {
this.counter.value = startingNumber
}
deleteApplication(): void {
assert(this.txn.sender === this.app.creator)
}
}
If your contract uses local state, you will need to override the optInToApplication method and override closeOutOfApplication and/or clearState as desired. To learn more about contract state, see On-Chain Storage
To have more granular control on what OnComplete a specific method allows, use the call or create decorator to control allowed OnCompletes when calling or creating the application.
class Counter extends Contract {
counter = LocalStateKey<number>();
// This method will increment a counter in local state
@allow.create('OptIn') // Allow an OptIn create so the creators counter can be set when creating the app
@allow.call('OptIn') // Allow anyone to OptIn to the contract so they can use local state
@allow.call('NoOp') // Allow anyone to call the app again with a NoOp call (can only OptIn once)
useLocalState(): void {
if (!this.counter(this.txn.sender).exists) this.counter(this.txn.sender).value = 1
else this.counter(this.txn.sender).value = this.counter(this.txn.sender).value + 1
}
}
Generated using TypeDoc