delegate-framework
Version:
A TypeScript framework for building robust, production-ready blockchain workflows with comprehensive error handling, logging, and testing. Maintained by delegate.fun
123 lines • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSwapProtocol = void 0;
class BaseSwapProtocol {
constructor(keypair, connection) {
this.requestId = 0;
this.keypair = keypair;
this.connection = connection;
}
/**
* Framework-style error handling wrapper
*/
async handleError(operation, context) {
const requestId = this.generateRequestId();
try {
this.logOperation(`${context}_started`, { requestId });
const result = await operation();
this.logOperation(`${context}_completed`, { requestId });
return result;
}
catch (error) {
await this.logError(error instanceof Error ? error : new Error(String(error)), { requestId, context });
throw error;
}
}
/**
* Framework-style retry operation
*/
async retryOperation(operation, maxRetries = 3) {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await operation();
}
catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
this.logOperation('retry_attempt_failed', {
error: lastError.message,
attempt,
maxRetries
});
if (attempt === maxRetries) {
throw lastError;
}
// Exponential backoff: wait 2^attempt seconds
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
throw lastError;
}
/**
* Framework-style error logging
*/
async logError(error, context) {
const errorContext = {
error: error.message,
stack: error.stack,
timestamp: new Date().toISOString(),
...context
};
this.logOperation('error_occurred', errorContext);
console.error('Swap protocol error:', errorContext);
}
/**
* Framework-style operation logging
*/
logOperation(operation, data) {
const logData = {
operation,
timestamp: new Date().toISOString(),
signer: this.keypair.publicKey.toBase58(),
...data
};
console.log(`[SwapProtocol] ${operation}:`, logData);
}
/**
* Generate unique request ID
*/
generateRequestId() {
return ++this.requestId;
}
/**
* Complete swap flow: quote -> transaction -> execute
*/
async swap(inputMint, outputMint, amount, slippage = 0.5) {
return this.handleError(async () => {
// Get quote
const quote = await this.getQuote(inputMint, outputMint, amount, slippage);
if (!quote) {
return {
success: false,
error: 'Failed to get swap quote'
};
}
// Create transaction
const transaction = await this.createSwapTransaction(quote, slippage);
// Execute transaction
const result = await this.executeSwap(transaction);
return {
...result,
outputAmount: quote.outputAmount,
priceImpact: quote.priceImpact
};
}, 'swap_flow');
}
/**
* Validate swap parameters
*/
validateSwapParams(inputMint, outputMint, amount) {
if (!inputMint || !outputMint) {
throw new Error('Input and output mints are required');
}
if (inputMint === outputMint) {
throw new Error('Input and output mints cannot be the same');
}
if (amount <= 0) {
throw new Error('Amount must be greater than 0');
}
}
}
exports.BaseSwapProtocol = BaseSwapProtocol;
//# sourceMappingURL=base-protocol.js.map