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

Commit

Permalink
feat(price): add price endpoint for retrieving costs of interactions …
Browse files Browse the repository at this point in the history
…on contract
  • Loading branch information
dtfiedler committed Nov 13, 2023
1 parent 03c0cd8 commit 244c863
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"start:prod": "yarn build && node dist/app.js",
"build": "yarn clean && yarn && npx tsc --project ./tsconfig.json",
"clean": "rimraf [ node_modules dist cache ]",
"test:docker": "docker compose up --exit-code-from test-runner --build",
"test:integration": "mocha --spec=tests/integration/**.test.ts",
"test:integration:local": "docker compose up arlocal -d ; yarn test:integration $* ; docker compose down -v",
"docker:run": "docker compose up arns-service --build",
Expand Down
8 changes: 8 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ router.get(
return contractReadInteractionHandler(ctx, next);
},
);
router.get(
`/v1/contract/:contractTxId${ARNS_CONTRACT_ID_REGEX}/price`,
(ctx: KoaContext, next: Next) => {
// set params for auction read interaction and then use our generic handler
ctx.params.functionName = 'priceForInteraction';
return contractReadInteractionHandler(ctx, next);
},
);
// generic handler that handles read APIs for any contract function
router.get(
`/v1/contract/:contractTxId${ARNS_CONTRACT_ID_REGEX}/read/:functionName`,
Expand Down
15 changes: 14 additions & 1 deletion src/routes/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,25 @@ export async function contractReadInteractionHandler(
functionName,
});

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

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

View workflow job for this annotation

GitHub Actions / build (lint:check)

Unexpected any. Specify a different type
// parse known integer values
if (typeof value === 'string' && !isNaN(+value)) {
parsedParams[key] = +value;
return parsedParams;
}
parsedParams[key] = value;
return parsedParams;
},
{},
);

const { result, evaluationOptions } = await getContractReadInteraction({
contractTxId,
warp,
logger,
functionName,
input,
input: parsedInput,
});

ctx.body = {
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/arlocal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export function handle(state, action) {
return { state };
}

if (input.function === 'priceForInteraction') {
return { result: { price: input.qty } };
}

throw new ContractError(
`No function supplied or function not recognized: "${input.function}"`,
);
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const axios = axiosPackage.create({
baseURL: serviceURL,
validateStatus: () => true, // don't throw errors
});
describe('PDNS Service Integration tests', () => {
describe('Integration tests', () => {
let ids: string[] = [];
let id: string;
let walletAddress: string;
Expand Down Expand Up @@ -140,6 +140,20 @@ describe('PDNS Service Integration tests', () => {
expect(status).to.equal(404);
});
});
describe('/:contractTxId/price', () => {
it('should properly handle price interaction inputs', async () => {
const { status, data } = await axios.get(
`/v1/contract/${id}/price?qty=100`,
);
expect(status).to.equal(200);
expect(data).to.not.be.undefined;
const { contractTxId, result, evaluationOptions } = data;
expect(contractTxId).to.equal(id);
expect(evaluationOptions).not.to.be.undefined;
expect(result).to.include.keys(['price']);
expect(result.price).to.equal(100);
});
});

describe('/:contractTxId/interactions', () => {
it('should return the contract interactions', async () => {
Expand Down
6 changes: 0 additions & 6 deletions tests/integration/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ export async function mochaGlobalSetup() {
},
}),
src: contractSrcJs,
evaluationManifest: {
evaluationOptions: {
// used for testing query params
throwOnInternalWriteError: true,
},
},
},
true, // disable bundling
);
Expand Down

0 comments on commit 244c863

Please sign in to comment.