Skip to main content

declareState

function declareState<T>(SmartContract: T, states: Record<string, FlexibleProvablePure<unknown>>): void

declareState can be used in place of the @state decorator to declare on-chain state on a SmartContract. It should be placed after the class declaration. Here is an example of declaring a state property x of type Field.

class MyContract extends SmartContract {
x = State<Field>();
// ...
}
declareState(MyContract, { x: Field });

If you're using pure JS, it's not possible to use the built-in class field syntax, i.e. the following will not work:

// THIS IS WRONG IN JS!
class MyContract extends SmartContract {
x = State();
}
declareState(MyContract, { x: Field });

Instead, add a constructor where you assign the property:

class MyContract extends SmartContract {
constructor(x) {
super();
this.x = State();
}
}
declareState(MyContract, { x: Field });

Type parameters

T extends typeof SmartContract

Parameters

SmartContract: T

states: Record\<string, FlexibleProvablePure\<unknown>>

Returns

void

Source

lib/mina/state.ts:163