Copy import { useWallet } from '@solana/wallet-adapter-react';
import { Connection, PublicKey, Transaction } from '@solana/web3.js';
import { getAssociatedTokenAddress, createTransferInstruction } from '@solana/spl-token';
const USDC_MINT = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v');
const API_BASE = 'https://api.claudelaunch.ai/v1';
// Generate unique service ID
const generateServiceId = () => {
return `svc-${Date.now()}-${Math.random().toString(16).slice(2, 10)}`;
};
// Main launch function
const launchToken = async (
connection: Connection,
wallet: WalletContextState,
tokenConfig: TokenConfig
) => {
const { publicKey, signTransaction } = wallet;
if (!publicKey || !signTransaction) throw new Error('Wallet not connected');
// Step 1: Prepare launch to get fee info
console.log('Preparing launch...');
const prepareRes = await fetch(`${API_BASE}/facilitator/prepare-launch`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
token: {
name: tokenConfig.name,
symbol: tokenConfig.symbol,
description: tokenConfig.description
},
platform: 'pumpfun',
initialBuy: tokenConfig.devBuy || 0
})
});
const { data: prepareData } = await prepareRes.json();
const { payment } = prepareData;
// Step 2: Pay USDC service fee
console.log(`Paying ${payment.amount} USDC service fee...`);
const serviceId = generateServiceId();
const payerTokenAccount = await getAssociatedTokenAddress(USDC_MINT, publicKey);
const serviceTokenAccount = await getAssociatedTokenAddress(
USDC_MINT,
new PublicKey(payment.recipient)
);
const amountInSmallestUnit = Math.floor(parseFloat(payment.amount) * 1_000_000);
const transferIx = createTransferInstruction(
payerTokenAccount,
serviceTokenAccount,
publicKey,
amountInSmallestUnit
);
const tx = new Transaction().add(transferIx);
tx.feePayer = publicKey;
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
const signedTx = await signTransaction(tx);
const paymentSignature = await connection.sendRawTransaction(signedTx.serialize());
await connection.confirmTransaction(paymentSignature, 'confirmed');
console.log('Payment confirmed:', paymentSignature);
// Step 3: Call facilitator with payment proof
console.log('Launching token...');
const launchRes = await fetch(`${API_BASE}/facilitator/launch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Payment-Signature': paymentSignature,
'X-Payment-Payer': publicKey.toString(),
'X-Payment-Service-Id': serviceId
},
body: JSON.stringify({
token: {
name: tokenConfig.name,
symbol: tokenConfig.symbol,
description: tokenConfig.description,
image: tokenConfig.imageUrl,
twitter: tokenConfig.twitter,
telegram: tokenConfig.telegram,
website: tokenConfig.website
},
launch: {
platform: 'pumpfun',
devBuyAmount: tokenConfig.devBuy || 0
}
})
});
const { data: launchData } = await launchRes.json();
// Step 4: Poll for completion
console.log('Waiting for launch to complete...');
const result = await pollLaunchStatus(launchData.launchId);
console.log('Token launched!', result.token.mint);
return result;
};
// Poll for launch completion
const pollLaunchStatus = async (launchId: string) => {
while (true) {
const res = await fetch(`${API_BASE}/facilitator/launch/${launchId}`);
const { data } = await res.json();
if (data.status === 'completed') {
return data;
}
if (data.status === 'failed') {
throw new Error(data.error?.message || 'Launch failed');
}
await new Promise(resolve => setTimeout(resolve, 2000));
}
};
// Usage in React component
const LaunchButton = () => {
const wallet = useWallet();
const connection = new Connection('https://api.mainnet-beta.solana.com');
const handleLaunch = async () => {
try {
const result = await launchToken(connection, wallet, {
name: 'MoonCat',
symbol: 'MCAT',
description: 'AI-powered cat token on Solana',
imageUrl: 'https://example.com/mooncat.png',
devBuy: 0.5
});
console.log('Success! Token:', result.token.mint);
console.log('View on pump.fun:', result.token.platformUrl);
} catch (error) {
console.error('Launch failed:', error);
}
};
return (
<button onClick={handleLaunch} disabled={!wallet.connected}>
Launch Token (5 USDC)
</button>
);
};