-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ideal-lab5/feat/8-murmurjs-implement-api-…
…wrapper feat: wrap api endpoints
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/dist | ||
/node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |