|
| 1 | +--- |
| 2 | +title: "Core API Methods" |
| 3 | +description: "Complete reference for MidnightSetupAPI methods and provider setup" |
| 4 | +--- |
| 5 | + |
| 6 | +import Link from "fumadocs-core/link"; |
| 7 | + |
| 8 | +## MidnightSetupAPI |
| 9 | + |
| 10 | +The `MidnightSetupAPI` is the main class for interacting with Midnight Network contracts. It provides methods for deploying, joining, and managing smart contracts. |
| 11 | + |
| 12 | +### Core Methods |
| 13 | + |
| 14 | +| Method | Description | Usage | |
| 15 | +|--------|-------------|-------| |
| 16 | +| `deployContract(providers, contractInstance)` | Deploy a new contract | Creates new contract instance | |
| 17 | +| `joinContract(providers, contractInstance, address)` | Join existing contract | Connect to deployed contract | |
| 18 | +| `getContractState()` | Read contract state | Get current contract data | |
| 19 | +| `getLedgerState()` | Read ledger state | Get blockchain data | |
| 20 | + |
| 21 | +### Deploy Contract |
| 22 | + |
| 23 | +Deploy a new smart contract to the Midnight Network: |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { MidnightSetupAPI } from '@meshsdk/midnight-setup'; |
| 27 | + |
| 28 | +const api = await MidnightSetupAPI.deployContract(providers, contractInstance); |
| 29 | +console.log('Contract deployed:', api.deployedContractAddress); |
| 30 | +``` |
| 31 | + |
| 32 | +### Join Contract |
| 33 | + |
| 34 | +Connect to an existing deployed contract: |
| 35 | + |
| 36 | +```typescript |
| 37 | +import { MidnightSetupAPI } from '@meshsdk/midnight-setup'; |
| 38 | + |
| 39 | +const contractAddress = "contract_address_here"; |
| 40 | +const api = await MidnightSetupAPI.joinContract(providers, contractInstance, contractAddress); |
| 41 | +console.log('Connected to contract:', contractAddress); |
| 42 | +``` |
| 43 | + |
| 44 | +### Get Contract State |
| 45 | + |
| 46 | +Retrieve the current state of a contract: |
| 47 | + |
| 48 | +```typescript |
| 49 | +const state = await api.getContractState(); |
| 50 | +console.log('Contract state:', state); |
| 51 | +``` |
| 52 | + |
| 53 | +### Get Ledger State |
| 54 | + |
| 55 | +Access the current ledger state: |
| 56 | + |
| 57 | +```typescript |
| 58 | +const ledgerState = await api.getLedgerState(); |
| 59 | +console.log('Ledger state:', ledgerState); |
| 60 | +``` |
| 61 | + |
| 62 | +## Provider Setup |
| 63 | + |
| 64 | +Set up providers for Midnight Network: |
| 65 | + |
| 66 | +```typescript |
| 67 | +import { setupProviders } from './lib/providers'; |
| 68 | + |
| 69 | +const providers = await setupProviders(); |
| 70 | +// Returns: MidnightSetupContractProviders |
| 71 | +``` |
| 72 | + |
| 73 | +### Provider Configuration |
| 74 | + |
| 75 | +```typescript |
| 76 | +const providers = { |
| 77 | + fetcher: blockfrostProvider, |
| 78 | + submitter: blockfrostProvider, |
| 79 | + wallet: laceWallet, |
| 80 | + // Additional provider configurations |
| 81 | +}; |
| 82 | +``` |
| 83 | + |
| 84 | +## Error Handling |
| 85 | + |
| 86 | +Always wrap API calls in try-catch blocks for proper error handling: |
| 87 | + |
| 88 | +```typescript |
| 89 | +try { |
| 90 | + const api = await MidnightSetupAPI.deployContract(providers, contractInstance); |
| 91 | + const state = await api.getContractState(); |
| 92 | + console.log('Success:', state); |
| 93 | +} catch (error) { |
| 94 | + console.error('Error:', error.message); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## TypeScript Support |
| 99 | + |
| 100 | +The package includes full TypeScript definitions: |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { |
| 104 | + MidnightSetupAPI, |
| 105 | + MidnightSetupContractProviders, |
| 106 | + ContractInstance |
| 107 | +} from '@meshsdk/midnight-setup'; |
| 108 | + |
| 109 | +// Type-safe provider setup |
| 110 | +const providers: MidnightSetupContractProviders = await setupProviders(); |
| 111 | + |
| 112 | +// Type-safe contract instance |
| 113 | +const contractInstance: ContractInstance = { |
| 114 | + // Contract configuration |
| 115 | +}; |
| 116 | +``` |
| 117 | + |
| 118 | +## Best Practices |
| 119 | + |
| 120 | +1. **Always handle errors** - Wrap API calls in try-catch blocks |
| 121 | +2. **Use TypeScript** - Leverage type safety for better development experience |
| 122 | +3. **Validate inputs** - Ensure contract instances and addresses are valid |
| 123 | +4. **Monitor state changes** - Listen for contract state updates |
| 124 | +5. **Test thoroughly** - Use testnet before deploying to mainnet |
0 commit comments