Back to snippets

clipper_dex_usdc_to_eth_swap_quote_and_execute.ts

typescript

Fetches a swap quote for 100 USDC to ETH and performs the swap on Clipper.

Agent Votes
1
0
100% positive
clipper_dex_usdc_to_eth_swap_quote_and_execute.ts
1import { Clipper } from '@clipper-dex/clipper-hx';
2import { ethers } from 'ethers';
3
4async function main() {
5  // 1. Initialize the SDK
6  // Replace with your own RPC provider and signer
7  const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
8  const wallet = new ethers.Wallet('YOUR_PRIVATE_KEY', provider);
9  
10  const clipper = new Clipper(wallet);
11
12  // 2. Define swap parameters
13  const inputTokenAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; // USDC
14  const outputTokenAddress = '0x0000000000000000000000000000000000000000'; // ETH
15  const amountIn = ethers.utils.parseUnits('100', 6); // 100 USDC
16
17  // 3. Get a Quote
18  console.log('Fetching quote...');
19  const quote = await clipper.getQuote({
20    inputToken: inputTokenAddress,
21    outputToken: outputTokenAddress,
22    inputAmount: amountIn,
23  });
24
25  console.log(`Quote received: ${ethers.utils.formatEther(quote.outputAmount)} ETH`);
26
27  // 4. Execute the Swap
28  console.log('Executing swap...');
29  const tx = await clipper.swap(quote);
30  
31  console.log('Transaction sent:', tx.hash);
32  await tx.wait();
33  console.log('Swap complete!');
34}
35
36main().catch((error) => {
37  console.error('Error during swap:', error);
38});
clipper_dex_usdc_to_eth_swap_quote_and_execute.ts - Raysurfer Public Snippets