delegate-framework
Version:
A TypeScript framework for building robust, production-ready blockchain workflows with comprehensive error handling, logging, and testing. Maintained by delegate.fun
83 lines • 3.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Burner = void 0;
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const base_delegate_1 = require("./base-delegate");
const bs58_1 = __importDefault(require("bs58"));
const BN = require("bn.js");
class Burner extends base_delegate_1.BaseDelegate {
async executeDelegate(delegateOptions) {
const requestId = this.generateRequestId();
try {
this.logOperation('burner_execution_started', { requestId });
this.validateOptions(delegateOptions);
const senderKeypair = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(delegateOptions.privateKey));
const mint = new web3_js_1.PublicKey(delegateOptions.tokenAddress);
// Get or create associated token account with retry
const senderTokenAccount = await this.retryOperation(async () => {
const account = await (0, spl_token_1.getOrCreateAssociatedTokenAccount)(this.connection, senderKeypair, mint, senderKeypair.publicKey, true);
return account;
}, 5);
// Get token decimals with retry
const decimals = await this.retryOperation(async () => {
const mintInfo = await (0, spl_token_1.getMint)(this.connection, mint);
return mintInfo.decimals;
}, 3);
if (decimals === -1) {
throw new Error("Failed to get token decimals");
}
// Calculate amount to burn
const amountToBurn = this.calculateBurnAmount(delegateOptions.numTokens, decimals);
// Execute burn transaction with retry
const burnSignature = await this.retryOperation(async () => {
const signature = await (0, spl_token_1.burnChecked)(this.connection, senderKeypair, senderTokenAccount.address, mint, senderKeypair, amountToBurn, decimals);
return signature;
}, 3);
this.logOperation('burner_execution_completed', {
requestId,
signatures: [burnSignature],
burnedAmount: amountToBurn.toString(),
tokenMint: mint.toBase58()
});
return {
success: true,
signatures: [burnSignature],
burnedAmount: amountToBurn.toString(),
tokenMint: mint.toBase58()
};
}
catch (error) {
await this.handleError(error instanceof Error ? error : new Error(String(error)), { requestId });
throw error;
}
}
validateOptions(delegateOptions) {
this.validateStringField(delegateOptions.tokenAddress, 'tokenAddress');
this.validateNumberField(delegateOptions.numTokens, 'numTokens', 0);
this.validateStringField(delegateOptions.privateKey, 'privateKey');
// Validate token address format
try {
new web3_js_1.PublicKey(delegateOptions.tokenAddress);
}
catch (error) {
throw new Error("Invalid token address format");
}
// Validate private key format
try {
bs58_1.default.decode(delegateOptions.privateKey);
}
catch (error) {
throw new Error("Invalid private key format");
}
}
calculateBurnAmount(numTokens, decimals) {
const multiplier = new BN(10).pow(new BN(decimals));
return new BN(numTokens).mul(multiplier);
}
}
exports.Burner = Burner;
//# sourceMappingURL=burner.js.map