NTS Interface class
The NTSInterface
class provides low-level access to the N1 network. It handles direct communication with the NTS backend for transaction processing, data operations, and app management. This class serves as the foundation for the higher-level NAppClient
.
You can access the NTSInterface class directly from the NAppClient
class.
Usage
import { NAppClient } from "@n1xyz/nts-sdk";
// Initialize the client with required parameters
const client = new NAppClient(
"https://network-rpc-url.com", // NTS network URL
"your-app-id", // App ID
appIdl, // App IDL (Interface Description Language)
walletPublicKey, // Wallet public key as Uint8Array
sessionPublicKey, // Optional: Session public key as Uint8Array
signMessageWithSessionKey // Optional: Function to sign messages with session key
);
// Access the NTSInterface class
const nts = client.ntsInterface;
Core Features
Transaction Management
// Send a transaction
const result = await nts.sendTransaction(
transaction, // Transaction object
signature, // Uint8Array signature
publicKey, // Uint8Array public key
sessionKey, // Optional: Uint8Array session key
clusterId // Optional: cluster ID (defaults to 'ts')
);
// Get transaction by ID
const tx = await nts.getTransactionById("transaction-id");
// List transactions (with optional pagination)
const transactions = await nts.getTransactions();
const moreTx = await nts.getTransactions("last-tx-id");
App Management
// Get app metadata
const metadata = await nts.getAppMetadata("app-id");
// Get app code
const code = await nts.getAppCode("app-id");
// Get app IDL
const idl = await nts.getAppIdl("app-id");
// List apps (with optional pagination)
const apps = await nts.getApps();
const moreApps = await nts.getApps("last-app-id");
// Get apps by admin
const adminApps = await nts.getAppIdsByAdmin("admin-key");
// Get cluster apps
const clusterApps = await nts.getClusterApps();
Data Operations
// Read field data
const value = await nts.readData("app-id", "field-id");
// Get all data for an app
const appData = await nts.getData("app-id");
// Filter by tag
const taggedData = await nts.getData("app-id", "tag");
Token Operations
// Get user balances
const userBalances = await nts.getUserBalances("user-address");
// Get app balances
const appBalances = await nts.getAppBalances();
// List all mints
const mints = await nts.getMints();
Session Management
// Validate session key
const isValid = await nts.validateSessionKey("session-key", "public-key");
Version Information
// Get NTS backend version
const version = await nts.getVersion();
console.log(version.versionName);
Types
The interface exports several TypeScript types for better development experience:
interface TransactionResponse {
transaction_id: string;
created_at: string;
app_id: string;
transaction_data: TransactionData;
transaction_writes: any;
action: string;
caller: string;
success: boolean;
logs: string[];
}
interface AppData {
app_id: string;
field_id: string;
value: any;
tag: string;
}
interface Balance {
mint: string;
balance: string;
appId: string;
appType: "ono" | "cluster";
clusterAppId?: string;
}
Error Handling
The interface includes built-in error handling for all network requests. Each method will throw appropriate errors that should be handled in try-catch blocks:
try {
const data = await nts.readData("app-id", "field-id");
} catch (error) {
console.error("Error reading data:", error);
}
Note: The NTSInterface
class is primarily used internally by the NAppClient
class, which provides a more developer-friendly interface for interacting with NTS apps. For most use cases, you should use the NAppClient
class instead.