Skip to content

Commit

Permalink
Merge pull request #26 from dOrgTech/feat/aci-backend
Browse files Browse the repository at this point in the history
Backend for ACI Changes
  • Loading branch information
EightRice authored Nov 6, 2024
2 parents 6056e4f + 36689d5 commit 06d5e0b
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ yarn-error.log*
.idea
.cosine
yarn.lock
bun.lockb
test.js
.vscode
.vscode
24 changes: 24 additions & 0 deletions components/aci/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { catchAsync } = require("../../services/response.util");
const { getContractEndpoints } = require("../../services/aci.service");

const getContractEndpointsController = catchAsync(async(req, response) => {
const {network} = req.body;
const contractId = req.params.contract_id;
if(!contractId) throw new Error("Contract ID is required");

let [endpoints, error] = await getContractEndpoints(network, contractId)
if(error) throw new Error(error)

if(endpoints){
endpoints = {
...endpoints,
operations: endpoints.children.map(x => x.name)
}
}

return response.json(endpoints);
})

module.exports = {
getContractEndpointsController
}
42 changes: 42 additions & 0 deletions routes/aci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
const { getContractEndpointsController } = require("../components/aci");

/**
* TEST Contracts
*
* KT1MzN5jLkbbq9P6WEFmTffUrYtK8niZavzH
* KT1VG3ynsnyxFGzw9mdBwYnyZAF8HZvqnNkw
*
*/

const aciRoutes = express.Router();

/**
* @swagger
* /aci/{contract_id}:
* post:
* summary: Get contract endpoints
* tags: [ACI]
* parameters:
* - in: path
* name: contract_id
* required: true
* description: The ID of the contract
* schema:
* type: string
* requestBody:
* required: false
* content:
* application/json:
* schema:
* type: object
* properties:
* network:
* type: string
* responses:
* 200:
* description: Contract ACI Endpoints
*/
aciRoutes.post('/aci/:contract_id', getContractEndpointsController)

module.exports = aciRoutes;
43 changes: 43 additions & 0 deletions routes/aci.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const request = require("supertest");
const express = require("express");
const aciRoutes = require("./aci");

const app = express();
app.use(express.json());
app.use("/", aciRoutes);

const contractIds = [
"KT1MzN5jLkbbq9P6WEFmTffUrYtK8niZavzH",
"KT1VG3ynsnyxFGzw9mdBwYnyZAF8HZvqnNkw",
"I_AM_INVALID_ID"
]

describe("ACI Routes", () => {
it("should return bad status on invalid address ", async () => {
await request(app)
.post(`/aci/${contractIds[2]}`)
.expect(400)
.expect("Content-Type", /json/)
});
it("should return valid status on valid address ", async () => {
await request(app)
.post(`/aci/${contractIds[0]}`)
.send({network:"ghostnet"})
.expect(200)
.expect("Content-Type", /json/)
});
it("should return valid JSON on valid address", async () => {
const res = await request(app)
.post(`/aci/${contractIds[0]}`)
.send({network:"ghostnet"})
.expect(200)
.expect("Content-Type", /json/)

expect(res.body).toHaveProperty("counter");
expect(res.body).toHaveProperty("name");
expect(res.body).toHaveProperty("type");
expect(res.body).toHaveProperty("children");
expect(res.body).toHaveProperty("operations");
});
});

1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ app.use(require("./routes/daos"));
app.use(require("./routes/polls"));
app.use(require("./routes/tokens"));
app.use(require("./routes/choices"));
app.use(require("./routes/aci"));

app.listen(port, async () => {
// perform a database connection when server starts
Expand Down
Loading

0 comments on commit 06d5e0b

Please sign in to comment.