Skip to main content

State

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

Defined in: lib/mina/v1/state.ts:86

Gettable and settable state that can be checked for equality.

Type Parameters

A

A

Methods

fetch()

fetch(): Promise<undefined | A>;

Defined in: lib/mina/v1/state.ts:57

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

Returns

Promise<undefined | A>


fromAppState()

fromAppState(appState: Field[]): A;

Defined in: lib/mina/v1/state.ts:84

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(): A;

Defined in: lib/mina/v1/state.ts:44

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()

getAndRequireEquals(): A;

Defined in: lib/mina/v1/state.ts:49

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()

requireEquals(a: A): void;

Defined in: lib/mina/v1/state.ts:62

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


requireEqualsIf()

requireEqualsIf(condition: Bool, a: A): void;

Defined in: lib/mina/v1/state.ts:69

Require that the on-chain state has to equal the given state if the provided condition is true.

If the condition is false, this is a no-op. If the condition is true, this adds a precondition that the verifying Mina node will check before accepting this transaction.

Parameters

condition

Bool

a

A

Returns

void


requireNothing()

requireNothing(): void;

Defined in: lib/mina/v1/state.ts:73

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

Returns

void


set()

set(a: A): void;

Defined in: lib/mina/v1/state.ts:53

Set the on-chain state to a new value.

Parameters

a

A

Returns

void