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
- _executionState
- address
- events
- tokenId
- _maxProofsVerified
- _methodMetadata
- _methods
- _provers
- _verificationKey
Accessors
Methods
- deploy
- emitEvent
- executionState
- fetchEvents
- send
- setPermissions
- setValue
- sign
- Proof
- analyzeMethods
- compile
- digest
- runOutsideCircuit
Constructors
constructor
• new SmartContract(address
, tokenId?
)
Parameters
Name | Type |
---|---|
address | PublicKey |
tokenId? | Field |
Defined in
Properties
_executionState
• Private
_executionState: undefined
| ExecutionState
Defined in
address
• address: PublicKey
Defined in
events
• events: Object
= {}
Index signature
▪ [key: string
]: AsFieldElements
<any
>
Defined in
tokenId
• tokenId: Field
Defined in
_maxProofsVerified
▪ Static
Optional
_maxProofsVerified: 0
| 2
| 1
Defined in
_methodMetadata
▪ Static
Private
_methodMetadata: Record
<string
, { digest
: string
; hasReturn
: boolean
; rows
: number
; sequenceEvents
: number
}> = {}
Defined in
_methods
▪ Static
Optional
_methods: MethodInterface
[]
Defined in
_provers
▪ Static
Optional
_provers: Prover
[]
Defined in
_verificationKey
▪ Static
Optional
_verificationKey: Object
Type declaration
Name | Type |
---|---|
data | string |
hash | Field |
Defined in
Accessors
account
• get
account(): PreconditionClassType
<AccountPrecondition
>
Returns
PreconditionClassType
<AccountPrecondition
>
Defined in
balance
• get
balance(): Object
Returns
Object
Name | Type |
---|---|
addInPlace | (x : string | number | bigint | UInt64 | UInt32 | Int64 ) => void |
subInPlace | (x : string | number | bigint | UInt64 | UInt32 | Int64 ) => void |
Defined in
experimental
• get
experimental(): Object
Returns
Object
Name | Type |
---|---|
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
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
self
• get
self(): AccountUpdate
Returns
Defined in
tokenSymbol
• get
tokenSymbol(): Object
Returns
Object
Name | Type |
---|---|
set | (tokenSymbol : string ) => void |
Defined in
Methods
deploy
▸ deploy(__namedParameters?
): void
Parameters
Name | Type |
---|---|
__namedParameters | Object |
__namedParameters.verificationKey? | Object |
__namedParameters.verificationKey.data | string |
__namedParameters.verificationKey.hash | string | Field |
__namedParameters.zkappKey? | PrivateKey |
Returns
void
Defined in
emitEvent
▸ emitEvent<K
>(type
, event
): void
Type parameters
Name | Type |
---|---|
K | extends string | number |
Parameters
Name | Type |
---|---|
type | K |
event | any |
Returns
void
Defined in
executionState
▸ Private
executionState(): AccountUpdate
Returns
Defined in
fetchEvents
▸ fetchEvents(start?
, end?
): Promise
<{ event
: AsFieldElements
<any
> ; type
: string
}[]>
Parameters
Name | Type |
---|---|
start | UInt32 |
end? | UInt32 |
Returns
Promise
<{ event
: AsFieldElements
<any
> ; type
: string
}[]>
Defined in
send
▸ send(args
): void
Parameters
Name | Type |
---|---|
args | Object |
args.amount | number | bigint | UInt64 |
args.to | PublicKey | AccountUpdate |
Returns
void
Defined in
setPermissions
▸ setPermissions(permissions
): void
Parameters
Name | Type |
---|---|
permissions | Permissions |
Returns
void
Defined in
setValue
▸ setValue<T
>(maybeValue
, value
): void
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
maybeValue | SetOrKeep <T > |
value | T |
Returns
void
Defined in
sign
▸ sign(zkappKey?
): void
Parameters
Name | Type |
---|---|
zkappKey? | PrivateKey |
Returns
void
Defined in
Proof
▸ Static
Proof(): typeof __class
Returns
typeof __class
Defined in
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 methoddigest
a digest of the method circuithasReturn
a boolean indicating whether the method returns a valuesequenceEvents
the number of actions the method dispatches
Defined in
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 @method
s,
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
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
runOutsideCircuit
▸ Static
runOutsideCircuit(run
): void
Parameters
Name | Type |
---|---|
run | () => void |
Returns
void