Skip to content

Commit

Permalink
Merge pull request #10 from ideal-lab5/feat/8-murmurjs-implement-api-…
Browse files Browse the repository at this point in the history
…wrapper

feat: wrap api endpoints
  • Loading branch information
juangirini authored Oct 1, 2024
2 parents 2bdb949 + 6ce43d1 commit 62a2c1b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dist
/node_modules
package-lock.json
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@ideallabs/murmur.js",
"version": "0.1.0",
"description": "A wrapper to interact with the Murmur API",
"license": "GPL-3.0",
"repository": "https://github.com/ideal-lab5/murmur.js",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ideal Labs <[email protected]>",
"dependencies": {
"axios": "^1.7.7"
},
"devDependencies": {
"@types/node": "^22.7.4",
"typescript": "^5.6.2"
}
}
81 changes: 81 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import axios, { AxiosInstance } from "axios";

class MurmurClient {
private client: AxiosInstance;

constructor(baseURL: string) {
this.client = axios.create({
baseURL,
headers: {
"Content-Type": "application/json",
},
});
}

/**
* Creates a new wallet with a specified validity period.
*
* @param validity - The number of blocks in which the wallet will be valid.
* @returns A promise that resolves to a string indicating the result of the wallet creation.
*/
async login(username: string, password: string): Promise<string> {
try {
const response = await this.client.post("/login", { username, password });
return response.data;
} catch (error) {
throw new Error(`Login failed: ${error}`);
}
}

/**
* Creates a new wallet with a specified validity period.
*
* @param validity - The number of blocks in which the wallet will be valid.
* @returns A promise that resolves to a string indicating the result of the wallet creation.
*/
async new(validity: number): Promise<string> {
const MAX_U32 = 2 ** 32 - 1;
if (!Number.isInteger(validity)) {
throw new Error("The validity parameter must be an integer.");
}

if (validity < 0 || validity > MAX_U32) {
throw new Error(
`The validity parameter must be within the range of 0 to ${MAX_U32}.`
);
}
try {
const response = await this.client.post("/new", { validity });
return response.data;
} catch (error) {
throw new Error(`New failed: ${error}`);
}
}

/**
* Executes a transaction to send a specified amount of tokens to a destination account.
*
* @param amount - The amount of tokens to send.
* @param to - The destination account.
* @returns A promise that resolves to a string indicating the result of the transaction.
*/
async execute(amount: bigint, to: string): Promise<string> {
const MAX_U128 = BigInt(2 ** 128 - 1);
if (!Number.isInteger(amount)) {
throw new Error("The amount parameter must be an integer.");
}
if (amount < 0 || amount > MAX_U128) {
throw new Error(
`The amount parameter must be within the range of 0 to ${MAX_U128}.`
);
}
try {
const response = await this.client.post("/execute", { amount, to });
return response.data;
} catch (error) {
throw new Error(`Execute failed: ${error}`);
}
}
}

export default MurmurClient;
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}

0 comments on commit 62a2c1b

Please sign in to comment.