Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
feat(blockHeight): fetch the most recent interactions sortKey for a b…
Browse files Browse the repository at this point in the history
…lockHeight and provide that on viewState
  • Loading branch information
dtfiedler committed Mar 1, 2024
1 parent 38c8b4a commit c6c1881
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/routes/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,12 @@ export async function contractReservedHandler(ctx: KoaContext) {

const queryParamsCastedToNumbers = ['qty', 'years', 'height'];
export async function contractReadInteractionHandler(ctx: KoaContext) {
const { warp, logger: _logger, sortKey: requestedSortKey } = ctx.state;
const {
warp,
logger: _logger,
sortKey: requestedSortKey,
blockHeight: requestedBlockHeight,
} = ctx.state;
const { contractTxId, functionName } = ctx.params;
const { query: input } = ctx.request;

Expand All @@ -550,7 +555,20 @@ export async function contractReadInteractionHandler(ctx: KoaContext) {
functionName,
});

// TODO: compute sortKey from blockHeight when provided
let evaluateWithSortKey = requestedSortKey;
if (!requestedSortKey && requestedBlockHeight) {
const { sortKey } = await getContractState({
contractTxId,
warp,
logger,
blockHeight: requestedBlockHeight,
});
logger.info('Using sort key from block height', {
blockHeight: requestedBlockHeight,
sortKey,
});
evaluateWithSortKey = sortKey;
}

const parsedInput = Object.entries(input).reduce(
(parsedParams: { [x: string]: any }, [key, value]) => {

Check warning on line 574 in src/routes/contract.ts

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
Expand All @@ -563,6 +581,10 @@ export async function contractReadInteractionHandler(ctx: KoaContext) {
parsedParams[key] = +value;
return parsedParams;
}
// exclude sortKey and blockHeight from input as they are used to evaluate the contract state
if (key === 'sortKey' || key === 'blockHeight') {
return parsedParams;
}
parsedParams[key] = value;
return parsedParams;
},
Expand All @@ -573,15 +595,15 @@ export async function contractReadInteractionHandler(ctx: KoaContext) {
contractTxId,
warp,
logger,
sortKey: requestedSortKey,
sortKey: evaluateWithSortKey,
functionName,
input: parsedInput,
});

ctx.body = {
contractTxId,
result,
sortKey: requestedSortKey,
sortKey: evaluateWithSortKey,
evaluationOptions,
};
}
44 changes: 44 additions & 0 deletions tests/integration/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,50 @@ describe('Integration tests', () => {
});
});

describe('/:contractTxId/read/:readInteraction', () => {
it('should return the read interaction for the provided contract and read interaction id', async () => {
const { status, data } = await axios.get(
`/v1/contract/${id}/read/priceForInteraction`,
);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, result } = data;
expect(contractTxId).to.equal(id);
expect(result).not.to.be.undefined;
});

it('should return a 400 for an invalid read interaction id', async () => {
const { status } = await axios.get(
`/v1/contract/${id}/read/non-existent-read-api`,
);
expect(status).to.equal(400);
});

it('should properly evaluate state for a read interaction at a provided sortKey', async () => {
const { status, data } = await axios.get(
`/v1/contract/${id}/read/priceForInteraction?sortKey=${contractInteractions[0].sortKey}`,
);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, sortKey, result } = data;
expect(contractTxId).to.equal(id);
expect(result).not.to.be.undefined;
expect(sortKey).not.be.undefined;
});

it('should properly evaluate state for a read interaction at a provided block height', async () => {
const { status, data } = await axios.get(
`/v1/contract/${id}/read/priceForInteraction?blockHeight=${contractInteractions[0].height}`,
);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, sortKey, result } = data;
expect(contractTxId).to.equal(id);
expect(result).not.to.be.undefined;
expect(sortKey).not.be.undefined;
});
});

describe('/:contractTxId/state/:nestedPath', () => {
for (const nestedPath of [
'owner',
Expand Down

0 comments on commit c6c1881

Please sign in to comment.