@api-city/transfer-token
Version:
Plugin for transferring tokens to different wallets in API City
65 lines (55 loc) • 1.87 kB
text/typescript
import { Command } from 'commander';
import {
Connection,
PublicKey,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
function createTransferTokenCommand() {
const command = new Command('transfer-token');
command
.description('Transfer tokens from one account to another')
.requiredOption(
'-d, --destination <string>',
'Destination address to transfer tokens to'
)
.requiredOption(
'-a, --amount <number>',
'Amount of tokens to transfer (in SOL)'
)
.action(async (options, cmd) => {
try {
const { wallet } = cmd.config;
if (!wallet) {
throw new Error('No wallet connected');
}
// Connect to the network
const connection = new Connection(process.env.NEXT_PUBLIC_SOLANA_RPC || 'http://localhost:8899', 'confirmed');
// Parse the destination address
const destinationPubkey = new PublicKey(options.destination);
// Convert SOL amount to lamports
const lamports = options.amount * LAMPORTS_PER_SOL;
// Create transfer instruction
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: destinationPubkey,
lamports,
})
);
// Send transaction
const signature = await wallet.sendTransaction(transaction, connection);
console.log('Transfer successful!');
console.log('Signature:', signature);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
});
return command;
}
export function install(program: Command) {
const transferTokenCommand = createTransferTokenCommand();
program.addCommand(transferTokenCommand);
}