1.19.0 • Published 2 years ago

socket-v2-sdk v1.19.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Socket v2 SDK

-> Docs <-

Install

  • yarn add socket-v2-sdk

    or

  • npm i 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

1.19.0

2 years ago

1.18.0

2 years ago

1.17.0

2 years ago

1.16.0

2 years ago

1.15.0

2 years ago

1.14.0

2 years ago

1.13.0

2 years ago

1.12.0

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago