Skip to content

Commit

Permalink
Merge pull request #1 from decentldotland/dev
Browse files Browse the repository at this point in the history
feat v0.0.2: terminology refactoring
  • Loading branch information
charmful0x authored Nov 15, 2023
2 parents 46ce851 + 0d89379 commit 6f6ae53
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 60 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 19 additions & 19 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -39,47 +39,47 @@ 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);
}
});

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);
}
Expand Down
44 changes: 22 additions & 22 deletions src/utils/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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];
Expand All @@ -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 [];
}
Expand Down

0 comments on commit 6f6ae53

Please sign in to comment.