Introduction to N1TS
Welcome to N1TS or NTS (N1 TypeScript), a powerful platform for building smart contracts using TypeScript on the N1 network.
What is N1TS?
N1TS is a comprehensive TypeScript development environment for building decentralized applications on the N1 blockchain. You use familiar TypeScript syntax to write smart contracts (called ‘apps’ in N1 terminology) that run in a secure, sandboxed environment with automatic state management and built-in token operations.
Under the hood, N1TS provides:
- TypeScript Compilation: Automatic compilation and bundling of your TypeScript code
- Secure Execution: Code runs in a secure, isolated environment
- State Management: Automatic persistence and retrieval of application state
- Token System: Native support for minting, transferring, and managing tokens
- Cross-Cluster Operations: Send tokens and data across different network clusters
Architecture Overview
N1TS provides a complete TypeScript-to-blockchain execution environment:
Your TypeScript Code
↓
N1TS Compiler
↓
Secure Execution
↓
Blockchain State Updates
Main Features
Feature | Description |
---|---|
TypeScript First | Write apps in TypeScript with full type safety and modern language features. No need to learn blockchain-specific languages. |
Automatic State Management | Properties assigned to 'this' are automatically persisted to blockchain state. Use NMap for typed collections. |
Built-in Token Operations | Native support for minting tokens, transferring between addresses, and cross-cluster operations with BigInt precision. |
Secure Execution | Apps run in a secure, isolated environment with resource limits and timeout protection for reliability. |
Cross-Cluster Communication | Send tokens and trigger actions across different network clusters with built-in tracking and validation. |
Comprehensive Validation | Extensive checks for balance validation, permission verification, and transaction safety. |
Development Tools | Built-in testing framework, compilation tools, and debugging utilities for rapid development. |
Admin Controls | Flexible permission system with protected fields and admin-only operations for secure app management. |
Core Concepts
NApp Base Class
All N1TS applications extend the NApp
base class, which provides:
- Automatic State Management: Properties are automatically persisted to blockchain state
- Built-in Methods: Access to blockchain functions and transaction context
- Security: Protected execution environment with validation
- Transaction Context: Access to signer, app admin, timestamp, and other context
Available Functions
N1TS provides these core functions for blockchain operations:
nread(fieldId, appId?)
- Read data from storagenwrite(fieldId, value, tag?, secondary_tag?)
- Write data to storagenmint(totalSupply, admin, meta)
- Create new tokensnmintEdit(mint, freeSupply, admin, meta)
- Edit existing tokensntransfer(origin, destination, mint, amount, args?)
- Transfer tokensntransferToCluster(origin, clusterId, mint, amount, args?)
- Cross-cluster transferlog(...args)
- Logging function
Context Methods
Access transaction and app context through these methods:
this.signer()
- Get transaction signer addressthis.appAdmin()
- Get app admin addressthis.appId()
- Get current app IDthis.time()
- Get current timestamp
Critical: Transaction Actions vs State Reading
Transaction Actions (called by users) are for state changes and cannot return values. Return statements in transaction actions are ignored.
State Reading is done through separate queries using the client SDK, not through actions.
// ❌ Wrong: Transaction action with return value
createToken(): string {
const mintId = nmint(BigInt(1000), this.signer(), "Token");
return mintId; // This is ignored in transactions!
}
// ✅ Correct: Transaction action for state change only
createToken(): void {
nmint(BigInt(1000), this.signer(), "Token");
this.log("Token creation initiated");
}
// ✅ Correct: Read state via client queries, not actions
// Use client.readField() or app state queries externally
Critical: Class Property Initialization
You CANNOT initialize class properties directly. Properties must be declared and then initialized in an init()
method.
// ❌ Wrong: Direct property initialization (except NMap)
class MyApp extends NApp {
counter: number = 0; // This doesn't work!
users: NMap<User> = new NMap<User>(this, 'users'); // ✅ This is correct for NMap
}
// ✅ Correct: Initialize properties properly
class MyApp extends NApp {
counter: number;
users: NMap<User> = new NMap<User>(this, 'users'); // NMap initialized directly
init(): void {
this.counter = 0; // Regular properties in init()
}
}
Execution Model
N1TS apps run with these characteristics:
- Resource Limits: Built-in constraints for security and performance
- Timeout Protection: Execution time limits prevent infinite loops
- Isolated Context: Each transaction runs in complete isolation
- Atomic Operations: All changes succeed together or fail together - no partial writes
Transaction Lifecycle
- Transaction Received: Network receives signed transaction
- Validation: Signature and permission validation
- Execution: Method executed in secure, isolated environment
- State Commit: All changes written to blockchain state atomically (or all reverted on failure)
- Response: Success/failure response with transaction logs
Token System
N1TS features a sophisticated native token system:
Key Characteristics
- BigInt Precision: All token amounts use BigInt for arbitrary precision
- Admin-Controlled: Every token has an admin who can modify properties
- Cluster-Aware: Tokens can be transferred across network clusters
- Validation-Heavy: Extensive checks prevent invalid operations
- Event-Logged: All operations are comprehensively logged
Token Operations
- Minting: Create new tokens with
nmint(totalSupply, admin, meta)
- Transfers: Move tokens with
ntransfer(origin, destination, mint, amount, args?)
- Cross-Cluster: Bridge tokens with
ntransferToCluster(origin, clusterId, mint, amount, args?)
- Admin Functions: Edit tokens with
nmintEdit(mint, freeSupply, admin, meta)
Token amounts in N1TS always use BigInt to handle arbitrary precision. Convert strings to BigInt before token operations.
Transaction Atomicity: All operations in a transaction either succeed completely or fail completely. If any operation fails, no state changes are saved - your app state remains unchanged.
Data Management
Automatic Persistence
Properties assigned to this
are automatically saved:
class MyApp extends NApp {
counter: number; // ❌ Cannot initialize here: counter: number = 0;
init(): void {
this.counter = 0; // ✅ Initialize in init() method
}
increment(): void { // ✅ No return value for transaction actions
this.counter++; // Automatically persisted
this.log("Counter incremented to:", this.counter);
}
}
NMap Collections
For key-value data, use typed NMap
collections:
class UserManager extends NApp {
users: NMap<UserProfile> = new NMap<UserProfile>(this, 'users');
init(): void {
// Initialize other properties here if needed
}
createUser(userId: string, profile: UserProfile): void {
this.users.set(userId, profile, 'user_creation', userId);
}
}
Pre-Requisite Knowledge
To get the most out of N1TS development:
- TypeScript/JavaScript: Solid understanding of modern TypeScript features
- Blockchain Concepts: Basic understanding of smart contracts and transactions
- Async Programming: Familiarity with async/await and Promises
- State Management: Understanding of application state and persistence
Development Workflow
- Setup: Install N1TS compiler and SDK packages
- Write: Create TypeScript app extending NApp class
- Test: Use built-in testing utilities and mock environments
- Compile: Generate JavaScript and IDL using N1TS compiler
- Deploy: Deploy to N1 network using SDK tools
- Interact: Call app methods using client SDK
Security Model
N1TS provides multiple layers of security:
Permission System
- Protected Fields: Fields starting with
_
require admin access - Token Admin Rights: Token admins can transfer any tokens of that type
- App Admin Controls: Special privileges within app context
Validation Checks
- Balance Verification: Transfers require sufficient token balance
- Permission Validation: Third-party operations require proper authorization
- Input Sanitization: All inputs are validated before processing
Join our Community
If you have questions or want to contribute:
- Discord - Join our developer community
- GitHub - Contribute to the codebase
- Documentation - Explore comprehensive guides and examples
Get involved in the N1TS ecosystem and help us build the future of decentralized applications using TypeScript!