DSA SDK NodeJS Implementation with flashloan

DSA SDK makes it very simple to execute complex DeFi transactions in NodeJS backend without writing even a single line of smart contracts. Below is an implementation illustrating “Long ETH” spell in NodeJS. You can check the recipe on our docs. The use-case also illustrates the flashloan transactions (no smart contract required).

Code Snippet:

const Web3 = require('web3')
const DSA = require('dsa-sdk'); // Use the latest version of SDK v1.4.5
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.ETH_NODE_URL))

const dsa = new DSA({
    web3: web3,
    mode: "node",
    privateKey: process.env.PRIVATE_KEY
});

async function setup(dsaId) {
    await dsa.setInstance(dsaId).then(console.log("Setup successful"));
}

// Long ETH use-case.
async function longEth() {
    let borrowAmount = 20; // 20 DAI
    let borrowAmtInWei = dsa.tokens.fromDecimal(borrowAmount, "dai"); // borrow flash loan and swap via Oasis

    let slippage = 2; // 2% slippage.
    let dai_address = dsa.tokens.info.dai.address;
    let eth_address = dsa.tokens.info.eth.address;

    let buyDetail = await dsa.oasis.getBuyAmount("ETH", "DAI", borrowAmount, slippage);
    console.log(buyDetail)
    let spells = dsa.Spell();

    spells.add({
        connector: "instapool",
        method: "flashBorrow",
        args: [dai_address, borrowAmtInWei, 0, 0]
    });

    spells.add({
        connector: "oasis",
        method: "sell",
        args: [eth_address, dai_address, borrowAmtInWei, buyDetail.unitAmt, 0, 0]
    });

    spells.add({
        connector: "compound",
        method: "deposit",
        args: [eth_address, "-1", 0, 0]
    });

    spells.add({
        connector: "compound",
        method: "borrow",
        args: [dai_address, borrowAmtInWei, 0, 0]
    });

    spells.add({
        connector: "instapool",
        method: "flashPayback",
        args: [dai_address, 0, 0]
    });

    dsa.cast({
        spells: spells,
        gas: 6000000, // Gas limit (optional)
        gasPrice: dsa.web3.utils.toWei("10", "gwei"), // Gas Price in wei.
        nonce: 89 // tx Nonce. (optional)
    }).then(console.log) // returns transaction hash
}

setup(dsaId).then(() => { // Note: Pass your `dsaId`.
    longEth()
})
1 Like

How to check transaction status using web3?

You can use the below code snippet to check if the transaction got confirmed to not.

Prerequisite: Make sure web3 library is has been initiated.

code snippet:

async function awaitTx(txHash) { // send transaction hash
    var isNotConfirmedShowed = true;

    while(isNotConfirmedShowed) {
        await web3.eth.getTransactionReceipt(txHash).then(function(txreceipt) {

            // txn is null
            if (!txreceipt) {return;}

            // txn is pending
            if (!txreceipt.blockNumber) {return;}

            isNotConfirmedShowed = false;
        }).catch(err => {
            console.log(err);
        });
    }
    if(!isNotConfirmedShowed) {
        return "Tx Confirmed";
    }
}
1 Like