Squid Dev Documentation
Ask or search…
K
📈

Stake

Stake with any token across chains with Squid's SDK or Widget
SDK
Staking widget
// Import necessary libraries
import { ethers } from "ethers";
import { Squid } from "@0xsquid/sdk";
// Load environment variables from the .env file
import * as dotenv from "dotenv";
dotenv.config();
const privateKey: string = process.env.PRIVATE_KEY!;
const integratorId: string = process.env.INTEGRATOR_ID!; // get one at https://form.typeform.com/to/cqFtqSvX
const rpcEndpoint: string = process.env.RPC_ENDPOINT!;
const radiantLendingPoolAddress = process.env.RADIANT_LENDING_POOL_ADDRESS!;
const usdcArbitrumAddress = process.env.USDC_ARBITRUM_ADDRESS!;
// Define chain and token addresses
const polygonId = "137"; // Polygon
const arbitrumId = "42161"; // Arbitrum
const nativeToken = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Define departing token
// Define amount to be sent
const amount = "10000000000000000";
// Import Radiant lending pool ABI
import radiantLendingPoolAbi from "../abi/radiantLendingPoolAbi";
// Function to get Squid SDK instance
const getSDK = (): Squid => {
const squid = new Squid({
baseUrl: "https://v2.api.squidrouter.com",
integratorId: integratorId,
});
return squid;
};
// Main function
(async () => {
// Set up JSON RPC provider and signer for source chain (Ethereum)
const provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
const signer = new ethers.Wallet(privateKey, provider);
// Initialize Squid SDK
const squid = getSDK();
await squid.init();
console.log("Initialized Squid SDK");
// Create contract interface and encode deposit function for Radiant lending pool
const radiantLendingPoolInterface = new ethers.utils.Interface(
radiantLendingPoolAbi
);
const depositEncodedData = radiantLendingPoolInterface.encodeFunctionData(
"deposit",
[
usdcArbitrumAddress,
"0", // Placeholder for dynamic balance
signer.address,
0,
]
);
// Set up parameters for swapping tokens and depositing into Radiant lending pool
const params = {
fromAddress: signer.address,
fromChain: polygonId,
fromToken: nativeToken,
fromAmount: amount,
toChain: arbitrumId,
toToken: usdcArbitrumAddress,
toAddress: signer.address,
slippage: 1,
slippageConfig: {
autoMode: 1,
},
quoteOnly: false,
// Customize contract call for depositing on Arbitrum
postHooks: [
{
callType: 1, // SquidCallType.FULL_TOKEN_BALANCE
target: radiantLendingPoolAddress,
value: "0",
callData: depositEncodedData,
payload: {
tokenAddress: usdcArbitrumAddress,
inputPos: 1,
},
estimatedGas: "50000",
},
],
};
console.log("Parameters:", params);
// Get the swap route using Squid SDK
const { route, requestId } = await squid.getRoute(params);
console.log("Calculated route:", route.estimate.toAmount);
// Execute the swap and deposit transaction
const tx = (await squid.executeRoute({
signer,
route,
})) as unknown as ethers.providers.TransactionResponse;
const txReceipt = await tx.wait();
// Show the transaction receipt with Axelarscan link
const axelarScanLink =
"https://axelarscan.io/gmp/" + txReceipt.transactionHash;
console.log(`Finished! Check Axelarscan for details: ${axelarScanLink}`);
// Wait a few seconds before checking the status
await new Promise((resolve) => setTimeout(resolve, 5000));
// Retrieve the transaction's route status
const getStatusParams = {
transactionId: txReceipt.transactionHash,
requestId: requestId,
fromChainId: polygonId,
toChainId: arbitrumId,
};
const status = await squid.getStatus(getStatusParams);
// Display the route status
console.log(`Route status: ${status.squidTransactionStatus}`);
})();
Install dependencies:
npm i @0xsquid/staking-widget @0xsquid/sdk ethers
And import the widget in your app:
import { SquidStakingWidget } from "@0xsquid/staking-widget";
import { SquidCallType } from "@0xsquid/sdk";
import { ethers } from "ethers";
import erc20Abi from "./abi/erc20Abi";
import pinjamStakingPoolAbi from "./abi/pinjamStakingPoolAbi";
const pinjamStakingPoolInterface = new ethers.Interface(
pinjamStakingPoolAbi
);
const kavaId = 2222;
const pinjamAxlUsdcPoolAddress = "0x11c3d91259b1c2bd804344355c6a255001f7ba1e";
const axlUsdcKavaAddress = "0xeb466342c4d449bc9f53a865d5cb90586f405215";
const pAxlUsdcAddress = "0x5c91f5d2b7046a138c7d1775bffea68d5e95d68d";
const erc20Interface = new ethers.Interface(erc20Abi);
export default function App() {
const stakeConfig = {
tokensConfig: [
{
stakedTokenExchangeRateGetter: async () => {
return 1;
},
unstakeLink: "https://app.pinjamlabs.com/staking",
customContractCalls: [
{
callType: SquidCallType.FULL_TOKEN_BALANCE,
target: axlUsdcKavaAddress,
value: "0",
callData: () => {
return erc20Interface.encodeFunctionData("approve", [
pinjamAxlUsdcPoolAddress,
"0",
]);
},
payload: {
tokenAddress: axlUsdcKavaAddress,
inputPos: 1,
},
estimatedGas: "50000",
},
{
callType: SquidCallType.FULL_TOKEN_BALANCE,
target: pinjamAxlUsdcPoolAddress,
value: "0",
callData: (route: any) => {
if (route.destinationAddress) {
return pinjamStakingPoolInterface.encodeFunctionData(
"deposit",
[axlUsdcKavaAddress, "0", route.destinationAddress, true]
);
}
return "0x0000000000000000000000000000000000000000";
},
payload: {
tokenAddress: axlUsdcKavaAddress,
inputPos: 1,
},
estimatedGas: "250000",
},
],
stakedToken: {
chainId: kavaId,
address: pAxlUsdcAddress,
name: "paxlUSDC",
symbol: "paxlUSDC",
decimals: 6,
logoURI:
"https://assets.coingecko.com/coins/images/6319/thumb/USD_Coin_icon.png?1547042389",
coingeckoId: "usd-coin",
},
tokenToStake: {
chainId: kavaId,
address: axlUsdcKavaAddress,
},
stakingContract: {
address: "0xe5274e38e91b615d8822e8512a29a16ff1b9c4af",
explorerLink: "https://kava.mintscan.io/account/",
},
logoUrl: "https://app.pinjamlabs.com/icons/pinkav.svg",
},
],
introPage: {
title: "Stake paxlUSDC on Kava",
description: "Stake paxlUSDC on Kava to earn rewards",
},
};
return (
<SquidStakingWidget
config={{
apiUrl: "https://v2.api.squidrouter.com",
integratorId: "<your integrator id>",
stakeConfig,
}}
/>
);
}
Last modified 1mo ago