@pngfi/jupiter-core
v0.5.0
Published
Jupiter core typescript library
Downloads
11
Readme
@pngfi/jupiter-core
Jupiter core typescript library
Same as @jup-ag/core, but integrated with Penguin's pools
Installation
Yarn
yarn add @pngfi/jupiter-core
NPM
npm install @pngfi/jupiter-core
Usage
import { Connection, PublicKey } from '@solana/web3.js';
import { Jupiter } from '@pngfi/jupiter-core';
import { Wallet } from '@project-serum/anchor';
const wallet = Wallet.local()
const main = async () => {
const connection = new Connection('https://solana-api.projectserum.com');
const wallet = new Wallet
// load Jupiter
const jupiter = await Jupiter.load({
connection,
cluster: 'mainnet-beta',
user: wallet.payer // or public key
})
// RouteMap which map each tokenMint and their respective tokenMints that are swappable
const routeMap = jupiter.getRouteMap();
const possibleSOLPairs = routeMap.get('So11111111111111111111111111111111111111112'); // return an array of token mints that can be swapped with SOL
// Calculate routes for swapping 1 SOL to USDC with 1% slippage
// routes are sorted based on outputAmount, so ideally the first route is the best.
const routes = jupiter.computeRoutes(new PublicKey('So11111111111111111111111111111111111111112'), new ublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'), 1_000_000_000, 1);
console.log('Quoted out amount', routes[0].outAmount);
// Prepare execute exchange
const { execute } = await jupiter.exchange({
route: routes[0]
})
// Swap for keypair
const swapResult = await execute()
// Swap for publicKey
// const swapResult = await execute({
// wallet: SignerWalletAdapter // from @solana/wallet-adapter-base, mainly need signTransaction and signAllTransactions
// })
if (swapResult.error) {
console.log(swapResult.error)
} else {
console.log(swapResult.txid)
console.log(swapResult.inputAddress)
console.log(swapResult.outputAddress)
console.log(swapResult.inputAmount)
console.log(swapResult.outputAmount)
}