npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

curve-price-fetcher

v1.0.3

Published

A utility for fetching prices of swaps on curve finance

Downloads

1

Readme

curve-price-fetcher

curve-price-fetcher is a node package for interacting with curve smart contracts and finding the best price for swaps (not taking into account fees). you will need to provide a web3 instance for it to work.

Usage:

Firstly install the dependency to your project

npm i curve-price-fetcher --save

Import the dependency where you need it:

const CurvePriceFetcher = require('curve-price-fetcher');

Instantiate the fetcher and pass in a web3 instance:

const web3 = new Web3(new Web3.providers.HttpProvider(`https://localhost:8545`));

const fetcher = new CurvePriceFetcher();

above we are using the hardcoded set of pools and meta data.

alternatively, you can pass in a custom list of pool meta data.

const web3 = new Web3(new Web3.providers.HttpProvider(`https://localhost:8545`));

const customPoolData = [
    {
        "poolAddress": "0xa2b47e3d5c44877cca798226b7b8118f9bfb7a56",
        "coins": [
            "0x5d3a536e4d6dbd6114cc1ead35777bab948e3643",
            "0x39aa39c021dfbae8fac545936693ac917d5e7563"
        ],
        "underlying": [
            "0x6b175474e89094c44da98b954eedeac495271d0f",
            "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
        ]
    },
    {
        "poolAddress": "0x52ea46506b9cc5ef470c5bf89f17dc28bb35d85c",
        "coins": [
            "0x5d3a536e4d6dbd6114cc1ead35777bab948e3643",
            "0x39aa39c021dfbae8fac545936693ac917d5e7563",
            "0xdac17f958d2ee523a2206206994597c13d831ec7"
        ],
        "underlying": [
            "0x6b175474e89094c44da98b954eedeac495271d0f",
            "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
            "0xdac17f958d2ee523a2206206994597c13d831ec7"
        ]
    }
];

const fetcher = new CurvePriceFetcher({ web3, customPoolData });

finally, you can also pass in an array of addresses. you can then fetch pool metadata after creation and it will be used for subsequent pricing data.

const customPoolData = [
   {
       "poolAddress": "0xa2b47e3d5c44877cca798226b7b8118f9bfb7a56"
   },
   {
       "poolAddress": "0x52ea46506b9cc5ef470c5bf89f17dc28bb35d85c"
   }
];

const fetcher = new CurvePriceFetcher({ web3, customPoolData });

await fetcher.getAllPoolMetaData();

Fetching Prices:

Once you have your fetcher instantiated, you can fetch prices like so:

let prices = await fetcher.getPoolPrices(swapFrom, swapTo, '1000000000000000000000');

prices.forEach(
    pool => console.log(`Pool ${pool.poolAddress}: ${pool.price.toNumber()}`)
)

Prices are returned as a BigNumber object

Improvements

initially this was a quick and dirty script for a project i'm working on but then i packaged in a module. functionality is limited to fetching prices but some things that could be added (feel free to PR):

  • take gas into account. the gas fees vary based on the underlying assets (quite vastly) due to different implementations, so the best price is not necessarily the best price.
  • add support for performing swap
  • route through multiple pools. for very large orders it will be more cost efficient to use multiple.
  • fetch pool addresses from curve registry instead of json file (i dont think curve have a registry at this time)

Examples

There's example code in the examples folder. To use this you can rename the .env.example file to .env and change your infura project id in the file. If using a local node, replace the provider in the example with the RPC url of your local node before running.