Batch multiple swaps in one transaction

Batching 3 swaps into one transaction to swap dust tokens all into ETH using DSA with just a few lines of JS.

Resources:

Uniswap Connector docs: https://docs.instadapp.io/resolvers/uniswap/
Uniswap Resolver docs: https://docs.instadapp.io/resolvers/uniswap/

Code Snippet:

let spells = dsa.Spell();

let buyToken = "ETH";
let buyTokenAddr = dsa.tokens.info.eth.address;

let dustTokens = [ // Dust tokens to sell.
    {
        token: "USDC",
        amount: "2.5" // 2.5 USDC
    },
    {
        token: "KNC",
        amount: "1657.4" // 1657.4 KNC
    },
    {
        token: "COMP",
        amount: "4.9" // 4.9 COMP
    }
]

// Looping over dustTokens.
for (let i = 0; i < dustTokens.length; i++) {
    let sellToken = dustTokens[i].token.toLowerCase(); // Sell Token Symbol.
    let sellTokenAmount = dustTokens[i].amount; // Sell Token amount in decimal form.

    let sellTokenAddr = dsa.tokens.info[sellToken].address; // Sell Token Address.
    let sellTokenAmtInWei = dsa.tokens.fromDecimal(sellTokenAmount, sellToken); // Sell Token amount in wei
    let slippage = 0.2; // 0.2% slippage.
    
    // Resolver function to get `uintAmt` value. 
    // You can use above resolver documentation for reference. 
    let sellDetails = await dsa.uniswap.getBuyAmount(sellTokenAddr, buyToken, sellTokenAmount, slippage);

    spells.add({
        connector: "uniswap",
        method: "sell",
        args: [buyTokenAddr, sellTokenAddr, sellTokenAmtInWei, sellDetails.unitAmt, 0, 0]
    });

}

// Note: Make sure you have set DSA instance.
// If not, follow this to setup: https://docs.instadapp.io/setup/

// Casting the spells to blockchain.
await dsa.cast(spells).then(tx => {
    console.log("Transaction Hash: ", tx)
});
2 Likes