Swap
curl --request GET \
--url https://api.kamino.finance/kswap/swap/const options = {method: 'GET'};
fetch('https://api.kamino.finance/kswap/swap/', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));import requests
url = "https://api.kamino.finance/kswap/swap/"
response = requests.get(url)
print(response.text)false{
"data": {
"transaction": "<string>",
"expectedAmountOut": "<string>",
"minAmountOut": "<string>",
"routerType": "metis"
},
"traceId": "<string>"
}Route
Swap
Get the best route and a pre-built transaction for swapping between two tokens on Solana via Kamino’s swap aggregator. The response includes the serialized transaction, expected output amount, minimum output after slippage, and which router was selected.
GET
/
kswap
/
swap
/
Swap
curl --request GET \
--url https://api.kamino.finance/kswap/swap/const options = {method: 'GET'};
fetch('https://api.kamino.finance/kswap/swap/', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));import requests
url = "https://api.kamino.finance/kswap/swap/"
response = requests.get(url)
print(response.text)false{
"data": {
"transaction": "<string>",
"expectedAmountOut": "<string>",
"minAmountOut": "<string>",
"routerType": "metis"
},
"traceId": "<string>"
}Get the best route and transaction for swapping between two tokens on Solana via Kamino’s swap aggregator.
Example
1
Import Dependencies
import {
createSolanaRpc,
createSolanaRpcSubscriptions,
pipe,
getTransactionDecoder,
getCompiledTransactionMessageDecoder,
decompileTransactionMessageFetchingLookupTables,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
addSignersToTransactionMessage,
signTransactionMessageWithSigners,
sendAndConfirmTransactionFactory,
getSignatureFromTransaction,
isTransactionWithinSizeLimit,
} from '@solana/kit';
import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer';
2
Configure and Initialize
const KEYPAIR_FILE = '/path/to/your/keypair.json';
const API_BASE_URL = 'https://api.kamino.finance';
const RPC_ENDPOINT = 'https://api.mainnet-beta.solana.com';
const WS_ENDPOINT = 'wss://api.mainnet-beta.solana.com';
const signer = await parseKeypairFile(KEYPAIR_FILE);
const rpc = createSolanaRpc(RPC_ENDPOINT);
const rpcSubscriptions = createSolanaRpcSubscriptions(WS_ENDPOINT);
3
Fetch Swap Transaction
Call the KSwap API to get the best route and a pre-built transaction. This example swaps 1 SOL to USDC with 50bps max slippage.const SOL = 'So11111111111111111111111111111111111111112';
const USDC = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
const params = new URLSearchParams({
tokenIn: SOL,
tokenOut: USDC,
amountIn: '1000000000', // 1 SOL in lamports
maxSlippageBps: '50',
wallet: signer.address,
});
const res = await fetch(`${API_BASE_URL}/kswap/swap/?${params}`);
const { data } = await res.json();
console.log(`Router: ${data.routerType}, Expected out: ${data.expectedAmountOut}`);
const encodedTransaction = data.transaction;
4
Decode, Sign, and Send
Decode the base64 transaction, sign it locally, and submit to the network.const txBuffer = Buffer.from(encodedTransaction, 'base64');
const txMessageBytes = getTransactionDecoder().decode(txBuffer).messageBytes;
const compiledMessage = getCompiledTransactionMessageDecoder().decode(txMessageBytes);
const { value: latestBlockhash } = await rpc.getLatestBlockhash({
commitment: 'finalized'
}).send();
const signedTransaction = await pipe(
await decompileTransactionMessageFetchingLookupTables(compiledMessage, rpc),
tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
tx => setTransactionMessageFeePayerSigner(signer, tx),
tx => addSignersToTransactionMessage([signer], tx),
tx => signTransactionMessageWithSigners(tx)
);
if (!isTransactionWithinSizeLimit(signedTransaction)) {
throw new Error('Transaction exceeds size limit');
}
const signature = getSignatureFromTransaction(signedTransaction);
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction, {
commitment: 'confirmed',
skipPreflight: true,
});
console.log('Swap successful! Signature:', signature);