Git Product home page Git Product logo

socket-v2-sdk's Introduction

Socket v2 SDK

-> Docs <-

Install

  • yarn add @socket.tech/socket-v2-sdk

    or

  • npm i @socket.tech/socket-v2-sdk

Usage

In summary:

  • Initialise the sdk:

    const socket = new Socket({ apiKey: API_KEY });
  • Retrieve the token lists

    const tokenList = await socket.getTokenList({
      fromChainId: 1,
      toChainId: 137,
    });
    
    // tokenList.from has list of from tokens
    // tokenList.to has list of to tokens
  • Create a path

    const path = new Path({ fromToken, toToken });
  • Get quote

    const quote = await socket.getBestQuote({
      path,
      amount,
      address,
    }, { ... Any quote preferences here })
  • You have 2 options for executing a quote. Managing the steps yourself or connecting a web3 provider.

  • Connecting web3 provider:

    const provider = new ethers.providers.Web3Provider(window.ethereum); // Or use wallet provider like onboard, web3modal, web3react etc.
    const connectedSocket = socket.connect(provider);
    await connectedSocket.start(quote, {
      onTx: (tx) => {
        console.log('Executing transaction', tx);
        return (tx) => {
          console.log('Done transaction', tx);
        }
      }
      ... // Other callbacks
    });
  • Handle the steps manually

    const execute = await socket.start(quote);
    let next = await execute.next();
    
    while (!next.done && next.value) {
      const tx = next.value;
      const approvalTxData = await tx.getApproveTransaction();
      // ... if there is approval send the approve and wait
    
      const sendTxData = await tx.getSendTransaction();
      // ... send the tx and execute next
    
      next = await execute.next(sendTx.hash);
    }

Direct api communication

All api functions are available through the typescript client

const socket = new Socket(API_KEY);
const activeRoute = await socket.client.routes.getActiveRoute({ activeRouteId: 1234 });

Test

  • USDC Polygon to BSC PRIVATE_KEY="<YOUR_PRIVATE_KEY_WITH_GT_15_USDC>" npx ts-node examples/1_usdc_poly_to_bsc.ts

Other examples here

socket-v2-sdk's People

Contributors

rugamoto avatar salil-naik avatar aniket965 avatar shreykeny avatar arthcp avatar reddyismav avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.