Created
February 14, 2025 03:44
-
-
Save iatomic1/9470f116725d8da093e8723aecb80606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { bot, forwardOrBroadCast } = require('../lib/'); | |
const { makeSTXTokenTransfer, broadcastTransaction, validateStacksAddress } = require('@stacks/transactions'); | |
require('dotenv').config(); | |
bot( | |
{ | |
pattern: 'trsf ?(.*)', | |
desc: 'Transfer STX tokens | Usage: !trsf <stx-address> <amount>', | |
type: 'all', | |
}, | |
async (message, match) => { | |
if (!message.fromMe) { | |
return await message.send('You are not him'); | |
} | |
const PRIVATE_KEY = process.env.STX_PRIVATE_KEY; | |
// Check for private key | |
if (!PRIVATE_KEY) { | |
return await message.send('Error: STX Private key not configured. Please contact administrator.'); | |
} | |
try { | |
// Validate basic input | |
if (!match) { | |
return await message.send('Error: Please provide both STX address and amount.\nUsage: !trsf <stx-address> <amount>'); | |
} | |
// Parse and validate arguments | |
const matchArr = match.trim().split(/\s+/); | |
if (matchArr.length !== 2) { | |
return await message.send('Error: Invalid number of arguments.\nUsage: !trsf <stx-address> <amount>'); | |
} | |
const [recipientAddress, amountStr] = matchArr; | |
// Validate STX address format (basic check) | |
if (!recipientAddress.startsWith('SP') || !validateStacksAddress(recipientAddress)) { | |
return await message.send('Error: Invalid STX address format. Please provide a valid Stacks address.'); | |
} | |
// Validate and parse amount | |
const amount = parseFloat(amountStr); | |
if (isNaN(amount) || amount <= 0) { | |
return await message.send('Error: Invalid amount. Please provide a positive number.'); | |
} | |
// Convert amount to microstacks (1 STX = 1,000,000 microstacks) | |
const microStacks = Math.floor(amount * 1000000); | |
const txOptions = { | |
recipient: recipientAddress, | |
amount: microStacks, | |
memo: 'test memo', | |
fee: Math.floor(0.001 * 1000000), // 0.001 STX in microstacks | |
senderKey: PRIVATE_KEY, | |
network: 'mainnet', | |
}; | |
const transaction = await makeSTXTokenTransfer(txOptions); | |
const response = await broadcastTransaction({ | |
transaction, | |
network: 'mainnet' | |
}); | |
if (response && response.txid) { | |
return await message.send( | |
`✅ STX transfer initiated successfully!\n\n` + | |
`Amount: ${amount} STX\n` + | |
`Recipient: ${recipientAddress}\n` + | |
`Transaction URL: https://explorer.hiro.so/txid/${response.txid}?chain=mainnet` | |
); | |
} else { | |
throw new Error('Transaction broadcast failed - no transaction ID received'); | |
} | |
} catch (error) { | |
// Handle specific error types | |
let errorMessage = 'An unexpected error occurred during the transfer.'; | |
if (error.message.includes('insufficient balance')) { | |
errorMessage = 'Error: Insufficient balance to complete the transfer.'; | |
} else if (error.message.includes('nonce')) { | |
errorMessage = 'Error: Transaction nonce issue. Please try again.'; | |
} else if (error.message.includes('fee')) { | |
errorMessage = 'Error: Transaction fee too low. Please try again later.'; | |
} | |
console.error('STX Transfer Error:', error); | |
return await message.send( | |
`❌ ${errorMessage}\n\n` + | |
`If this error persists, please contact support.` | |
); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment