Skip to content

Commit

Permalink
add policyIdExists endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgrakkurt committed Jan 17, 2022
1 parent 26a0ece commit fdf8441
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -790,3 +790,27 @@ We recommend querying using payment key hashes (`addr_vkh`) when possible (other
}
```
</details>
<details>
<summary>/multiAsset/policyIdExists</summary>
This endpoint is used to check if given policyIds already exist on chain.

Number of ids need to be in [1, 100]

Input

```js
{
policyIds: Array<string>, // hex encoded policyIds that will be checked
}
```

Output

```js
{
policyIdResults: Array<{
[policyId: string]: boolean
}>
}
```
</details>
7 changes: 7 additions & 0 deletions src/index.ts
Expand Up @@ -42,6 +42,8 @@ import { handleGetTxIO } from "./services/txIO";
import { handleTipStatusGet, handleTipStatusPost } from "./services/tipStatus";
import { handleGetTransactions } from "./services/transactions";

import { handlePolicyIdExists } from "./services/policyIdExists";

import { HealthChecker } from "./HealthChecker";

import { createCertificatesView } from "./Transactions/certificates";
Expand Down Expand Up @@ -378,6 +380,11 @@ const routes: Route[] = [
method: "post",
handler: handleTxStatus(pool),
},
{
path: "/multiAsset/policyIdExists",
method: "post",
handler: handlePolicyIdExists(pool),
},
{
path: "/v2/importerhealthcheck",
method: "get",
Expand Down
42 changes: 42 additions & 0 deletions src/services/policyIdExists.ts
@@ -0,0 +1,42 @@
import { Pool } from "pg";
import { Request, Response } from "express";

export const handlePolicyIdExists =
(pool: Pool) =>
async (req: Request, res: Response): Promise<void> => {
if (!req.body || !req.body.policyIds) {
throw new Error("error, no policyIds informed.");
}
if (!Array.isArray(req.body.policyIds)) {
throw new Error("'policyIds should be an array.");
}

const policyIds: string[] = req.body.policyIds;

if (policyIds.length > 100) {
throw new Error("Max limit of 100 policyIds exceeded.");
}
if (policyIds.length === 0) {
throw new Error("error, at least 1 policyId should be informed.");
}

const result = await pool.query(query, [policyIds]);

const policyIdResults: { [key: string]: boolean } = {};

policyIds.forEach((policyId: string) => {
policyIdResults[policyId] = false;
});

result.rows.forEach((row: any) => {
policyIdResults[row.policy_hex] = true;
});

res.send({
policyIdResults,
});
};

const query = `
SELECT encode(policy, 'hex') as policy_hex FROM multi_asset
WHERE encode(policy, 'hex') = ANY($1);`;

0 comments on commit fdf8441

Please sign in to comment.