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

comet-sdk

v1.0.1

Published

The Typescript SDK for Comet, a DEX aggregator on NEAR.

Downloads

3

Readme

Game plan

  1. Single DEX swaps for REF and Jumbo

    • Study REF's router
    • Study the provider object returned by the wallet adapter.
    • Study Jupiter's SDK. Follow its naming conventions.
    • Write SDK: wrapper on a graphQL API
      1. /quote: Get a ranked list of possible routes
      2. /swap: Get serialized TX to swap
  2. Split routes (v2): Use NearWalletSelector.signAndSendTransactions. The wallet can sign multiple TXs directed to different contracts using https://github.com/ref-finance/ref-ui/blob/19d5bee1c40e0b98686965e19a590a7f3ea27ac0/src/utils/sender-wallet.ts#L163. Eg. for adding liquidity in REF, you first call ft_transfer_call() for each token, then call the add_liquidity function on REF.

  3. Multi leg swaps and Orderbook based swaps- todo

  4. Aurora based swaps- Copy code from REF's UI. This is an add-on for the aggregator.

    • https://wallet.near.org/sign?transactions=DQAAAG1vbm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AAAAAAAAAAAAAAAAAAAAAAAAAAGC3mGyIAAAAAAAAAAAAAAAAAAAAAAAA&callbackUrl=https%3A%2F%2Fapp.ref.finance%2F

    • TXs

      1. Token storage deposit: https://explorer.mainnet.near.org/transactions/AVdCHhFM46mYdAUes4EqbXEMfDo8sYfMqC6oCKF5qspT
      2. Transfer token to Aurora: https://explorer.mainnet.near.org/transactions/84kGefNaj3ZKZeFGJi5pW7NBjHAfPMtLvHXVdDP47xNN
      3. Opaque messages sent to Aurora: https://explorer.mainnet.near.org/transactions/5oFCN1aLrCCGJXLi2PEAWB7xwJ1ZnUiGrQGMTSvap2E9, https://explorer.mainnet.near.org/transactions/8TC2XcdUsKNqSnsWQfkkx5WUkmkmXLx6KM9MoGkvzwdA, https://explorer.mainnet.near.org/transactions/5KnMNnaswMULq2b4MiKrveUJDecaNKgZ4LtoeFgA37Jw

DEX aggregator test setup

  1. Single DEX swap

    • Best route tests are not immediate priority. We can setup local pools and compare results.
    • No tests for returned TX structure.
  2. Multi leg swaps

    1. Deploy an instance of test-token for every token needed by pools.

    2. Deploy pools for every token pair

    3. Test cases:

      1. Ref- single and double swap
      2. Combination of Ref and Jumbo

      Stableswap has the same swap interface as regular pools, so we do not need separate tests. Mainnet stableswap pool ID is 1910.

    4. Have separate unit tests to find best route

Execution plan

  1. Call ft_transfer_call on the input token with v1.comet.near as the destination.
  2. The args are
{
    "receiver_id": "v1.comet.near",
    "amount" : "100000",
    "msg": {
        "referral_id": "vault.comet.near",
        "dexes": [
            {
                "dex": "v2.ref-finance.near",
                "token_in": "wrap.near",

                // Do not decode
                "actions": [{
                    "pool_id": 0,
                    "token_in": "wrap.near",
                    "token_out": "dai.near",
                    "amount_in": "100000",
                    "min_amount_out": "5000",
                }],
            },
            {
                "dex": "spot.spin-fi.testnet",
                "token_in": "dai.near",

                // do not decode
                "actions": [{
                    "market_id": 1,
                    "drop": ["23"],
                    "place": [
                        {
                        "price": "2000000000000000000000000",
                        "quantity": "1000000000000000000000000",
                        "ttl": 604800,
                        "market_order": true,
                        "order_type": "Bid",
                        "client_order_id": 1
                        }
                    ]
                }]
            }
        ]
    }
}
  1. v1.comet.near will get this message through ft_on_transfer.

  2. For each dex

    1. Read the dex and token_in.
    2. Call token_in::ft_transfer_call, with dex as the reciever_id. The message format should be
    {
        "force": 0,
        "actions": {}, // decoded actions array
        "referral_id": "" // the passed reciever ID
    }
  3. Keep a whitelist of allowed DEXes to prevent fund loss.

  4. Spin needs 3 calls

    1. Deposit input token with ft_transfer_call
    2. Place market orders with place_bid or place_ask. Market orders are executed immediately. Multi-market swaps can be atomically performed using batch_ops.
    3. Withdraw tokens with withdraw function

Gas research

  • 1 TGas = 10^12 gas

  • 3 pool swap on REF: 47 TGas

    • Tx to reciept conversion: 2 Tgas
    • Transfer wNEAR to Ref: 10 TGas
    • Ref swap: 22 TGas
    • Transfer output token to user: 5 TGas
    • Withdraw callback: 3 Tgas
    • Token transfer callback: 2 Tgas
  • Max gas per call: 300 TGas. This should be sufficient for 3 REF-like swaps.

  • swap() based method: takes 139 TGas.

  • Instant swap method: 121 TGas

    • 100 TGas if ft_balance_of and callback is removed
    • Instant swap without outbound transfer: 84

wNEAR -> USDT issue

  1. REF UI returns wrap.near -> DAI (6b175474e89094c44da98b954eedeac495271d0f.factory.bridge.near) -> USDT

  2. SDK returns wrap.near -> USDC (a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near) -> USDT

    It's using uni v2 algorithm on the stable pool 1910.