MidnightMidnight Setup
Core API Methods
Complete reference for MidnightSetupAPI methods and provider setup
MidnightSetupAPI
The MidnightSetupAPI is the main class for interacting with Midnight Network contracts. It provides methods for deploying, joining, and managing smart contracts.
Core Methods
| Method | Description | Usage |
|---|---|---|
deployContract(providers, contractInstance) | Deploy a new contract | Creates new contract instance |
joinContract(providers, contractInstance, address) | Join existing contract | Connect to deployed contract |
getContractState() | Read contract state | Get current contract data |
getLedgerState() | Read ledger state | Get blockchain data |
Deploy Contract
Deploy a new smart contract to the Midnight Network:
import { MidnightSetupAPI } from '@meshsdk/midnight-setup';
const api = await MidnightSetupAPI.deployContract(providers, contractInstance);
console.log('Contract deployed:', api.deployedContractAddress);Join Contract
Connect to an existing deployed contract:
import { MidnightSetupAPI } from '@meshsdk/midnight-setup';
const contractAddress = "contract_address_here";
const api = await MidnightSetupAPI.joinContract(providers, contractInstance, contractAddress);
console.log('Connected to contract:', contractAddress);Get Contract State
Retrieve the current state of a contract:
const state = await api.getContractState();
console.log('Contract state:', state);Get Ledger State
Access the current ledger state:
const ledgerState = await api.getLedgerState();
console.log('Ledger state:', ledgerState);Provider Setup
Set up providers for Midnight Network:
import { setupProviders } from './lib/providers';
const providers = await setupProviders();
// Returns: MidnightSetupContractProvidersProvider Configuration
const providers = {
fetcher: blockfrostProvider,
submitter: blockfrostProvider,
wallet: laceWallet,
// Additional provider configurations
};Error Handling
Always wrap API calls in try-catch blocks for proper error handling:
try {
const api = await MidnightSetupAPI.deployContract(providers, contractInstance);
const state = await api.getContractState();
console.log('Success:', state);
} catch (error) {
console.error('Error:', error.message);
}TypeScript Support
The package includes full TypeScript definitions:
import {
MidnightSetupAPI,
MidnightSetupContractProviders,
ContractInstance
} from '@meshsdk/midnight-setup';
// Type-safe provider setup
const providers: MidnightSetupContractProviders = await setupProviders();
// Type-safe contract instance
const contractInstance: ContractInstance = {
// Contract configuration
};Best Practices
- Always handle errors - Wrap API calls in try-catch blocks
- Use TypeScript - Leverage type safety for better development experience
- Validate inputs - Ensure contract instances and addresses are valid
- Monitor state changes - Listen for contract state updates
- Test thoroughly - Use testnet before deploying to mainnet