delegate-framework
Version:
A TypeScript framework for building robust, production-ready blockchain workflows with comprehensive error handling, logging, and testing. Maintained by delegate.fun
91 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDelegate = void 0;
class BaseDelegate {
constructor(connection, signerKeypair, feeTakerKeypair) {
this.requestId = 0;
this.connection = connection;
this.signerKeypair = signerKeypair;
this.feeTakerKeypair = feeTakerKeypair;
}
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;
}
async handleError(error, context) {
const errorContext = {
error: error.message,
stack: error.stack,
timestamp: new Date().toISOString(),
...context
};
this.logOperation('error_occurred', errorContext);
// Could be extended to send to external logging service
console.error('Delegate operation failed:', errorContext);
}
logOperation(operation, data) {
const logData = {
operation,
timestamp: new Date().toISOString(),
signer: this.signerKeypair.publicKey.toBase58(),
...data
};
console.log(`[Delegate] ${operation}:`, logData);
}
generateRequestId() {
return ++this.requestId;
}
validatePublicKey(publicKeyString, fieldName) {
try {
new (require("@solana/web3.js").PublicKey)(publicKeyString);
}
catch (error) {
throw new Error(`Invalid ${fieldName}: ${publicKeyString}, must be a valid public key`);
}
}
validateRequiredField(value, fieldName) {
if (!value) {
throw new Error(`${fieldName} is required`);
}
}
validateStringField(value, fieldName, minLength = 1) {
if (typeof value !== 'string') {
throw new Error(`${fieldName} must be a non-empty string`);
}
if (value.length < minLength) {
throw new Error(`${fieldName} must be a non-empty string`);
}
}
validateNumberField(value, fieldName, min, max) {
if (typeof value !== 'number' || isNaN(value)) {
throw new Error(`${fieldName} must be a valid number`);
}
if (min !== undefined && value < min) {
throw new Error(`${fieldName} must be at least ${min}`);
}
if (max !== undefined && value > max) {
throw new Error(`${fieldName} must be at most ${max}`);
}
}
}
exports.BaseDelegate = BaseDelegate;
//# sourceMappingURL=base-delegate.js.map