Skip to content

Commit da3c5b7

Browse files
authored
feat: readState - state parameter added (#507)
1 parent 46fb180 commit da3c5b7

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/contract/Contract.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ export interface Contract<State = unknown> {
106106
* Returns state of the contract at required sortKey or blockHeight.
107107
*
108108
* @param sortKeyOrBlockHeight - either a sortKey or block height at which the contract should be read
109-
*
110-
* @param currentTx - a set of currently evaluating interactions, that should
111-
* be skipped during contract inner calls - to prevent the infinite call loop issue
112-
* (mostly related to contract that use the Foreign Call Protocol)
109+
* @param interactions - optional interactions to be applied on state
110+
* @param signal - allows to communicate with a DOM request (such as Fetch) and abort it
111+
* @param state - uses specified state to read contract state (overrides reading state from the cache)
113112
*/
114113
readState(
115114
sortKeyOrBlockHeight?: string | number,
116115
interactions?: GQLNodeInterface[],
117-
signal?: AbortSignal
116+
signal?: AbortSignal,
117+
state?: SortKeyCacheResult<EvalStateResult<State>>
118118
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;
119119

120120
/**
@@ -128,10 +128,18 @@ export interface Contract<State = unknown> {
128128
signal?: AbortSignal
129129
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;
130130

131+
/**
132+
* Reads state at a specified sortKey and applies indicated interactions
133+
* @param sortKey - sortKey at which the contract should be read
134+
* @param interactions - optional interactions to be applied on state
135+
* @param signal - allows to communicate with a DOM request (such as Fetch) and abort it
136+
* @param state - uses specified state to read contract state (overrides reading state from the cache)
137+
*/
131138
readStateFor(
132139
sortKey: string,
133140
interactions: GQLNodeInterface[],
134-
signal?: AbortSignal
141+
signal?: AbortSignal,
142+
state?: SortKeyCacheResult<EvalStateResult<State>>
135143
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;
136144

137145
/**

src/contract/HandlerBasedContract.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
149149
async readState(
150150
sortKeyOrBlockHeight?: string | number,
151151
interactions?: GQLNodeInterface[],
152-
signal?: AbortSignal
152+
signal?: AbortSignal,
153+
state?: SortKeyCacheResult<EvalStateResult<State>>
153154
): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
154155
this.logger.info('Read state for', {
155156
contractTxId: this._contractTxId,
@@ -181,7 +182,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
181182
sortKey,
182183
false,
183184
interactions,
184-
signal
185+
signal,
186+
state
185187
);
186188
this.logger.info('Execution Context', {
187189
srcTxId: executionContext.contractDefinition?.srcTxId,
@@ -221,9 +223,10 @@ export class HandlerBasedContract<State> implements Contract<State> {
221223
async readStateFor(
222224
sortKey: string,
223225
interactions: GQLNodeInterface[],
224-
signal?: AbortSignal
226+
signal?: AbortSignal,
227+
state?: SortKeyCacheResult<EvalStateResult<State>>
225228
): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
226-
return this.readState(sortKey, interactions, signal);
229+
return this.readState(sortKey, interactions, signal, state);
227230
}
228231

229232
async readStateBatch(
@@ -635,7 +638,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
635638
upToSortKey?: string,
636639
forceDefinitionLoad = false,
637640
interactions?: GQLNodeInterface[],
638-
signal?: AbortSignal
641+
signal?: AbortSignal,
642+
state?: SortKeyCacheResult<EvalStateResult<State>>
639643
): Promise<ExecutionContext<State, HandlerApi<State>>> {
640644
const { definitionLoader, interactionsLoader, stateEvaluator } = this.warp;
641645
let cachedState: SortKeyCacheResult<EvalStateResult<State>>;
@@ -646,7 +650,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
646650
EvalStateResult<State>
647651
>;
648652
}
649-
cachedState = cachedState || (await stateEvaluator.latestAvailableState<State>(contractTxId, upToSortKey));
653+
cachedState = state || cachedState || (await stateEvaluator.latestAvailableState<State>(contractTxId, upToSortKey));
650654
if (upToSortKey && this.evaluationOptions().strictSortKey && cachedState?.sortKey != upToSortKey) {
651655
throw new Error(`State not cached at the exact required ${upToSortKey} sortKey`);
652656
}

0 commit comments

Comments
 (0)