The Namespace Object
The Koii Namespace Wrapper is a utility that helps Koii tasks interact with the network effortlessly. It handles task data, blockchain interactions, file operations, and node communication, so developers can focus on their core logic.
Core Features
1. State Management
Store and retrieve task data reliably:
// Store data
await namespaceWrapper.storeSet("key", "value");
// Retrieve data
const value = await namespaceWrapper.storeGet("key");
2. Blockchain Integration
Securely interact with the Koii Network:
// Send a transaction
const tx = await namespaceWrapper.sendTransaction(
senderAccount,
receiverAccount,
amount,
);
// Get network status
const nodes = await namespaceWrapper.getNodes();
3. File System Operations
Handle files consistently across environments:
// Create a directory
await namespaceWrapper.fs("mkdir", "data");
// Write a file
await namespaceWrapper.fs("writeFile", "data/config.json", "{}");
// Read a file
const content = await namespaceWrapper.fs("readFile", "data/config.json");
4. Task Management
Manage task state and rounds:
// Get current round
const round = await namespaceWrapper.getRound();
// Submit task results
await namespaceWrapper.checkSubmissionAndUpdateRound(submission, round);
Benefits of Using the Namespace Wrapper
-
Simplified Development
- No need to handle low-level infrastructure
- Consistent APIs across different operations
- Built-in error handling and security
-
Better Code Organization
- Clear separation of concerns
- Standardized interfaces
- Modular architecture
-
Enhanced Security
- Secure blockchain interactions
- Protected file system access
- Safe state management
-
Improved Reliability
- Automatic error handling
- Built-in retries for network operations
- Data persistence
Common Use Cases
1. Data Storage
- Typescript
- JavaScript
async function saveUserData(userId: string, data: object) {
try {
// Store user data
await namespaceWrapper.storeSet(`user_${userId}`, JSON.stringify(data));
// Verify storage
const stored = await namespaceWrapper.storeGet(`user_${userId}`);
return JSON.parse(stored);
} catch (error) {
console.error("Failed to save user data:", error);
throw error;
}
}
async function saveUserData(userId, data) {
try {
// Store user data
await namespaceWrapper.storeSet(`user_${userId}`, JSON.stringify(data));
// Verify storage
const stored = await namespaceWrapper.storeGet(`user_${userId}`);
return JSON.parse(stored);
} catch (error) {
console.error("Failed to save user data:", error);
throw error;
}
}
2. Task Submissions
- Typescript
- JavaScript
async function submitTaskResult(submission: string) {
try {
const round = await namespaceWrapper.getRound();
await namespaceWrapper.checkSubmissionAndUpdateRound(submission, round);
console.log("Task result submitted for round:", round);
} catch (error) {
console.error("Submission failed:", error);
throw error;
}
}
async function submitTaskResult(submission) {
try {
const round = await namespaceWrapper.getRound();
await namespaceWrapper.checkSubmissionAndUpdateRound(submission, round);
console.log("Task result submitted for round:", round);
} catch (error) {
console.error("Submission failed:", error);
throw error;
}
}
3. File Management
- Typescript
- JavaScript
async function processDataFile(filename: string) {
try {
// Read input file
const data = await namespaceWrapper.fs("readFile", filename, "utf8");
// Process data
const processed = await processData(data);
// Save results
await namespaceWrapper.fs("writeFile", `processed_${filename}`, processed);
} catch (error) {
console.error("File processing failed:", error);
throw error;
}
}
async function processDataFile(filename: string) {
try {
// Read input file
const data = await namespaceWrapper.fs("readFile", filename, "utf8");
// Process data
const processed = await processData(data);
// Save results
await namespaceWrapper.fs("writeFile", `processed_${filename}`, processed);
} catch (error) {
console.error("File processing failed:", error);
throw error;
}
}
Best Practices
- Always Use Error Handling
try {
await namespaceWrapper.storeSet("key", "value");
} catch (error) {
console.error("Operation failed:", error);
// Implement appropriate error recovery
}
- Validate Input Data
- Typescript
- JavaScript
function validateData(data: any) {
if (!data || typeof data !== "object") {
throw new Error("Invalid data format");
}
// Add more validation as needed
}
function validateData(data) {
if (!data || typeof data !== "object") {
throw new Error("Invalid data format");
}
// Add more validation as needed
}
- Log Important Operations
await namespaceWrapper.logger(
"log",
"Task completed successfully",
"TaskCompletion",
);
Next Steps
To learn more about specific features, check out these guides:
- Installation Guide - Get started with the wrapper.
- Namespace Wrapper Methods - Interact with tasks and the blockchain.
- Environment Variables - Import and use Environment Variables.
- REST APIs - Build HTTP endpoints.
- Database Operations - Learn about data storage.
- File System - Handle files and directories.
- Blockchain/Transaction Operations - Work with blockchain and transaction operations.
- Task Status - Get task state information with namespace methods.
- Network/Task Handling - Manage network data and tasks.
- Audit and Distribution - Manage network data and tasks.