Prerequisite: This guide assumes you’ve already installed the Mach SDK. If not, please see the SDK Installation page first.
Development Environment Setup
To create an optimal development environment for working with Mach Protocol, follow these guidelines:
Set up your environment with these recommended tools:
- Node.js: Version 20 or higher
- Package Manager: npm, yarn, or pnpm
- IDE: VS Code with TypeScript and ESLint extensions
- Environment Variables: Use
.env files for managing private keys and other secrets
Environment Variables
For secure development, store your private keys and configuration in environment variables:
# .env file example
PRIVATE_KEY=your_private_key_here
MACH_ENVIRONMENT=testnet # or mainnet
Then load them in your application:
import dotenv from 'dotenv';
import { swapTokens } from '@tristeroresearch/mach-sdk';
dotenv.config();
async function executeSwap() {
return await swapTokens({
// ... swap parameters
privateKey: process.env.PRIVATE_KEY
});
}
Testing on Testnet
Before deploying to production, always test your integration on a testnet:
import { swapTokens } from '@tristeroresearch/mach-sdk';
// Perform a testnet swap
const result = await swapTokens({
fromChain: 'goerli', // Ethereum testnet
toChain: 'arbitrum-goerli', // Arbitrum testnet
fromToken: 'USDC',
toToken: 'USDC',
amount: '1',
privateKey: process.env.PRIVATE_KEY,
testnet: true // Important flag for testnet operations
});
Getting Testnet Tokens
You can obtain testnet tokens for testing through testnet faucets available on the respective chain’s websites.
Advanced Development Scenarios
Handling Multiple Orders
When handling multiple orders in your application, implement proper tracking:
import { swapTokens, getOrderStatus } from '@tristeroresearch/mach-sdk';
// Store orders in memory or database
const orderTracking = new Map();
async function createAndTrackOrder(params) {
const result = await swapTokens(params);
orderTracking.set(result.orderId, {
status: 'pending',
details: result,
created: new Date()
});
// Set up monitoring
monitorOrderStatus(result.orderId);
return result;
}
async function monitorOrderStatus(orderId) {
// Check every 10 seconds
const interval = setInterval(async () => {
try {
const status = await getOrderStatus(orderId);
// Update tracking
const orderInfo = orderTracking.get(orderId);
orderTracking.set(orderId, {
...orderInfo,
status: status.status,
lastUpdated: new Date()
});
// If in final state, stop monitoring
if (['completed', 'failed', 'cancelled'].includes(status.status)) {
clearInterval(interval);
}
} catch (error) {
console.error(`Error monitoring order ${orderId}:`, error);
}
}, 10000);
}
Error Handling Best Practices
Implement robust error handling in your applications:
import { swapTokens } from '@tristeroresearch/mach-sdk';
async function safeSwap(params) {
try {
const result = await swapTokens(params);
return {
success: true,
data: result
};
} catch (error) {
// Handle specific error types
if (error.message.includes('insufficient funds')) {
return {
success: false,
error: 'INSUFFICIENT_FUNDS',
message: 'Your wallet does not have enough funds for this transaction'
};
} else if (error.message.includes('rejected')) {
return {
success: false,
error: 'USER_REJECTED',
message: 'Transaction was rejected by the user'
};
}
// Generic error handling
return {
success: false,
error: 'SWAP_FAILED',
message: 'Failed to execute swap',
details: error.message
};
}
}
For applications processing high volumes of swaps:
- Connection Pooling: Reuse wallet connections when possible
- Batch Processing: Group multiple operations where feasible
- Caching: Cache chain and token information to reduce API calls
- Rate Limiting: Implement rate limiting for API calls to avoid throttling
Debugging Tips
When debugging issues with the Mach SDK:
- Enable Console Logging: Add detailed logging to track the flow of your application
- Check Transaction Status: Verify transaction status on the source chain’s block explorer
- Validate Input Parameters: Ensure all parameters are correctly formatted before submitting
- Review Error Messages: The SDK provides specific error messages to help diagnose issues
For additional help with development issues, join our Discord community where our team can provide assistance.