- React
- Browser SDK
- React Native
Report incorrect code
Copy
Ask AI
import { useSolana } from "@phantom/react-sdk";
import { Connection, PublicKey, VersionedTransaction, TransactionMessage } from "@solana/web3.js";
import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createTransferInstruction, getAccount } from "@solana/spl-token";
const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const USDC_DECIMALS = 6;
function SendUSDC() {
const { solana } = useSolana();
const send = async (to: string, amount: number) => {
const connection = new Connection("https://api.mainnet-beta.solana.com");
const from = new PublicKey(await solana.getPublicKey());
const recipient = new PublicKey(to);
const fromATA = await getAssociatedTokenAddress(USDC_MINT, from);
const toATA = await getAssociatedTokenAddress(USDC_MINT, recipient);
const instructions = [];
// Create recipient's token account if it doesn't exist
try {
await getAccount(connection, toATA);
} catch {
instructions.push(
createAssociatedTokenAccountInstruction(from, toATA, recipient, USDC_MINT)
);
}
// Add transfer instruction
instructions.push(
createTransferInstruction(fromATA, toATA, from, amount * 10 ** USDC_DECIMALS)
);
const { blockhash } = await connection.getLatestBlockhash();
const transaction = new VersionedTransaction(
new TransactionMessage({
payerKey: from,
recentBlockhash: blockhash,
instructions,
}).compileToV0Message()
);
const { signature } = await solana.signAndSendTransaction(transaction);
return signature;
};
return (
<button onClick={() => send("RECIPIENT_ADDRESS", 10)}>
Send 10 USDC
</button>
);
}
Report incorrect code
Copy
Ask AI
import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
import { Connection, PublicKey, VersionedTransaction, TransactionMessage } from "@solana/web3.js";
import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createTransferInstruction, getAccount } from "@solana/spl-token";
const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const USDC_DECIMALS = 6;
const sdk = new BrowserSDK({
providers: ["google", "apple", "injected"],
appId: "your-app-id",
addressTypes: [AddressType.solana],
});
async function sendUSDC(to: string, amount: number) {
const connection = new Connection("https://api.mainnet-beta.solana.com");
const from = new PublicKey(await sdk.solana.getPublicKey());
const recipient = new PublicKey(to);
const fromATA = await getAssociatedTokenAddress(USDC_MINT, from);
const toATA = await getAssociatedTokenAddress(USDC_MINT, recipient);
const instructions = [];
try {
await getAccount(connection, toATA);
} catch {
instructions.push(
createAssociatedTokenAccountInstruction(from, toATA, recipient, USDC_MINT)
);
}
instructions.push(
createTransferInstruction(fromATA, toATA, from, amount * 10 ** USDC_DECIMALS)
);
const { blockhash } = await connection.getLatestBlockhash();
const transaction = new VersionedTransaction(
new TransactionMessage({
payerKey: from,
recentBlockhash: blockhash,
instructions,
}).compileToV0Message()
);
const { signature } = await sdk.solana.signAndSendTransaction(transaction);
return signature;
}
Report incorrect code
Copy
Ask AI
import { useSolana } from "@phantom/react-native-sdk";
import { Connection, PublicKey, VersionedTransaction, TransactionMessage } from "@solana/web3.js";
import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createTransferInstruction, getAccount } from "@solana/spl-token";
import { View, Button, Alert, StyleSheet } from "react-native";
const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const USDC_DECIMALS = 6;
function SendUSDC() {
const { solana } = useSolana();
const send = async (to: string, amount: number) => {
try {
const connection = new Connection("https://api.mainnet-beta.solana.com");
const from = new PublicKey(await solana.getPublicKey());
const recipient = new PublicKey(to);
const fromATA = await getAssociatedTokenAddress(USDC_MINT, from);
const toATA = await getAssociatedTokenAddress(USDC_MINT, recipient);
const instructions = [];
try {
await getAccount(connection, toATA);
} catch {
instructions.push(
createAssociatedTokenAccountInstruction(from, toATA, recipient, USDC_MINT)
);
}
instructions.push(
createTransferInstruction(fromATA, toATA, from, amount * 10 ** USDC_DECIMALS)
);
const { blockhash } = await connection.getLatestBlockhash();
const transaction = new VersionedTransaction(
new TransactionMessage({
payerKey: from,
recentBlockhash: blockhash,
instructions,
}).compileToV0Message()
);
const { signature } = await solana.signAndSendTransaction(transaction);
Alert.alert("Success", `Transaction sent: ${signature}`);
return signature;
} catch (error) {
Alert.alert("Error", error instanceof Error ? error.message : "Transaction failed");
throw error;
}
};
return (
<View style={styles.container}>
<Button
title="Send 10 USDC"
onPress={() => send("RECIPIENT_ADDRESS", 10)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
padding: 20,
},
});
Token addresses
| Token | Mint Address | Decimals |
|---|---|---|
| USDC | EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v | 6 |
| USDT | Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB | 6 |