Skip to content

Commit

Permalink
WIP - Arbitrary Contract Interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshpw committed May 12, 2024
1 parent 79aa578 commit 3afce82
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ yarn-error.log*
.vscode

.idea
.cosine
.cosine
yarn.lock
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
}
1 change: 0 additions & 1 deletion components/daos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
} = require("../../utils");

const dbo = require("../../db/conn");
const { response } = require("express");
const { getPkhfromPk } = require("@taquito/utils");

const getAllLiteOnlyDAOs = async (req, response) => {
Expand Down
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 @@ -25,6 +25,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 3afce82

Please sign in to comment.