Squid Dev Documentation
Ask or search…
⌃K

Working examples

​
SDK
API
Widget
Example of a cross-chain swap from Avalanche-AVAX to Polygon-MATIC.
First, install the Squid SDK and Squid types:
npm i @0xsquid/[email protected] @0xsquid/squid-types # 2.8.0-beta.0 or higher
import { Squid } from "@0xsquid/sdk"
import { ethers } from "ethers"
​
const userAddress = "<your address>"
const integratorId = "<your integrator id>"
​
const squid = new Squid({
baseUrl: "https://v2.api.squidrouter.com",
integratorId: integratorId
})
​
// initialize the Squid client
await squid.init()
​
const { route } = await squid.getRoute({
fromChain: "43114", // Avalanche
fromAmount: "10000000000000000", // 0.1 AVAX
fromToken: "0xEEeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
toChain: "137", // Polygon
toToken: "0xEEeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
fromAddress: userAddress,
toAddress: userAddress,
slippageConfig: {
autoMode: 1
}
})
​
const privateKey = "<your private key>"
const rpcUrl = "https://avalanche.drpc.org" // fromChain RPC URL
​
const provider = new ethers.JsonRpcProvider(rpcUrl)
const signer = new ethers.Wallet(privateKey, provider)
​
const result = await squid.executeRoute({
route,
signer
})
​
console.log(result)
Example for getting a route from Avalanche-AVAX to Polygon-MATIC:
const integratorId = "<your-integrator-id>"
const userAddress = "<your-address>"
​
const result = await fetch("https://v2.api.squidrouter.com/v2/route", {
method: "POST",
body: JSON.stringify({
fromChain: 43114, // Avalanche
toChain: 137, // Polygon
fromToken: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
toToken: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
fromAmount: "100000000000000000", // 0.1 AVAX
fromAddress: userAddress,
toAddress: userAddress,
slippageConfig: {
autoMode: 1
}
}),
headers: {
"x-integrator-id": integratorId,
"Content-Type": "application/json"
}
})
​
const route = await result.json()
​
console.log(route)
Install the Squid Widget React component:
npm i @0xsquid/widget
And import it in your app:
import { SquidWidget } from "@0xsquid/widget"
​
export default function App() {
const config = {
companyName: "Squid Widget",
integratorId: "<your integrator id>",
slippage: 1,
instantExec: true,
infiniteApproval: false,
apiUrl: "https://api.squidrouter.com",
} as AppConfig;
​
return <SquidWidget config={config} />
}
Last modified 1mo ago