This is a comprehensive walkthrough for developers looking to implement crosschain token swaps using the Across /swap/approval API on the Lens chain.
Across /swap/approval API provides a streamlined interface for developers to build crosschain swapping applications that easily fit in any development environment and tech stacks.
The ideal Across Crosschain Swap API approach involves setting an allowance and executing the transaction directly via the signer. This is a gasful flow.
The Cross-swap API is currently in beta. We appreciate your participation and kindly request you to report any unexpected behaviors or API response anomalies.
Let's get started!
Understanding the /swap/approval Endpoint
In the /swap/approval endpoint crosschain swap flow, consumers of the API need to make sure that the depositor has granted sufficient allowance to the respective contract of the Across Protocol.
const wallet = new Wallet(PRIVATE_KEY).connect(
new ethers.providers.JsonRpcProvider(RPC_URL)
);
const { data } = await axios.get(
`https://preview.across.to/api/swap/approval`,
{
params: {
tradeType: "minOutput",
// required min. output amount of `outputToken` in smallest unit
amount: ethers.utils.parseUnits("0.001", 18).toString(),
// Eth on Ethereum Mainnet
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
// Ethereum Mainnet
originChainId: 1,
// WETH on Lens Chain
outputToken: "0xE5ecd226b3032910CEaa43ba92EE8232f8237553",
// Lens Chain
destinationChainId: 232,
depositor: wallet.address,
},
}
);
// If the depositor address has insufficient allowance, the response
// will contain the field `approvalTxns`. This field is a list of
// approve-transactions that are required before a cross-swap can be
// facilitated.
if (data.approvalTxns) {
// This field is a list because in rare cases (e.g. USDT), updating an
// already-set allowance requires 2 steps:
// 1. Set allowance to 0
// 2. Set allowance to new value
for (const approvalTxn of data.approvalTxns) {
const tx = await signer.sendTransaction({
to: approvalTxn.to,
data: approvalTxn.data,
});
console.log(`Approval tx hash:`, tx.hash);
await tx.wait();
}
}
// Sign cross-swap tx
const tx = await wallet.sendTransaction({
to: data.swapTx.to,
data: data.swapTx.data,
value: data.swapTx.value,
});
console.log("Cross-swap tx hash: ", tx.hash);
await tx.wait();
Important fields to note here are:
tradeType : Defines the type of trade for eg - minOutput.
amount : Specifies amount of inputToken in the smallest unit (wei for ETH or 6 decimals for USDC).
inputToken : Address of the token being swapped.
originChainId : Chain ID of the source/origin chain (232 for Lens Chain mainnet).
outputToken : Address of the token to be received after the swap.
destinationChainId : Chain ID of the destination chain where the output token will be delivered.
depositor : The address of the sender (usually the wallet initiating the swap).
Upon successful execution of the /swap/approval endpoint, the return data should be something like this:
With the above response from the /swap/approval endpoint, you can proceed to sign transaction on the wallet client.
Lens Use Cases using /swap/approval Endpoint
1. Bridge ETH on Ethereum Mainnet to ETH on Lens Chain
import axios from 'axios'
import { parseUnits } from 'viem'
async function fetchSwapDetails() {
const { data } = await axios.get('https://app.across.to/api/swap/approval', {
params: {
tradeType: 'minOutput',
amount: parseUnits("0.001",18).toString(),
inputToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // ETH on Ethereum
originChainId: 1,
outputToken: '0xE5ecd226b3032910CEaa43ba92EE8232f8237553', // WETH on Lens Chain
destinationChainId: 232,
depositor: '<address-of-the-sender>'
}
})
// console.log(data) [if needed]
return data
}
Key Parameters:
tradeType : Defines the type of trade for eg - minOutput.
amount : Specifies amount of inputToken in the smalled unit (wei for ETH or 6 decimals for USDC).
inputToken : Address of the token being swapped.
originChainId : Chain ID of the source/origin chain (232 for lens chain mainnet).
outputToken : Address of the token to be received after the swap.
destinationChainId : Chain ID of the destination chain where the output token will be delivered.
depositor : The address of the sender (usually the wallet initiating the swap).
2. Bridge GHO from Ethereum Mainnet to GHO on Lens Chain
import axios from 'axios'
import { parseUnits } from 'viem'
async function fetchSwapDetails() {
const { data } = await axios.get('https://app.across.to/api/swap/approval', {
params: {
tradeType: 'minOutput',
amount: parseUnits("0.001",18).toString(),
inputToken: '0x1ff1dC3cB9eeDbC6Eb2d99C03b30A05cA625fB5a', // ETH on Ethereum
originChainId: 1,
outputToken: '0x6bDc36E20D267Ff0dd6097799f82e78907105e2F', // WETH on Lens Chain
destinationChainId: 232,
depositor: '<address-of-the-sender>'
}
})
// console.log(data) [if needed]
return data
}
Key Parameters:
tradeType: Use "minOutput" to ensure a minimum amount after the swap.
inputToken: Address of ETH or USDC on the origin chain. (Note: USDC contains 6 decimal places)
originChainId: Chain ID of the origin chain.
outputToken: Corresponding token address on Lens.
destinationChainId: Chain ID of Lens.
3. Swap USDC on Ethereum Mainnet to GHO on Lens Chain
import axios from 'axios'
import { parseUnits } from 'viem'
async function fetchSwapDetails() {
const { data } = await axios.get('https://app.across.to/api/swap/approval', {
params: {
tradeType: 'minOutput',
amount: parseUnits("0.001",18).toString(),
inputToken: '0x1ff1dC3cB9eeDbC6Eb2d99C03b30A05cA625fB5a', // USDC on Ethereum
originChainId: 1,
outputToken: '0x6bDc36E20D267Ff0dd6097799f82e78907105e2F', // GHO on Lens Chain
destinationChainId: 232,
depositor: '<address-of-the-depositor>'
}
})
// console.log(data) [if needed]
return data
}
Key Parameters:
inputToken: Address of USDC on Ethereum Mainnet.
outputToken: Address of GHO on Lens.
With this, you have now completely understood the core principles of using the /swap/approval endpoint and walked through certain examples that enable you to swap and bridge tokens to build custom use cases on Lens Chain.
If you have any doubts, please feel free to reach out to us here.
Last updated
Get swap approval data
get
Returns data required to execute a cross-chain swap.
If the input token requires approval, approvalTxns will be included in the response.
Query parameters
tradeTypestring ยท enumRequired
Type of trade. Use minOutput, exactInput or exactOutput.