import { Account, DENOM as ALNT_DENOM } from '@cerc-io/registry-sdk'; import { DeliverTxResponse } from '@cosmjs/stargate'; import { registryTransactionWithRetry } from '@/app/api/registry/route'; import { getRegistry, getRegistryConfig } from '../config'; import { LaconicTransferResult } from '../types'; export const transferLNTTokens = async (): Promise => { try { const registryConfig = getRegistryConfig(); const registry = getRegistry(); console.log('Resolving deployer LRN to get payment address...'); // Resolve the deployer LRN to get the payment address const deployerLrn = process.env.DEPLOYER_LRN; if (!deployerLrn) { return { success: false, error: 'DEPLOYER_LRN environment variable is required' }; } const resolveResult = await registry.resolveNames([deployerLrn]); console.log('Resolve result:', resolveResult); if (!resolveResult || resolveResult.length === 0) { return { success: false, error: `Failed to resolve deployer LRN: ${deployerLrn}` }; } const deployerRecord = resolveResult[0]; if (!deployerRecord.attributes) { return { success: false, error: 'Deployer record has no attributes' }; } // Find the paymentAddress attribute const paymentAddress = deployerRecord.attributes.paymentAddress const deployerMinPayment = (deployerRecord.attributes.minimumPayment as string).split(ALNT_DENOM)[0] console.log('Found payment address:', paymentAddress); console.log('Found minimum payment:', deployerMinPayment); console.log('Initiating LNT transfer...'); // Send tokens from prefilled account to payment address const transferResult = await sendTokensToAccount( registryConfig.privateKey, paymentAddress, deployerMinPayment ); console.log('LNT transfer result:', transferResult); if (!transferResult.transactionHash) { return { success: false, error: 'LNT transfer failed - no transaction hash returned' }; } return { success: true, transactionHash: transferResult.transactionHash }; } catch (error) { console.error('Failed to transfer LNT tokens:', error); return { success: false, error: error instanceof Error ? error.message : 'Unknown error during LNT transfer' }; } }; const getAccount = async (accountPrivateKey: string): Promise => { const account = new Account( Buffer.from(accountPrivateKey, 'hex'), ); await account.init(); return account; } const sendTokensToAccount = async ( senderPrivateKey: string, receiverAddress: string, amount: string, ): Promise => { const registry = getRegistry(); const account = await getAccount(senderPrivateKey); const laconicClient = await registry.getLaconicClient(account); const txResponse: DeliverTxResponse = await registryTransactionWithRetry( () => laconicClient.sendTokens( account.address, receiverAddress, [ { denom: ALNT_DENOM, amount, }, ], "auto", ), ); return txResponse; }