From a7961966622804434dddffd894b1784ff3a4c2f6 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 24 Jan 2024 22:18:03 +0100 Subject: [PATCH 1/5] feat: resolve MEM named functions to ID --- src/utils/base.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utils/base.ts b/src/utils/base.ts index 83182e4..3879da5 100644 --- a/src/utils/base.ts +++ b/src/utils/base.ts @@ -1,4 +1,4 @@ -import { generateFunctionFormat, TXS_BROADCASTER_URL } from "./utils"; +import { generateFunctionFormat, resolveMemName, TXS_BROADCASTER_URL } from "./utils"; import { Inputs, MEMResponseObject, state } from "../types"; interface deployValues { @@ -7,6 +7,10 @@ interface deployValues { export abstract class Base { async readFunction(id: string): Promise { + + if (id.endsWith(".mem")) { + id = await resolveMemName(id); + } const url = `https://api.mem.tech/api/state/${id}`; const headers = { @@ -26,6 +30,10 @@ export abstract class Base { id: string, inputs: Inputs ): Promise { + + if (id.endsWith(".mem")) { + id = await resolveMemName(id); + } const url = `https://api.mem.tech/api/transactions`; // Set the headers for the request const headers = { From bdcff57d185046f493233c2f84304ffd33543209 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 24 Jan 2024 22:18:42 +0100 Subject: [PATCH 2/5] feat: MEM named functions resolver --- src/utils/utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index fdd15a2..e97f6fb 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -33,3 +33,28 @@ export function generateFunctionFormat(sc: string, state: string): any { return { dataTx, tags }; } + +export async function resolveMemName(funcName: string): Promise { + try { + const name: string = funcName.split(".mem")[0]; + + const headers = { + "Content-Type": "application/json", + }; + + const response = await fetch( + "https://api.mem.tech/api/state/GJn1h75nIAyMW5XWgzraL-Ldxr2Zb38WlLEVwk6CBDs", + { headers }, + ); + + if (response.ok) { + const data = (await response.json())?.names; + if (name in data) { + return data[name] as any; + } + } + throw new Error(response.statusText); + } catch (error) { + return null; + } +} From c3062b0e6a48a78675514c0f3fd2b278f60d5bf7 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 24 Jan 2024 22:19:07 +0100 Subject: [PATCH 3/5] feat: v0.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da9f1b3..aea265b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mem-sdk", - "version": "0.0.3", + "version": "0.0.4", "description": "JS SDK for MEM Serverless Functions", "main": "dist/index.cjs.js", "module": "dist/index.es.js", From 042abb5eff75720f4283401047f0a60e8bc566dc Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 24 Jan 2024 22:20:15 +0100 Subject: [PATCH 4/5] chore: add named function example --- examples/node/named.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 examples/node/named.js diff --git a/examples/node/named.js b/examples/node/named.js new file mode 100644 index 0000000..b98faae --- /dev/null +++ b/examples/node/named.js @@ -0,0 +1,11 @@ +const { Mem } = require("mem-sdk"); + +const ansFunctionAddress = "ans.mem"; + +async function runExampleMem() { + const mem = new Mem(); + const result = (await mem.read(ansFunctionAddress)); + return console.log(result); +} + +runExampleMem(); From c573b6d3d393d1840b4948fe944f86248ec3d064 Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 24 Jan 2024 22:20:36 +0100 Subject: [PATCH 5/5] chore: add named function example --- examples/node/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/node/package.json b/examples/node/package.json index 14e049f..387938b 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -1,7 +1,8 @@ { "name": "ts-example", "scripts": { - "start": "node ./index.js" + "start": "node ./index.js", + "named": "node ./named.js" }, "dependencies": { "mem-sdk": "file:../..",