Files

152 lines
5.7 KiB
TypeScript
Raw Permalink Normal View History

Initial commit: Deal orchestration tool - Freeze-resistant arbitrage loop - Implemented complete arbitrage loop (Steps 0-4) - Risk control service with hard caps (30% LTV, 25% USDTz exposure) - Progressive redemption testing (0k → 50k → cd /home/intlc/projects/proxmox/dbis_core/src/core/defi/arbitrage && git commit -m "Initial commit: Deal orchestration tool - Freeze-resistant arbitrage loop - Implemented complete arbitrage loop (Steps 0-4) - Risk control service with hard caps (30% LTV, 25% USDTz exposure) - Progressive redemption testing ($50k → $250k → $1M+) - Graceful failure handling and state management - CLI interface and programmatic API - Comprehensive documentation Features: - Capital split into three buckets (Core ETH, Working Liquidity, Opportunistic) - ETH wrapping and collateral supply - USDT borrowing at controlled LTV - Discount arbitrage execution - Partial monetization with redemption testing - Loop closing with profit capture Design Principles: - One-way risk only - Anchor asset (ETH) untouchable - No leverage on discounted assets - Independent leg settlement"M+) - Graceful failure handling and state management - CLI interface and programmatic API - Comprehensive documentation Features: - Capital split into three buckets (Core ETH, Working Liquidity, Opportunistic) - ETH wrapping and collateral supply - USDT borrowing at controlled LTV - Discount arbitrage execution - Partial monetization with redemption testing - Loop closing with profit capture Design Principles: - One-way risk only - Anchor asset (ETH) untouchable - No leverage on discounted assets - Independent leg settlement
2026-01-27 14:45:19 -08:00
#!/usr/bin/env node
// Deal Orchestration Tool - CLI Entry Point
import { dealOrchestratorService } from './deal-orchestrator.service';
import { logger } from '@/infrastructure/monitoring/logger';
import { DealExecutionRequest } from './types';
/**
* CLI interface for executing deals
*/
async function main() {
const args = process.argv.slice(2);
if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
console.log(`
Deal Orchestration Tool - Freeze-Resistant Arbitrage Loop
Usage:
node cli.js execute <totalEthValue> [options]
Options:
--totalEthValue <value> Total ETH value in USD (e.g., 10000000 for $10M)
--participantBankId <id> Participant bank ID
--moduleId <id> DeFi module ID
--poolId <id> Optional pool ID
--usdtzDiscount <rate> USDTz discount rate (default: 0.40)
--maxLtv <rate> Maximum LTV (default: 0.30)
--redemptionPortion <rate> Redemption portion (default: 0.35)
--coldStoragePortion <rate> Cold storage portion (default: 0.65)
Examples:
node cli.js execute --totalEthValue 10000000 --participantBankId BANK001 --moduleId MODULE001
node cli.js execute 10000000 BANK001 MODULE001
`);
process.exit(0);
}
if (args[0] === 'execute') {
try {
// Parse arguments
const request: DealExecutionRequest = {
totalEthValue: process.env.TOTAL_ETH_VALUE || args[1] || '10000000',
participantBankId: process.env.PARTICIPANT_BANK_ID || args[2] || '',
moduleId: process.env.MODULE_ID || args[3] || '',
poolId: process.env.POOL_ID || args[4],
usdtzDiscountRate: parseFloat(
process.env.USDTZ_DISCOUNT || args.find((a) => a.startsWith('--usdtzDiscount='))?.split('=')[1] || '0.40'
),
maxLtv: parseFloat(
process.env.MAX_LTV || args.find((a) => a.startsWith('--maxLtv='))?.split('=')[1] || '0.30'
),
monetizationSplit: {
redemptionPortion: parseFloat(
process.env.REDEMPTION_PORTION ||
args.find((a) => a.startsWith('--redemptionPortion='))?.split('=')[1] ||
'0.35'
),
coldStoragePortion: parseFloat(
process.env.COLD_STORAGE_PORTION ||
args.find((a) => a.startsWith('--coldStoragePortion='))?.split('=')[1] ||
'0.65'
),
},
};
if (!request.participantBankId || !request.moduleId) {
throw new Error('participantBankId and moduleId are required');
}
logger.info('Executing Deal', {
totalEthValue: request.totalEthValue,
participantBankId: request.participantBankId,
moduleId: request.moduleId,
});
const result = await dealOrchestratorService.executeDeal(request);
console.log('\n=== Deal Execution Result ===');
console.log(`Deal ID: ${result.dealId}`);
console.log(`Status: ${result.status}`);
console.log(`Step: ${result.state.step}`);
console.log(`\nCapital Buckets:`);
console.log(` Core ETH: $${result.state.buckets.coreEth.toString()}`);
console.log(` Working Liquidity: $${result.state.buckets.workingLiquidity.toString()}`);
console.log(` Opportunistic USDTz: $${result.state.buckets.opportunisticUsdtz.toString()}`);
if (result.step1) {
console.log(`\nStep 1 - Working Liquidity:`);
console.log(` WETH Supplied: ${result.step1.wethAmount.toString()}`);
console.log(` USDT Borrowed: $${result.step1.borrowedUsdt.toString()}`);
console.log(` LTV: ${result.step1.ltv.mul(100).toFixed(2)}%`);
}
if (result.step2) {
console.log(`\nStep 2 - Discount Arbitrage:`);
console.log(` USDT Spent: $${result.step2.usdtSpent.toString()}`);
console.log(` USDTz Received: $${result.step2.usdtzReceived.toString()}`);
console.log(` Discount: ${result.step2.discountRate.mul(100).toFixed(2)}%`);
}
if (result.step3) {
console.log(`\nStep 3 - Partial Monetization:`);
console.log(` USDTz for Redemption: $${result.step3.usdtzForRedemption.toString()}`);
console.log(` USDTz for Cold Storage: $${result.step3.usdtzForColdStorage.toString()}`);
console.log(` Redemption Successful: ${result.step3.redemptionSuccessful}`);
if (result.step3.usdtReceived) {
console.log(` USDT Received: $${result.step3.usdtReceived.toString()}`);
}
}
if (result.step4) {
console.log(`\nStep 4 - Loop Closed:`);
console.log(` Borrow Repaid: ${result.step4.borrowRepaid}`);
console.log(` ETH Unlocked: ${result.step4.ethUnlocked}`);
console.log(` Profit Captured: $${result.step4.profitCaptured.toString()}`);
console.log(` Remaining USDTz: $${result.step4.remainingUsdtz.toString()}`);
}
if (result.finalProfit) {
console.log(`\nFinal Profit: $${result.finalProfit.toString()}`);
}
console.log(`\nRisk Checks: ${result.riskChecks.filter((r) => r.passed).length}/${result.riskChecks.length} passed`);
console.log(`Redemption Tests: ${result.redemptionTests.filter((r) => r.successful).length}/${result.redemptionTests.length} successful`);
if (result.state.errors.length > 0) {
console.log(`\nErrors:`, result.state.errors);
}
process.exit(result.status === 'completed' ? 0 : 1);
} catch (error: any) {
logger.error('CLI Error', {
error: error.message,
stack: error.stack,
});
console.error('Error:', error.message);
process.exit(1);
}
} else {
console.error('Unknown command:', args[0]);
process.exit(1);
}
}
// Run CLI if executed directly
if (require.main === module) {
main().catch((error) => {
logger.error('Unhandled CLI Error', { error });
process.exit(1);
});
}