mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-16 08:08:56 -05:00
feat: solana send transaction
Still needs cleanup but functional
This commit is contained in:
parent
d6791fe7c3
commit
55755bd91b
@ -20,7 +20,7 @@ const FarcasterSolanaProvider = dynamic(
|
||||
);
|
||||
|
||||
export function Providers({ session, children }: { session: Session | null, children: React.ReactNode }) {
|
||||
const solanaEndpoint = process.env.NEXT_PUBLIC_SOLANA_ENDPOINT || "https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY";
|
||||
const solanaEndpoint = process.env.SOLANA_RPC_ENDPOINT || "https://solana-rpc.publicnode.com";
|
||||
return (
|
||||
<SessionProvider session={session}>
|
||||
<WagmiProvider>
|
||||
|
||||
@ -433,6 +433,9 @@ export default function Demo(
|
||||
Address: <pre className="inline">{truncateAddress(solanaAddress)}</pre>
|
||||
</div>
|
||||
<SignSolanaMessage signMessage={solanaSignMessage} />
|
||||
<div className="mb-4">
|
||||
<SendSolana />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@ -484,6 +487,83 @@ function SignSolanaMessage({ signMessage }: { signMessage?: (message: Uint8Array
|
||||
);
|
||||
}
|
||||
|
||||
function SendSolana() {
|
||||
const [state, setState] = useState<
|
||||
| { status: 'none' }
|
||||
| { status: 'pending' }
|
||||
| { status: 'error'; error: Error }
|
||||
| { status: 'success'; signature: string }
|
||||
>({ status: 'none' });
|
||||
|
||||
const { connection: solanaConnection } = useSolanaConnection();
|
||||
const { sendTransaction, publicKey } = useSolanaWallet();
|
||||
|
||||
const ashoatsPhantomSolanaWallet = 'Ao3gLNZAsbrmnusWVqQCPMrcqNi6jdYgu8T6NCoXXQu1';
|
||||
|
||||
const handleSend = useCallback(async () => {
|
||||
setState({ status: 'pending' });
|
||||
try {
|
||||
if (!publicKey) {
|
||||
throw new Error('no Solana publicKey');
|
||||
}
|
||||
|
||||
const { blockhash } = await solanaConnection.getLatestBlockhash();
|
||||
if (!blockhash) {
|
||||
throw new Error('failed to fetch latest Solana blockhash');
|
||||
}
|
||||
|
||||
// Set both fromPubkey and toPubkey to constants for debugging
|
||||
const fromPubkeyStr = publicKey;
|
||||
const toPubkeyStr = new (await import('@solana/web3.js')).PublicKey(ashoatsPhantomSolanaWallet); // TODO: Replace with a real base58 pubkey string
|
||||
console.error('Debug Solana transfer:', { fromPubkeyStr, toPubkeyStr });
|
||||
const transaction = new (await import('@solana/web3.js')).Transaction();
|
||||
transaction.add(
|
||||
(await import('@solana/web3.js')).SystemProgram.transfer({
|
||||
fromPubkey: publicKey,
|
||||
toPubkey: toPubkeyStr,
|
||||
lamports: 0n,
|
||||
}),
|
||||
);
|
||||
transaction.recentBlockhash = blockhash;
|
||||
transaction.feePayer = new (await import('@solana/web3.js')).PublicKey(fromPubkeyStr);
|
||||
|
||||
const simulation = await solanaConnection.simulateTransaction(transaction);
|
||||
if (simulation.value.err) {
|
||||
// Gather logs and error details for debugging
|
||||
const logs = simulation.value.logs?.join('\n') ?? 'No logs';
|
||||
const errDetail = JSON.stringify(simulation.value.err);
|
||||
throw new Error(`Simulation failed: ${errDetail}\nLogs:\n${logs}`);
|
||||
}
|
||||
const signature = await sendTransaction(transaction, solanaConnection);
|
||||
setState({ status: 'success', signature });
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
setState({ status: 'error', error: e });
|
||||
} else {
|
||||
setState({ status: 'none' });
|
||||
}
|
||||
}
|
||||
}, [sendTransaction, publicKey, solanaConnection]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
onClick={handleSend}
|
||||
disabled={state.status === 'pending'}
|
||||
isLoading={state.status === 'pending'}
|
||||
>
|
||||
Send Transaction (1 lamport)
|
||||
</Button>
|
||||
{state.status === 'error' && renderError(state.error)}
|
||||
{state.status === 'success' && (
|
||||
<div className="mt-2 text-xs">
|
||||
<div>Hash: {truncateAddress(state.signature)}</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function SignMessage() {
|
||||
const { isConnected } = useAccount();
|
||||
const { connectAsync } = useConnect();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user