Swap Native token → Shorttail
Last updated
Last updated
E.g: 0.01 ETH → USDC on Ethereum mainnet (Chain 1), specifying selling amount.
const fetch = require('node-fetch');
import ethers from "ethers";
import exchangeAbi from "./exchangeAbi.json";
// Get a quote
async function getQuote() {
const quotePayload = {
"output_asset_symbol": "USDC",
"input_asset_symbol": "ETH",
"chain_id": 1,
"time_in_seconds": 60,
"input_amount": "10000000000000000"
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(quotePayload)
};
const response = await fetch('https://api.clipper.exchange/rfq/quote', requestOptions);
const quote = await response.json();
return quote;
}
// Sign the quote
async function signQuote(quoteId) {
const signPayload = {
"quote_id": quoteId,
"destination_address": "0x0000000000000000000000000000000000000000"
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(signPayload)
};
const response = await fetch('https://api.clipper.exchange/rfq/sign', requestOptions);
const signResponse = await response.json();
return signResponse;
}
// Execute transaction
async function executeSwap(signResponse) {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
const exchangeContract = new ethers.Contract(
signResponse.clipper_exchange_address,
exchangeAbi,
provider
);
const auxData = "0x00000000000000000000000000000000000000000000000000";
const result = await exchangeContract.sellEthForToken(
signResponse.output_asset_address,
signResponse.input_amount,
signResponse.output_amount,
signResponse.good_until,
signResponse.destination_address,
[
signResponse.signature.v,
signResponse.signature.r,
signResponse.signature.s,
],
auxData,
{
value: signResponse.input_amount,
}
);
}
async function main() {
// 1. Get a quote
const quote = await getQuote();
console.log("Quote:", quote);
// 2. Sign a quote
const signResponse = await signQuote(quote.id);
console.log("Sign Response:", signResponse);
// 3. Execute transaction
await executeSwap(signResponse);
console.log("Swap executed successfully.");
}