Untitled
unknown
typescript
a year ago
2.4 kB
6
Indexable
import { Connection, PublicKey, Keypair, Transaction, TransactionInstruction, } from '@solana/web3.js'; import { TOKEN_PROGRAM_ID, Token, ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token'; import axios from 'axios'; async function main() { // Créez une nouvelle connexion Solana const connection = new Connection('https://api.mainnet-beta.solana.com'); const walletPrivateKey = new Uint8Array([/* private key bytes */]); const walletKeypair = Keypair.fromSecretKey(walletPrivateKey); // Récupérez l'adresse du pool Raydium et les adresses des jetons à échanger const raydiumPoolAddress = new PublicKey('Raydium Pool Address'); const tokenAAddress = new PublicKey('Token A Address'); const tokenBAddress = new PublicKey('Token B Address'); // Obtenez les informations sur le pool Raydium const raydiumPoolInfo = await axios.get(`https://raydium.io/api/pool/${raydiumPoolAddress.toString()}`); const poolTokenAccountA = new PublicKey(raydiumPoolInfo.data.data.pubkey); // Créez une instruction de swap const tokenSwapProgramId = new PublicKey(raydiumPoolInfo.data.data.programId); const tokenSwap = new Token( connection, raydiumPoolAddress, tokenSwapProgramId, walletKeypair ); const slippage = 0.01; // Slippage tolerance (1% in this example) // Calculer la quantité de token A que vous voulez échanger const amountIn = 100; // Montant à échanger en token A const swapInstruction = Token.createSwapInstruction( tokenSwapProgramId, walletKeypair.publicKey, poolTokenAccountA, tokenAAddress, tokenBAddress, walletKeypair.publicKey, walletKeypair.publicKey, walletKeypair.publicKey, amountIn, slippage ); // Créez et signez une transaction contenant l'instruction de swap const transaction = new Transaction().add(swapInstruction); // Signez la transaction avec la clé privée du portefeuille transaction.sign(walletKeypair); // Envoyez la transaction à Solana const transactionHash = await connection.sendTransaction(transaction, [walletKeypair]); console.log(`Transaction hash: ${transactionHash}`); // Attendez que la transaction soit confirmée pour vérifier le succès du swap await connection.confirmTransaction(transactionHash); console.log('Swap successful!'); } main().catch((error) => { console.error(error); });
Editor is loading...
Leave a Comment