GithubHelp home page GithubHelp logo

solidity-sample-code's Introduction

Solidity Sample Code

This repository contains a contract and its respective tests that I developed while working as a Smart Contract Programmer.

The functionality of the contract in this repository is to maintain the correct collateralization of debt issued by the platform. If a user issues debt and the collateral falls below the required health ratio, it will liquidate the vault via an Uniswap Flash Loan.

Please note that this contract won't compile out of the box because it requires dependencies that I'm not allowed to share.

This contract exposes two main functions that anyone can call: checkLiquidableVaults and liquidateVault. The first one returns an array of the vaultIDs that are liquidable. The second is the function that should be called when liquidating a vault.

The contract also has 5 setter functions, but I think the focus of this document should be on how a user, without capital, can liquidate a vault (and profit from it) by calling the function liquidateVault.

enter image description here

The function liquidateVault is pretty simple, it checks if vaultID is liquidable, and if it can be liquidated, it starts the process by getting a flash loan. When calling IUniswapV2Pair.swap() if we send data via the calldata parameter, Uniswap will trigger the callback function. This callback function can be overridden and execute custom logic. The only requirement is that, at the end of the callback function, the tokens borrowed via flash loan + a 0.3% fee must be returned to Uniswap. Otherwise, the transaction will revert.

In this contract, the custom logic is in charge of liquidating the vault, repay the flash loan, and finally, distribute rewards to all participants. In this case, the Treasury, the Reward Pool, and the Liquidator (caller).

This contract is tested using Hardhat, Chai, and OpenZeppelin's Test helpers.

solidity-sample-code's People

Contributors

galmoli avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.