From c03e7d360018d44a5847d72311134bf0314ef3a4 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 15 Nov 2023 15:16:53 +0100 Subject: [PATCH 1/4] chore: terminology refactoring --- src/utils/cache.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/utils/cache.js b/src/utils/cache.js index 1d67a2b..aa79a06 100644 --- a/src/utils/cache.js +++ b/src/utils/cache.js @@ -5,27 +5,27 @@ import axios from "axios"; const cache = new NodeCache(); -export async function saveContract(src, state) { +export async function saveFunction(src, state) { try { - const contract_id = await guidGenerator(); - const contract = { src, state }; + const function_id = await guidGenerator(); + const func = { src, state }; - cache.set(contract_id, contract); + cache.set(function_id, func); - return { contract_id }; + return { function_id }; } catch (error) { console.log(error); - return { contract_id: undefined }; + return { function_id: undefined }; } } -export async function writeContract(contract_id, input) { +export async function writeFunction(function_id, input) { try { - if (!cache.has(contract_id)) { - return { error: `${contract_id} not deployed on testnet` }; + if (!cache.has(function_id)) { + return { error: `${function_id} not deployed on testnet` }; } - const { src, state, exmContext } = cache.get(contract_id); + const { src, state, exmContext } = cache.get(function_id); const body = { contractType: 0, @@ -36,9 +36,9 @@ export async function writeContract(contract_id, input) { }; const tx = await axios.post(MEM_TESTNET_URL, body); - const newCntx = await updateExmCntx(contract_id, tx.data.exmContext); + const newCntx = await updateExmCntx(function_id, tx.data.exmContext); - cache.set(contract_id, { + cache.set(function_id, { src: src, state: JSON.stringify(tx.data.state), exmContext: newCntx, @@ -50,9 +50,9 @@ export async function writeContract(contract_id, input) { } } -async function updateExmCntx(contract_id, newCntx) { +async function updateExmCntx(function_id, newCntx) { try { - const oldCntx = JSON.parse(cache.get(contract_id)?.exmContext); + const oldCntx = JSON.parse(cache.get(function_id)?.exmContext); for (const req in newCntx.requests) { oldCntx.requests[req] = newCntx.requests[req]; @@ -68,23 +68,23 @@ async function updateExmCntx(contract_id, newCntx) { return JSON.stringify(newCntx); } } -export async function getContract(contract_id) { +export async function getFunction(function_id) { try { - if (!cache.has(contract_id)) { - return { error: `${contract_id} not deployed on testnet` }; + if (!cache.has(function_id)) { + return { error: `${function_id} not deployed on testnet` }; } - const contract = cache.get(contract_id); - return contract; + const func = cache.get(function_id); + return func; } catch (error) { console.log(error); } } -export async function getAllContracts() { +export async function getAllFunctions() { try { - const contracts = cache.keys(); - return contracts; + const functions = cache.keys(); + return functions; } catch (error) { return []; } From 60e45d1fc6852b7afeddf7f8323a96481bbbbae1 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 15 Nov 2023 15:17:19 +0100 Subject: [PATCH 2/4] chore: terminology refactoring --- src/api.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/api.js b/src/api.js index 9edd4de..1a0f4ce 100644 --- a/src/api.js +++ b/src/api.js @@ -3,10 +3,10 @@ import bodyParser from "body-parser"; import cors from "cors"; import { - saveContract, - writeContract, - getContract, - getAllContracts, + saveFunction, + writeFunction, + getFunction, + getAllFunctions, } from "./utils/cache.js"; const app = express(); @@ -39,8 +39,8 @@ app.use((err, req, res, next) => { app.post("/deploy", async (req, res) => { try { const { src, state } = req.body; - const contract_id = await saveContract(src, state); - res.json(contract_id); + const function_id = await saveFunction(src, state); + res.json(function_id); } catch (error) { console.log(error); } @@ -48,38 +48,38 @@ app.post("/deploy", async (req, res) => { app.post("/write", async (req, res) => { try { - const { contract_id, input } = req.body; - const tx = await writeContract(contract_id, input); + const { function_id, input } = req.body; + const tx = await writeFunction(function_id, input); res.json(tx); } catch (error) { console.log(error); } }); -app.get("/state/:contract_id", async (req, res) => { +app.get("/state/:function_id", async (req, res) => { try { - const { contract_id } = req.params; - const contract = await getContract(contract_id); - res.json(JSON.parse(contract.state)); + const { function_id } = req.params; + const func = await getFunction(function_id); + res.json(JSON.parse(func.state)); } catch (error) { console.log(error); } }); -app.get("/data/contract/:contract_id", async (req, res) => { +app.get("/data/function/:function_id", async (req, res) => { try { - const { contract_id } = req.params; - const contract = await getContract(contract_id); - res.json(contract); + const { function_id } = req.params; + const func = await getFunction(function_id); + res.json(func); } catch (error) { console.log(error); } }); -app.get("/contracts", async (req, res) => { +app.get("/functions", async (req, res) => { try { - const contracts = await getAllContracts(); - res.json(contracts); + const functions = await getAllFunctions(); + res.json(functions); } catch (error) { console.log(error); } From b51791614761bfcbd98ea9ee9ffd83756987ae0e Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 15 Nov 2023 15:17:46 +0100 Subject: [PATCH 3/4] docs: update endpoints --- README.md | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 89aa72d..f5c2931 100644 --- a/README.md +++ b/README.md @@ -40,18 +40,18 @@ const TESTNET_ENDPOINT = "https://endpoint.something/deploy"; async function deploy() { try { - const sourceCode = readFileSync("./func.js", { encoding: "utf8" }); // the src code of the function/contract - const initState = readFileSync("./func.json", { encoding: "utf8" }); // the JSON initial function state + const sourceCode = readFileSync("./func.js", { encoding: "utf8" }); // the src code of the function + const initState = readFileSync("./state.json", { encoding: "utf8" }); // the JSON initial function state const body = { src: sourceCode, state: initState, }; - const contract_id = (await axios.post(TESTNET_ENDPOINT, body))?.data - ?.contract_id; - console.log(contract_id); - return contract_id; + const function_id = (await axios.post(TESTNET_ENDPOINT, body))?.data + ?.function_id; + console.log(function_id); + return function_id; } catch (error) { console.log(error); } @@ -67,11 +67,11 @@ const TESTNET_ENDPOINT = "https://endpoint.something/write"; async function write() { try { const input = '{"function": "something"}'; // example: '{"function": "increment"}' - const contract_id = "your_contract_id"; // example: "ed0afed6-311f-be78-1063-8518bab3e29a" + const function_id = "your_function_id"; const body = { input, - contract_id, + function_id, }; const result = (await axios.post(TESTNET_ENDPOINT, body))?.data; console.log(result); @@ -81,21 +81,17 @@ async function write() { } } ``` -### Get contract state +### Get function state -- `GET /state/:contract_id` +- `GET /state/:function_id` -### Get contract data (state, source code, and exmContext) +### Get function data (state, source code, and exmContext) -- `GET /data/contract/:contract_id` +- `GET /data/function/:function_id` -### Get all testnet contracts +### Get all testnet deployed functions -- `GET /contracts` +- `GET /functions` ## License This repository is licensed under the [MIT License](./LICENSE) - - - - From 0d893790dc6b37404057d73e60ee329c83b6ec83 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 15 Nov 2023 15:18:12 +0100 Subject: [PATCH 4/4] feat: v0.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c90d183..88e8db9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mem-carbon-testnet", - "version": "0.0.1", + "version": "0.0.2", "description": "MEM Carbon is a testnet network with a temporal states storage", "type": "module", "main": "./src/api.js",