DSA-SDK - DeFi Development Simplified
Building, integrating and working among different DeFi protocols is still a headache for many developers. DSA-SDK simplifies cross-protocol interactions using smart-contracts and Spells. In this guide we will swap ETH to DAI using Kyber Protocol and DSA Spells.

June 11, 2020 — 6 min read

DeFi is built to be composable: protocols and apps plug into each other to create new financial infrastructure.
Currently, more than $1 billion is locked in DeFi, and developers are creating new DeFi apps and protocols every day.
But building DeFi protocols, and connecting them to each other, is still a headache for many developers. That hurts the user experience and slows mainstream DeFi adoption.
In the previous article, we introduced you to DeFi-SDK, which developers can use to read data across different DeFi protocols.
In this post, we'll introduce DSA (DeFi Smart Accounts) and show how developers can use its SDK to build easy-to-use DeFi apps.
DeFi Smart Accounts, created by InstaDapp, let users and developers reach all DeFi protocols through one account. That makes for a better user experience and helps developers build new financial apps.
There are many DeFi protocols for lending, trading, exchanges, and more. They're open, so developers can connect them to each other to add new features.
Example: If Compound offers a better yield than MakerDAO, a developer can help users move from MakerDAO to Compound.
Here are the steps:
Instapool: Access liquidity
Maker: Payback DAI
Maker: Withdraw ETH
Compound: deposit ETH
Compound: borrow DAI
Instapool: Return liquidity
To do this, a developer has to understand and integrate with every one of these systems.
DSA-SDK solves this with a simple library, backed by an openly accessible smart contract, for taking actions across these DeFi protocols.
The DSA-SDK lets developers work with DSA accounts and build new apps on them.
DSA-SDK is a JavaScript library. It talks to the DSA smart contract and to connector contracts (created by InstaDapp) that interact with DeFi protocols.
Regular Ethereum accounts (EOAs) create DSAs, and each EOA can create several DeFi accounts. DeFi accounts are fully trustless, so users can withdraw their assets at any time.

There are 3 key parts in DSAs:
DeFi Accounts store assets and are owned by users. They can run combined transactions across connectors.
Connectors interact with different protocols, so developers can perform many kinds of actions.
Authentication Modules let users set guardians, managers, or third-party apps to manage their DSAs. Permissions can be set down to the connector level. For example, a user can let their wallet provider rebalance assets to cut interest or raise yield, and nothing else.
You can read more about the DSA-SDK architecture on Github.
Let's create a DSA account and use it with DSA-SDK. The library is JavaScript, so it runs right in your browser console.
Pre-requisite
ETH balance in your Metamask account
NOTE: DeFi-SDK currently only works in Ethereum Mainnet. We will use real 💰.
Using DSA-SDK, we will swap ETH with DAI through the Kyber protocol.
Now open the Google Chrome console on this page with CTRL + SHIFT + I, and paste in the following code.
var script = document.createElement('script');
script.type = 'text/javascript';
script.src ='https://cdn.jsdelivr.net/npm/dsa-sdk@latest/build/dsa.min.js';
document.head.appendChild(script);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src='https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.2.6/dist/web3.min.js'
document.head.appendChild(script);This will import DSA-SDK in your browser.
Now, let’s see if it’s working:
var dsa = new DSA()dsa.tokens.info.dai.name
It will output DAI Stable.
Now let’s get Etheruem provider (Metamask):
web3 = new Web3(web3.currentProvider); window.web3 = new Web3(ethereum); await ethereum.enable();
Now, let’s reinitialize DSA with our web3 provider:
dsa = new DSA(web3);
Next, create a DSA account by running the command below. MetaMask will pop up and ask you to approve a transaction. The ETH amount should be 0. You only pay the gas fee.
await dsa.build()
This outputs a transaction hash, which you can track on an Ethereum explorer like Etherscan.
You've now created your DeFi Smart Account. Check it with this command:
await dsa.getAccounts(ethereum.selectedAddress)
It will output your DSA account address, like below:

Now fund this address. We'll swap 0.01 ETH for DAI, so send it more than 0.01 ETH.
You can send funds directly to your DSA account using Metamask or any other wallet.
Once the funds arrive, we're ready to swap ETH for DAI on Kyber. Kyber is an onchain liquidity protocol you can use to swap tokens. To do this, we'll use a Spell.
Spells are a series of connector functions that perform a specific action. They can be as simple as "lend your ETH on Compound" or as complex as Flash loans.
DSA-SDK includes some pre-built spells, and you can also write your own.
In this tutorial, we'll cast a Kyber swap spell to convert ETH into DAI.
The following code shows the Kyber spell.
spells.add({ connector: "kyber", method: "sell", args: [buyAddr, sellAddr, sellAmt, unitAmt, getId, setId]});
The argument mentioned are:
buyAddr — Address of Token which we want to buy. It’s DAI in our case.
sellAddr — Address of Token which we want to sell. It’s ETH in our case.
sellAmt — The amount we want to convert, which is 0.01 ETH in our case.
unitAmt — The amount of buyAmt/sellAmt with slippage. We need to call a DSA-SDK resolver for this.
getId — ID stores the amount of token deposited, we will pass 0 for this.
setId — ID stores the amount of token deposited. we will pass 0 for this.
To get unitAmt, we need to use DSA-SDK to call the Kyber swap contract.
dsa.kyber.getBuyAmount(buyToken, sellToken, sellAmt, slippage);
Here the fields are.
buyToken — Token contract address you want to buy. In our case, it’s DAI.
sellToken — Token contract address you want to sell. In our case, it’s ETH.
sellAmt — 0.01 ETH
slippage — how much slippage, we allow while swapping. In our case, it’s 1%.
Now run the following function in your console to get the unitAmt.
await dsa.kyber.getBuyAmount(dsa.tokens.info.dai.address, '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0.01', 1);
It will give us the below result.

Now, we have the unitAmt , we will prepare our spell.
let spells = dsa.Spell();
var buyAddr = dsa.tokens.info.dai.address
var sellAddr= '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
var sellAmt = dsa.tokens.fromDecimal('0.01', 'eth')
var unitAmt = "238903446303943680897"
var getId = 0
var setId = 0
spells.add({ connector: "kyber", method: "sell", args: [buyAddr, sellAddr, sellAmt, unitAmt, getId, setId]});Now it’s time to cast our spell.
dsa.cast(spells)
It will return a transaction hash and now we can check our DSA balance using the following code.
dsaAccountAddress = await dsa.getAccounts(ethereum.selectedAddress)await dsa.balances.getBalances(dsaAccountAddress[0].address)
It will show the balances of all supported token in our DSA account.
That's it! We used DSA-SDK and the Kyber protocol to swap ETH for DAI. We didn't have to learn or integrate Kyber ourselves. DSA-SDK handled it all 💫
To learn more about DSA-SDK check out the documentation. If you are stuck anywhere, you can reach out to InstaDapp developer telegram.
Need help with your project or have questions? Contact us via this form, on Twitter @Quicknode, or ping us on Discord!
Founded in 2017, Quicknode deploys institutional-grade blockchain infrastructure for developers and enterprises. With 99.99% uptime and support for 77+ chains, teams build and scale onchain applications without compromise.
The latest engineering insights, product updates, and web3 news delivered straight to your inbox.
📚 NEW: Building Financial Software on SolanaFree book with 10 complete programs, from options to perps.
Get a copy