1.24.2 • Published 1 year ago
@socket.tech/socket-v2-sdk v1.24.2
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
1.24.2
1 year ago
1.24.1
2 years ago
1.22.0
2 years ago
1.23.2
2 years ago
1.23.3
2 years ago
1.23.0
2 years ago
1.24.0
2 years ago
1.23.1
2 years ago
1.21.14
2 years ago
1.21.15
2 years ago
1.21.10
2 years ago
1.21.11
2 years ago
1.21.12
2 years ago
1.21.13
2 years ago
1.21.8
2 years ago
1.21.9
2 years ago
1.21.6
2 years ago
1.21.7
2 years ago
1.21.4
2 years ago
1.21.5
2 years ago
1.21.2
3 years ago
1.21.3
3 years ago
1.21.1
3 years ago
1.21.0
3 years ago
1.20.0
3 years ago