DEV Community

Neelansh Mathur
Neelansh Mathur

Posted on

DeFi & LoanShark - Polygon Week 3

Part of Polygon Fellowship 2022.

gm

Decentralized Finance (DeFi) has been the most interesting topic in blockchain not just for me but for many folks, simply because it provides a new way of dealing with finances that we never saw before.

In this post, I'll share what I created in the DeFi space as part of my fellowship and a brief about my research into some interesting topics.

Borrowing Dapp

The base for this project was an assignment in Polygon Academy. The assignment merely gives us the statement:

Build a borrowing dapp that let's a user submit Ether to a smart contract as collateral and receive stablecoin as a loan. Have this be a web interface with text field input and a button to submit. Get creative!

As you can see, there is a very basic problem statement given, which leaves us with a lot of room for creativity!

So let's think of a smart contract that loans a certain stablecoin at a fixed ratio in return for Ether/Matic. Then when the user has to repay, a small fee will be taken from the Ether that will be returned to the user.
This ratio and fee can be updated by the owner.

What I also thought is, why not let the stablecoin be changed? But then, the existing loans should have been repaid.
So, we can introduce another functionality that is whether the loan contract is active or not. If it is not active, borrowing won't take place but user's can repay their stablecoin to get back the ether.

I am calling this contract LoanShark. If you have played GTA Vice City you will be very familiar with this 🙂

For my smart contract example, I have chosen to create my own ERC20 token called SharkUSDC/sUSDC.
I thought of creating my own stablecoin for this exercise, but decided against it for now. I think it would be a good future prospect, so definitely check out my upcoming posts!

Also, since you can use any ERC20 token, you can set an actual stablecoin for loans and then transfer the coins to the contract and it will be operational.
With this approach, the drawback in my current iteration of code is that since the supply is not in our control, anyone can repay a lot of stablecoin to get ether since a mapping is not there. This can be an upgrade soon.

Challenges

There were a lot of issues to deal with on the Smart Contract side of things.

First of all, there was a problem with multiplication when I was calculating the amount for borrow/repay. Multiplying two uint256 variables results in 1018 being multiplied twice. This cost me a lot of time to figure out. To solve it I just divided the final result once by 1018.

The second issue is something I haven't been able to resolve yet. I tried verifying my contract by using Hardhat's Verify Task which always works. It worked for my ERC20 token but didn't work for my LoanShark contract.
LoanShark takes three things in its constructor:

  1. Stablecoin address
  2. Ratio
  3. Fee Address is passed as a string, and fee is passed as 0. All good, they all work. But then ratio cannot be zero. And to pass it as "1" for One-to-One ETH-Stablecoin mapping, I have to multiply it with 1018 so that it is stored correctly.

This didn't work on the command line. Can't pass this large of a value.

Then I came across a hardhat article that describes how we can pass complex arguments for verification.
We can pass arguments by exporting it from an arguments.js file.

An Example is my own arguments.js:

const { parseEther } = require("ethers/lib/utils");

module.exports = [
    "0x1AfE5e07f6c6f092494DA8423708c412939B6906",
    parseEther('1'),
    0
]
Enter fullscreen mode Exit fullscreen mode

Unfortunately, for whatever reason, this was passed to Etherscan but did not get verified. 😞

I'll update if this issue is resolved, but this is heartbreaking. I need to see the green tick of my verified contract on etherscan. ✅

UPDATE: It got verified! The fix was to convert the BigNumber returned from parseEther to a string with the toString method of BigNumber. Passing the value as a string myself didn't work, but this did.
You can access the verified contract at: https://goerli.etherscan.io/address/0x2162Ba3A993BC8442CEE02340693D1818bbB6826

Links

Check out the site at http://loanshark2.surge.sh.
Devfolio project: https://devfolio.co/projects/loanshark-cd70

Uniswapper

In my post about Week 2, I mentioned that I had been working on Uniswapper, my contract that handles swapping by using Uniswap's contracts. I fiddled with it a bit more in the Add Liquidity part and got it to work perfectly as I wanted:
Deposit just ETH and it will split evenly and add liquidity.

A problem before which I didn't think would happen is leftover tokens. I read and understood that in some uniswap functions if you provided say more ETH than necessary for the transaction, it will refund that ETH back to the user. But here the user is the Smart Contract and not the actual user, so we need to refund both the ETH and the Tokens in case this happens.

Chainlink

In the DeFi world, everything runs around the price of assets. Oracles (plural, not the company) are used to get price data on-chain or off-chain.
A widely-used protocol is the Oracle Chainlink which brings off-chain data to the blockchain. So you can actually compare prices with real-world values like fiat currencies USD/EUR and actual listed stocks.

You can use Oracle's latestAnswer variable in their PriceConsumerV3 contracts by calling it from the frontend or from your Solidity Smart Contracts.

There have a great tutorial here if you want a quickstart.

My implementation is here: https://github.com/neelansh15/Chainlink-Price-Consumer

Research Topic

We had to research on an assigned web3 project. My assigned project is Polymarket, a decentralized betting and information platform. I might create a separate post for that, so stay tuned!

gn

Top comments (0)