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:588

Properties

_executionState

Private _executionState: undefined | ExecutionState

Defined in

lib/zkapp.ts:570


address

address: PublicKey

Defined in

lib/zkapp.ts:567


events

events: Object = {}

Index signature

[key: string]: AsFieldElements<any>

Defined in

lib/zkapp.ts:757


tokenId

tokenId: Field

Defined in

lib/zkapp.ts:568


_maxProofsVerified

Static Optional _maxProofsVerified: 0 | 2 | 1

Defined in

lib/zkapp.ts:577


_methodMetadata

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

Defined in

lib/zkapp.ts:572


_methods

Static Optional _methods: MethodInterface[]

Defined in

lib/zkapp.ts:571


_provers

Static Optional _provers: Prover[]

Defined in

lib/zkapp.ts:576


_verificationKey

Static Optional _verificationKey: Object

Type declaration

NameType
datastring
hashField

Defined in

lib/zkapp.ts:578

Accessors

account

get account(): PreconditionClassType<AccountPrecondition>

Returns

PreconditionClassType<AccountPrecondition>

Defined in

lib/zkapp.ts:725


balance

get balance(): Object

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:753


experimental

get experimental(): Object

Returns

Object

NameType
get token(){ id: Field = customToken.id; parentTokenId: Field = customToken.parentTokenId; tokenOwner: PublicKey = customToken.tokenOwner; 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:733


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 } } }>

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:729


self

get self(): AccountUpdate

Returns

AccountUpdate

Defined in

lib/zkapp.ts:721


tokenSymbol

get tokenSymbol(): Object

Returns

Object

NameType
set(tokenSymbol: string) => void

Defined in

lib/zkapp.ts:749

Methods

deploy

deploy(__namedParameters?): void

Parameters

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

Returns

void

Defined in

lib/zkapp.ts:664


emitEvent

emitEvent<K>(type, event): void

Type parameters

NameType
Kextends string | number

Parameters

NameType
typeK
eventany

Returns

void

Defined in

lib/zkapp.ts:760


executionState

Private executionState(): AccountUpdate

Returns

AccountUpdate

Defined in

lib/zkapp.ts:685


fetchEvents

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

Parameters

NameType
startUInt32
end?UInt32

Returns

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

Defined in

lib/zkapp.ts:793


send

send(args): void

Parameters

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

Returns

void

Defined in

lib/zkapp.ts:742


setPermissions

setPermissions(permissions): void

Parameters

NameType
permissionsPermissions

Returns

void

Defined in

lib/zkapp.ts:908


setValue

setValue<T>(maybeValue, value): void

Type parameters

Name
T

Parameters

NameType
maybeValueSetOrKeep<T>
valueT

Returns

void

Defined in

lib/zkapp.ts:902


sign

sign(zkappKey?): void

Parameters

NameType
zkappKey?PrivateKey

Returns

void

Defined in

lib/zkapp.ts:681


Proof

Static Proof(): typeof __class

Returns

typeof __class

Defined in

lib/zkapp.ts:580


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:861


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:616


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:654


runOutsideCircuit

Static runOutsideCircuit(run): void

Parameters

NameType
run() => void

Returns

void

Defined in

lib/zkapp.ts:837