create-midnight-app
Version:
š Scaffold for building Midnight smart contracts with dynamic type detection and automated CLI generation
118 lines (90 loc) ⢠3.78 kB
JavaScript
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import dotenv from 'dotenv';
// Midnight SDK imports
import { WalletBuilder } from '@midnight-ntwrk/wallet';
import { getZswapNetworkId, setNetworkId, NetworkId } from '@midnight-ntwrk/midnight-js-network-id';
import { nativeToken } from '@midnight-ntwrk/zswap';
import { firstValueFrom } from 'rxjs';
// Configuration
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const projectRoot = path.resolve(__dirname, '..', '..');
const envPath = path.join(projectRoot, '.env');
dotenv.config({ path: envPath });
// Constants
const GENESIS_SEED = '0000000000000000000000000000000000000000000000000000000000000001';
const TRANSFER_AMOUNT = 10_000_000n; // 10 tUsdt = 1,000,000,0 microTusdt
// Testnet configuration
const TESTNET_CONFIG = {
indexer: 'https://indexer.testnet-02.midnight.network/api/v1/graphql',
indexerWS: 'wss://indexer.testnet-02.midnight.network/api/v1/graphql/ws',
node: 'https://rpc.testnet-02.midnight.network',
proofServer: 'http://127.0.0.1:6300'
};
// Validation
const targetAddress = process.env.WALLET_ADDRESS;
if (!targetAddress) {
console.error('ā WALLET_ADDRESS not found in .env file. Run "npm run generate-key" first.');
process.exit(1);
}
console.log('š Midnight Faucet');
console.log(`š„ Target: ${targetAddress}`);
(async () => {
try {
// Set network to testnet
setNetworkId(NetworkId.TestNet);
// Build genesis wallet
const sourceWallet = await WalletBuilder.build(
TESTNET_CONFIG.indexer,
TESTNET_CONFIG.indexerWS,
TESTNET_CONFIG.proofServer,
TESTNET_CONFIG.node,
GENESIS_SEED,
getZswapNetworkId(),
'info'
);
sourceWallet.start();
// Wait for wallet to sync
await new Promise(resolve => setTimeout(resolve, 5000));
const sourceState = await firstValueFrom(sourceWallet.state());
const sourceBalance = sourceState.balances[nativeToken()] || 0n;
if (sourceBalance === 0n) {
console.error('ā Genesis wallet has no funds!');
await sourceWallet.close();
return;
}
console.log(`šø Sending ${TRANSFER_AMOUNT} microTusdt (100 tUsdt) to your wallet...`);
// Create a proper transfer transaction using Midnight SDK
console.log('š§ Creating transfer transaction...');
const transferRecipe = await sourceWallet.transferTransaction([
{
amount: TRANSFER_AMOUNT,
type: nativeToken(),
receiverAddress: targetAddress
}
]);
console.log('š Proving transaction...');
const provenTx = await sourceWallet.proveTransaction(transferRecipe);
console.log('š¤ Submitting transaction...');
const txId = await sourceWallet.submitTransaction(provenTx);
console.log(`ā
Transfer submitted! Transaction ID: ${txId}`);
console.log('ā³ Waiting for confirmation...');
// Wait for transaction to be confirmed
await new Promise(resolve => setTimeout(resolve, 10000));
await sourceWallet.close();
console.log('š Transfer completed successfully!');
} catch (error) {
console.error('ā Transfer failed:', error.message);
// Check if it's a version mismatch error (common issue)
if (error.message.includes('Unsupported version') || error.message.includes('version')) {
console.log('\nš§ Version mismatch - use manual faucet:');
console.log('š https://midnight.network/testnet-faucet');
console.log(`š ${targetAddress}`);
} else {
console.log('\nš§ Manual faucet: https://midnight.network/testnet-faucet');
console.log(`š ${targetAddress}`);
}
}
})();