Skip to main content

State

type State<A>: {
"fetch": Promise<undefined | A>;
"fromAppState": A;
"get": A;
"getAndRequireEquals": A;
"requireEquals": void;
"requireNothing": void;
"set": void;
};

Gettable and settable state that can be checked for equality.

Type parameters

A

Type declaration

fetch()

Asynchronously fetch the on-chain state. This is intended for getting the state outside a smart contract.

Returns

Promise\<undefined | A>

fromAppState()

Get the state from the raw list of field elements on a zkApp account, for example:

let myContract = new MyContract(address);
let account = Mina.getAccount(address);

let x = myContract.x.fromAppState(account.zkapp!.appState);

Parameters

appState: Field[]

Returns

A

get()

Get the current on-chain state.

Caution: If you use this method alone inside a smart contract, it does not prove that your contract uses the current on-chain state. To successfully prove that your contract uses the current on-chain state, you must add an additional .requireEquals() statement or use .getAndRequireEquals():

let x = this.x.get();
this.x.requireEquals(x);

OR

let x = this.x.getAndRequireEquals();

Returns

A

getAndRequireEquals()

Get the current on-chain state and prove it really has to equal the on-chain state, by adding a precondition which the verifying Mina node will check before accepting this transaction.

Returns

A

requireEquals()

Prove that the on-chain state has to equal the given state, by adding a precondition which the verifying Mina node will check before accepting this transaction.

Parameters

a: A

Returns

void

requireNothing()

DANGER ZONE: Override the error message that warns you when you use .get() without adding a precondition.

Returns

void

set()

Set the on-chain state to a new value.

Parameters

a: A

Returns

void

Source

lib/mina/state.ts:78