How to Make Your Own DeFi Protocol Without Writing a Smart Contract
Decentralized finance, or “DeFi”, has grown to become the undeniably dominant use case for Ethereum today. With funds locked in DeFi protocols exceeding $1 billion earlier this year, we are witnessing the start of an entirely new financial system being built from the bottom up. New DeFi protocols seem to be popping up every week. These protocols, often colloquially referred to as “Money Legos”, can be combined and composed to create entirely new financial products.
At Authereum, we’re making building with these protocols easier than ever before. In this post we’ll explore how you can take advantage of Authereum’s batched transactions API to create your own protocol without writing a single line of smart contract code.
Batched Transactions
Regular Ethereum accounts, also known as Externally Owned Accounts (EOAs), have an unfortunate limitation. Each transaction from an EOA can only directly interact with a single address or contract. This frequently shows up in dapp interactions with messages like “You must first unlock X before you Y”.
Contract based accounts like Authereum do not suffer from this limitation and can batch transactions to multiple contracts for a smoother user experience. The “approval” step can be completely abstracted away in the eyes of the user and they can simply perform the action that they intended too. Dapps like Erasure Bay and 1inch.exchange are already using batched transactions to improve their user experiences.
But what if we take this a step further? We can abstract away entire protocols to create something entirely new.
Our Building Blocks
The number of DeFi protocols we can build with is growing every day but they primarily fall into a few main categories:
- Exchange — Uniswap, dYdX, Kyber Network, 0x
- Lending/Borrowing — Compound, dYdX, Aave
- Synthetic Assets — Maker, Synthetix, FutureSwap
- Insurance — Nexus Mutual, Opyn
In this example, we’ll be building an ETH lending protocol using Maker and Compound that gets a better return than Compound itself (based on todays lending rates).
Our Lending App
Our example DeFi protocol, lets users lend their ETH to earn interest. We could just lend ETH on Compound but the rates are abysmally low and hardly worth the effort (currently < 0.01%). This app will take advantage of Maker’s low stability fee (currently 0%) and Compound’s much higher DAI lending rate (currently 4.31%). If you want to take at the full implementation you can find it here.
Lending
To get users a return on their ETH we will:
- Open a Vault and deposit ETH
- Withdraw DAI from the Vault
- Lend that DAI out on Compound
This will allow users to maintain full exposure to ETH while also earning about ~2.15% interest. This is half of the DAI lending rate on Compound because only half the value of the ETH collateral can be withdrawn from the Vault in order to maintain a healthy collateralization ratio.
To do this we’ll batch 3 transactions:
- Open the Vault, deposit ETH, and withdraw DAI
Maker makes these steps easy to do with one transaction. The function we want to interact with on the smart contract is:
function openLockETHAndDraw(
address manager, // A contract in the Maker system
address jug, // A contract in the Maker system
address ethJoin, // A contract in the Maker system
address daiJoin, // A contract in the Maker system
bytes32 ilk, // 🤔IDK, ask someone at Maker
uint256 wadD // The amount of DAI to withdraw
)
Our first transaction will look something like this:
const openVaultTransaction = {
to: makerProxy, // The contract being called
data: openVaultData, // Our calldata for the function above
value: ethLendAmount, // The amount of ETH being lent
gasLimit: GAS_LIMIT // The amount of gas to include
}
2. Approve DAI to the cDAI contract
Compounds DAI lending contract is also an ERC20 called cDAI! You can deposit DAI and receive cDAI that will earn interest. First, we must approve a transfer of DAI to the cDAI contract. (No need to approve an unlimited amount when using batched transactions.) To do this we call approve
on the DAI contract with the cDAI contract’s address as our _spender
and the amount of DAI withdrawn above as the _value
.
function approve(
address _spender, // The cDAI contract address
uint256 _value. // The DAI withdrawn from the vault
)
Our second transaction will then look like this:
const approveDaiTransaction = {
to: daiAddress, // The DAI contract address
data: approveDaiData, // The calldata for `approve`
value: '0', // No ETH needed
gasLimit: GAS_LIMIT // The amount of gas to include
}
3. Lend DAI on Compound
Now we call mint
on the cDAI contract which will take our DAI that we approved and mint cDAI. This is the cDAI contract's mint
function:
function mint(
uint256 mintAmount // The DAI withdrawn from the vault
)
Our third transaction will look like this:
const mintCDaiTransaction = {
to: cDaiAddress, // The cDAI contract address
data: mintCDaiData, // The calldata for `mint`
value: '0', // No ETH needed
gasLimit: GAS_LIMIT // The amount of gas to include
}
🔮✨This is where the magic happens✨🧙♂️
We can now batch up our transactions and send them off the Ethereum network to be executed as a single on-chain transaction.
import Authereum from 'authereum'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()// Send off our batched transaction!
await provider.sendTransactionBatch([
openVaultTransaction,
approveDaiTransaction,
mintCDaiTransaction
])
And that’s it! We can now get a larger return on ETH than most of the major lending protocols including Compound itself, dYdX, and Aave. You can find the code for the full working demo here and and run it yourself.
Conclusion
Contract based accounts make it easy to mix and match DeFi protocols to make something entirely new. The number of protocols is constantly growing and the possibilities for combining them are exploding. What will you come up with?
You can find more information about Authereum and our batched transactions API in our docs. Have some questions or want to chat about your idea? Jump in our Telegram and shoot us a message. Lastly, follow Authereum on Twitter for all of our latest news and updates.