Skip to content

Commit

Permalink
Merge pull request #3 from decentldotland/dev
Browse files Browse the repository at this point in the history
feat: v0.0.4 -- resolved MEM named functions
  • Loading branch information
charmful0x authored Jan 24, 2024
2 parents 7cea139 + c573b6d commit c8123ff
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
11 changes: 11 additions & 0 deletions examples/node/named.js
Original file line number Diff line number Diff line change
@@ -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();
3 changes: 2 additions & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "ts-example",
"scripts": {
"start": "node ./index.js"
"start": "node ./index.js",
"named": "node ./named.js"
},
"dependencies": {
"mem-sdk": "file:../..",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 9 additions & 1 deletion src/utils/base.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -7,6 +7,10 @@ interface deployValues {

export abstract class Base {
async readFunction(id: string): Promise<any> {

if (id.endsWith(".mem")) {
id = await resolveMemName(id);
}
const url = `https://api.mem.tech/api/state/${id}`;

const headers = {
Expand All @@ -26,6 +30,10 @@ export abstract class Base {
id: string,
inputs: Inputs
): Promise<MEMResponseObject | any> {

if (id.endsWith(".mem")) {
id = await resolveMemName(id);
}
const url = `https://api.mem.tech/api/transactions`;
// Set the headers for the request
const headers = {
Expand Down
25 changes: 25 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,28 @@ export function generateFunctionFormat(sc: string, state: string): any {

return { dataTx, tags };
}

export async function resolveMemName(funcName: string): Promise<any> {
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;
}
}

0 comments on commit c8123ff

Please sign in to comment.