gor-deploy/src/services/laconicTransfer.ts
shreerang acabd4569f Use solana GOR token payments to deploy apps (#1)
Part of https://www.notion.so/Laconic-Mainnet-Plan-1eca6b22d47280569cd0d1e6d711d949

Co-authored-by: Shreerang Kale <shreerangkale@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Reviewed-on: #1
Co-authored-by: shreerang <shreerang@noreply.git.vdb.to>
Co-committed-by: shreerang <shreerang@noreply.git.vdb.to>
2025-07-21 13:14:05 +00:00

117 lines
3.2 KiB
TypeScript

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<LaconicTransferResult> => {
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<Account> => {
const account = new Account(
Buffer.from(accountPrivateKey, 'hex'),
);
await account.init();
return account;
}
const sendTokensToAccount = async (
senderPrivateKey: string,
receiverAddress: string,
amount: string,
): Promise<DeliverTxResponse> => {
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;
}