@jup-ag/core
Jupiter core typescript library
Installation
Yarn
yarn add @jup-ag/core
NPM
npm install @jup-ag/core
Usage
import bs58 from 'bs58'
import fetch from 'node-fetch'
import { Connection, PublicKey, Keypair } from '@solana/web3.js'
import { Jupiter, TOKEN_LIST_URL } from '@jup-ag/core'
const SOLANA_RPC_ENDPOINT = "https://solana-api.projectserum.com"
const WALLET_PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || "Paste private key here with funds to swap"
const USER_PRIVATE_KEY = bs58.decode(WALLET_PRIVATE_KEY)
const USER_KEYPAIR = Keypair.fromSecretKey(USER_PRIVATE_KEY)
const main = async () => {
const connection = new Connection(SOLANA_RPC_ENDPOINT)
const tokens = await (await fetch(TOKEN_LIST_URL['mainnet-beta'])).json()
const jupiter = await Jupiter.load({
connection,
cluster: 'mainnet-beta',
user: USER_KEYPAIR
})
const routeMap = jupiter.getRouteMap()
const inputToken = tokens.find(t => t.address == 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v')
console.log(inputToken)
const possiblePairs = routeMap.get(inputToken.address);
const possiblePairsTokenInfo = {}
possiblePairs.forEach( (address) => {
possiblePairsTokenInfo[address] = tokens.find((t) => {
return t.address == address
})
})
const outputToken = possiblePairsTokenInfo['Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB']
console.log(outputToken)
const inputAmount = parseInt(0.01 * (10**inputToken.decimals))
const slippagePercentage = 1
const routes = await jupiter.computeRoutes(new PublicKey(inputToken.address), new PublicKey(outputToken.address), inputAmount, slippagePercentage);
console.log('Quoted out amount: ', routes[0].outAmount)
const { execute } = await jupiter.exchange({
route: routes[0]
})
const swapResult = await execute()
if (swapResult.error) {
console.log(swapResult.error)
} else {
console.log(`https://explorer.solana.com/tx/${swapResult.txid}`)
console.log(`inputAddress=${swapResult.inputAddress.toString()} outputAddress=${swapResult.outputAddress.toString()}`)
console.log(`inputAmount=${swapResult.inputAmount} outputAmount=${swapResult.outputAmount}`)
}
}
main()