Skip to main content

Class: SmartContract

The main zkapp class. To write a zkapp, extend this class as such:

class YourSmartContract extends SmartContract {
// your smart contract code here
}

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new SmartContract(address, tokenId?)

Parameters

NameType
addressPublicKey
tokenId?Field

Defined in

lib/zkapp.ts:616

Properties

#executionState

Private #executionState: undefined | ExecutionState

Defined in

lib/zkapp.ts:595


address

address: PublicKey

Defined in

lib/zkapp.ts:592


events

events: Object = {}

A list of event types that can be emitted using this.emitEvent()`.

Index signature

[key: string]: ProvablePure<any>

Defined in

lib/zkapp.ts:927


tokenId

tokenId: Field

Defined in

lib/zkapp.ts:593


_maxProofsVerified

Static Optional _maxProofsVerified: 0 | 2 | 1

Defined in

lib/zkapp.ts:602


_methodMetadata

Static _methodMetadata: Record<string, { digest: string ; hasReturn: boolean ; rows: number ; sequenceEvents: number }> = {}

Defined in

lib/zkapp.ts:597


_methods

Static Optional _methods: MethodInterface[]

Defined in

lib/zkapp.ts:596


_provers

Static Optional _provers: Prover[]

Defined in

lib/zkapp.ts:601


_verificationKey

Static Optional _verificationKey: Object

Type declaration

NameType
datastring
hashField

Defined in

lib/zkapp.ts:603

Accessors

account

get account(): PreconditionClassType<AccountPrecondition>

Current account of the SmartContract.

Returns

PreconditionClassType<AccountPrecondition>

Defined in

lib/zkapp.ts:855


balance

get balance(): Object

Balance of this SmartContract.

Returns

Object

NameType
addInPlace(x: string | number | bigint | UInt64 | UInt32 | Int64) => void
subInPlace(x: string | number | bigint | UInt64 | UInt32 | Int64) => void

Defined in

lib/zkapp.ts:921


network

get network(): PreconditionClassType<{ blockchainLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; globalSlotSinceGenesis: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; globalSlotSinceHardFork: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; minWindowDensity: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; nextEpochData: { epochLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; ledger: { hash: { isSome: Bool ; value: Field } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } } ; lockCheckpoint: { isSome: Bool ; value: Field } ; seed: { isSome: Bool ; value: Field } ; startCheckpoint: { isSome: Bool ; value: Field } } ; snarkedLedgerHash: { isSome: Bool ; value: Field } ; stakingEpochData: { epochLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; ledger: { hash: { isSome: Bool ; value: Field } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } } ; lockCheckpoint: { isSome: Bool ; value: Field } ; seed: { isSome: Bool ; value: Field } ; startCheckpoint: { isSome: Bool ; value: Field } } ; timestamp: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } }>

Current network state of the SmartContract.

Returns

PreconditionClassType<{ blockchainLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; globalSlotSinceGenesis: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; globalSlotSinceHardFork: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; minWindowDensity: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; nextEpochData: { epochLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; ledger: { hash: { isSome: Bool ; value: Field } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } } ; lockCheckpoint: { isSome: Bool ; value: Field } ; seed: { isSome: Bool ; value: Field } ; startCheckpoint: { isSome: Bool ; value: Field } } ; snarkedLedgerHash: { isSome: Bool ; value: Field } ; stakingEpochData: { epochLength: { isSome: Bool ; value: { lower: UInt32 ; upper: UInt32 } } ; ledger: { hash: { isSome: Bool ; value: Field } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } } ; lockCheckpoint: { isSome: Bool ; value: Field } ; seed: { isSome: Bool ; value: Field } ; startCheckpoint: { isSome: Bool ; value: Field } } ; timestamp: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } ; totalCurrency: { isSome: Bool ; value: { lower: UInt64 ; upper: UInt64 } } }>

Defined in

lib/zkapp.ts:861


self

get self(): AccountUpdate

Returns the current AccountUpdate associated to this SmartContract.

Returns

AccountUpdate

Defined in

lib/zkapp.ts:809


token

get token(): Object

Token of the SmartContract.

Returns

Object

NameType
idField
parentTokenIdField
tokenOwnerPublicKey
burn(__namedParameters: { address: PublicKey ; amount: number | bigint | UInt64 }) => void
mint(__namedParameters: { address: PublicKey ; amount: number | bigint | UInt64 }) => AccountUpdate
send(__namedParameters: { amount: number | bigint | UInt64 ; from: PublicKey ; to: PublicKey }) => AccountUpdate

Defined in

lib/zkapp.ts:867


tokenSymbol

get tokenSymbol(): Object

Token symbol of this token.

Returns

Object

NameType
set(tokenSymbol: string) => void

Defined in

lib/zkapp.ts:915

Methods

approve

approve(updateOrCallback, layout?): AccountUpdate

Approve an account update or callback. This will include the account update in the zkApp's public input, which means it allows you to read and use its content in a proof, make assertions about it, and modify it.

If this is called with a callback as the first parameter, it will first extract the account update produced by that callback. The extracted account update is returned.

\@method myApprovingMethod(callback: Callback) {
let approvedUpdate = this.approve(callback);
}

Under the hood, "approving" just means that the account update is made a child of the zkApp in the tree of account updates that forms the transaction. The second parameter layout allows you to also make assertions about the approved update's own children, by specifying a certain expected layout of children. See Layout.

Parameters

NameType
updateOrCallbackAccountUpdate | Callback<any>
layout?AccountUpdatesLayout

Returns

AccountUpdate

The account update that was approved (needed when passing in a Callback)

Defined in

lib/zkapp.ts:893


deploy

deploy(__namedParameters?): void

Deploys a SmartContract.

let tx = await Mina.transaction(feePayer, () => {
AccountUpdate.fundNewAccount(feePayer, { initialBalance });
zkapp.deploy({ zkappKey });
});

Parameters

NameType
__namedParametersObject
__namedParameters.verificationKey?Object
__namedParameters.verificationKey.datastring
__namedParameters.verificationKey.hashstring | Field
__namedParameters.zkappKey?PrivateKey

Returns

void

Defined in

lib/zkapp.ts:702


emitEvent

emitEvent<K>(type, event): void

Emits an event. Events will be emitted as a part of the transaction and can be collected by archive nodes.

Type parameters

NameType
Kextends string | number

Parameters

NameType
typeK
eventany

Returns

void

Defined in

lib/zkapp.ts:933


fetchEvents

fetchEvents(start?, end?): Promise<{ event: ProvablePure<any> ; type: string }[]>

Fetches a list of events that have been emitted by this SmartContract.

Parameters

NameType
startUInt32
end?UInt32

Returns

Promise<{ event: ProvablePure<any> ; type: string }[]>

Defined in

lib/zkapp.ts:969


init

init(zkappKey?): void

SmartContract.init() will be called only when a SmartContract will be first deployed, not for redeployment. This method can be overridden as follows

class MyContract extends SmartContract {
init() {
super.init();
this.setPermissions();
this.x.set(Field(1));
}
}

Parameters

NameType
zkappKey?PrivateKey

Returns

void

Defined in

lib/zkapp.ts:762


newSelf

newSelf(): AccountUpdate

Same as SmartContract.self but explicitly creates a new AccountUpdate.

Returns

AccountUpdate

Defined in

lib/zkapp.ts:844


requireSignature

requireSignature(): void

Use this command if the account update created by this SmartContract should be signed by the account owner, instead of authorized with a proof.

Note that the smart contract's Permissions determine which updates have to be (can be) authorized by a signature.

If you only want to avoid creating proofs for quicker testing, we advise you to use LocalBlockchain({ proofsEnabled: false }) instead of requireSignature(). Setting proofsEnabled to false allows you to test your transactions with the same authorization flow as in production, with the only difference being that quick mock proofs are filled in instead of real proofs.

Returns

void

Defined in

lib/zkapp.ts:785


send

send(args): void

Parameters

NameType
argsObject
args.amountnumber | bigint | UInt64
args.toPublicKey | AccountUpdate

Returns

void

Defined in

lib/zkapp.ts:905


setPermissions

setPermissions(permissions): void

Changes the Permissions of this SmartContract.

Parameters

NameType
permissionsPermissions

Returns

void

Defined in

lib/zkapp.ts:1088


setValue

setValue<T>(maybeValue, value): void

Type parameters

Name
T

Parameters

NameType
maybeValueSetOrKeep<T>
valueT

Returns

void

Defined in

lib/zkapp.ts:1079


sign

sign(zkappKey?): void

Deprecated

this.sign() is deprecated in favor of this.requireSignature()

Parameters

NameType
zkappKey?PrivateKey

Returns

void

Defined in

lib/zkapp.ts:791


skipAuthorization

skipAuthorization(): void

Use this command if the account update created by this SmartContract should have no authorization on it, instead of being authorized with a proof.

WARNING: This is a method that should rarely be useful. If you want to disable proofs for quicker testing, take a look at LocalBlockchain({ proofsEnabled: false }), which causes mock proofs to be created and doesn't require changing the authorization flow.

Returns

void

Defined in

lib/zkapp.ts:802


Proof

Static Proof(): typeof __class

Returns a Proof type that belongs to this SmartContract.

Returns

typeof __class

Defined in

lib/zkapp.ts:608


analyzeMethods

Static analyzeMethods(): Record<string, { digest: string ; hasReturn: boolean ; rows: number ; sequenceEvents: number }>

This function is run internally before compiling a smart contract, to collect metadata about what each of your smart contract methods does.

For external usage, this function can be handy because calling it involves running all methods in the same "mode" as compile() does, so it serves as a quick-to-run check for whether your contract can be compiled without errors, which can greatly speed up iterating.

analyzeMethods() will also return the number of rows of each of your method circuits (i.e., the number of constraints in the underlying proof system), which is a good indicator for circuit size and the time it will take to create proofs.

Note: If this function was already called before, it will short-circuit and just return the metadata collected the first time.

Returns

Record<string, { digest: string ; hasReturn: boolean ; rows: number ; sequenceEvents: number }>

an object, keyed by method name, each entry containing:

  • rows the size of the constraint system created by this method
  • digest a digest of the method circuit
  • hasReturn a boolean indicating whether the method returns a value
  • sequenceEvents the number of actions the method dispatches

Defined in

lib/zkapp.ts:1037


compile

Static compile(): Promise<{ provers: Prover[] ; verificationKey: { data: string ; hash: string } ; verify: (publicInput: PublicInput, proof: unknown) => Promise<boolean> }>

Compile your smart contract.

This generates both the prover functions, needed to create proofs for running @methods, and the verification key, needed to deploy your zkApp.

Although provers and verification key are returned by this method, they are also cached internally and used when needed, so you don't actually have to use the return value of this function.

Under the hood, "compiling" means calling into the lower-level Pickles and Kimchi libraries to create two prover & verifier indices (one for the "step circuit" which combines all of your smart contract methods into one circuit, and one for the "wrap circuit" which wraps it so that proofs end up in the original finite field). These are fairly expensive operations, so expect compiling to take at least 20 seconds, up to several minutes if your circuit is large or your hardware is not optimal for these operations.

Returns

Promise<{ provers: Prover[] ; verificationKey: { data: string ; hash: string } ; verify: (publicInput: PublicInput, proof: unknown) => Promise<boolean> }>

Defined in

lib/zkapp.ts:644


digest

Static digest(): string

Computes a hash of your smart contract, which will reliably change whenever one of your method circuits changes. This digest is quick to compute. it is designed to help with deciding whether a contract should be re-compiled or a cached verification key can be used.

Returns

string

the digest, as a hex string

Defined in

lib/zkapp.ts:682


runOutsideCircuit

Static runOutsideCircuit(run): void

Parameters

NameType
run() => void

Returns

void

Defined in

lib/zkapp.ts:1013