Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jup-ag/core

Package Overview
Dependencies
Maintainers
1
Versions
193
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jup-ag/core

  • 0.0.0-test.3
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
increased by1.45%
Maintainers
1
Weekly downloads
 
Created
Source

@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"
/* *******************
** Wallet/Keypair of Swapper
* *******************/
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 () => {
    /* *******************
    ** Setup Solana RPC connection
    * *******************/
    const connection = new Connection(SOLANA_RPC_ENDPOINT)
    /* *******************
    ** Fetch token list from from Jupiter API
    ** tokens format
      {
        chainId: 101,
        address: '8f9s1sUmzUbVZMoMh6bufMueYH1u4BJSM57RCEvuVmFp',
        symbol: 'TRUE',
        name: 'TrueSight',
        decimals: 9,
        logoURI: 'https://i.ibb.co/pKTWrwP/true.jpg',
        tags: [ 'utility-token', 'capital-token' ]
      },
    * *******************/
    const tokens = await (await fetch(TOKEN_LIST_URL['mainnet-beta'])).json()
    // console.log(tokens)
    /* *******************
    ** Load Jupiter
    * *******************/
    const jupiter = await Jupiter.load({
        connection,
        cluster: 'mainnet-beta',
        user: USER_KEYPAIR // or public key
    })
    /* *******************
    ** Get routeMap, which maps each tokenMint and their respective tokenMints that are swappable
    * *******************/
    const routeMap = jupiter.getRouteMap()
    // console.log(routeMap)
    /* *******************
    ** Input token: USDC
    * *******************/
    const inputToken = tokens.find(t => t.address == 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v')
    console.log(inputToken)
    /* *******************
    ** Get possible pairs
    * *******************/
    const possiblePairs = routeMap.get(inputToken.address); // return an array of token mints that can be swapped with SOL
    const possiblePairsTokenInfo = {}
    possiblePairs.forEach( (address) => {
      possiblePairsTokenInfo[address] = tokens.find((t) => {
        return t.address == address
      })
    })
    // console.log(possiblePairsTokenInfo)
    /* *******************
    ** Output token: USDT
    * *******************/
    const outputToken = possiblePairsTokenInfo['Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB']
    console.log(outputToken)
    /* *******************
    ** Calculate routes for swapping USDC to USDT with 1% slippage
    ** routes are sorted based on outputAmount, so ideally the first route is the best.
    * *******************/
    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)
    
    /* *******************
    ** Route Data
    {
      marketInfos: [
        {
          marketMeta: [Object],
          inputMint: [PublicKey],
          outputMint: [PublicKey],
          notEnoughLiquidity: false,
          minInAmount: undefined,
          inAmount: 10000,
          outAmount: 9993,
          outAmountWithSlippage: 9893,
          minOutAmount: undefined,
          priceImpactPct: 0,
          feeAmount: 4,
          feeMint: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
          feePct: 0.0004
        }
      ],
      getDepositAndFee: [Function: getDepositAndFee],
      inAmount: 10000,
      outAmount: 9993,
      outAmountWithSlippage: 9893,
      priceImpactPct: 0
    }
    * *******************/
    // console.log(routes[0])
    /* *******************
    ** Prepare execute exchange
    * *******************/
    const { execute } = await jupiter.exchange({
        route: routes[0]
    })
    /* *******************
    ** Swap for keypair
    * *******************/
    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()

FAQs

Package last updated on 30 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc