Coder Social home page Coder Social logo

catherinee24 / flashloan-foundry-test Goto Github PK

View Code? Open in Web Editor NEW

This project forked from catellatech/flashloan-foundry-test

1.0 1.0 0.0 7.06 MB

Making a Flashloan test with Foundry!

Home Page: https://book.getfoundry.sh/getting-started/installation

Solidity 100.00%

flashloan-foundry-test's Introduction

Trying a Flashloan with Foundry

Author

Lets build an example with Foundry in the polygon mainnet where you can experience how we can start a flash loan. Note we won't be actually doing an arbitrage here, because finding profitable arbitrage opportunities is the hardest part and not related to the code, but will essentially just learn how to execute a flash loan. ๐Ÿ“š๐Ÿ”“

First what are Flash Loans?

You might be thinking: Is it some kind of loan? Well, yes, it is. It's a special type of loan where a borrower can borrow an asset as long as they return the borrowed amount and some interest before the end of the transaction. Since the borrowed amount is returned back, with interest, in the same transaction, there is no possibility for anyone to run away with the borrowed money. If the loan is not repaid in the same transaction, the transaction fails overall and is reverted.

๐Ÿˆธ Applications of a Flash Loan

They help in arbitrage between assets, causing liquidations in DeFi lending protocols, often play part in DeFi hacks, and other use cases. You can essentially use your own creativity to create something new ๐Ÿ˜‡

๐Ÿค” How do Flash Loans work?

There are 4 basic steps to any flash loan. To execute a flash loan, you first need to write a smart contract that has a transaction that uses a flash loan. Assume the function is called createFlashLoan(). The following 4 steps happen when you call that function, in order:

  • Your contract calls a function on a Flash Loan provider, like Aave, indicating which asset you want and how much of it.
  • The Flash Loan provider sends the assets to your contract, and calls back into your contract for a different function, executeOperation.
  • executeOperation is all custom code you must have written - you go wild with the money here. At the end, you approve the Flash Loan provider to withdraw back the borrowed assets, along with a premium.
  • The Flash Loan provider takes back the assets it gave you, along with the premium.

What you will practices in this test:

  • setUp: it's an optional function, it's like the beforeEach in JS
  • The functions we want to test must start with "test", they're like the "it" in JS
  • Learning how to config the foundry cheatcodes to make what we want.
  • We are going to fork the mainnet.
  • We are going to impersonate an account to be able to send transactions from that account. and so much more.

The main idea of this project was to practice Foundry and its most basic commands for testing with Solidity.

๐Ÿšจ Note: One of the commands that helped us detect errors faster with -v (verbosity).

Verbosity of the EVM.

Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).

Verbosity levels:

  • 2: Print logs for all tests
  • 3: Print execution traces for failing tests
  • 4: Print execution traces for all tests, and setup traces for failing tests
  • 5: Print execution and setup traces for all tests.

But you can only run the test, because we config in foundry.toml file the verbosity.


Installing / Getting started

# Clone this project
$ git clone https://github.com/catellaTech/FLASHLOAN-FOUNDRY-TEST

# Access
$ cd FLASHLOAN-FOUNDRY-TEST

# Install dependencies
$ forge install

Commands

  • $ forge build
  • $ forge test --fork-url <RPC URL> --fork-block-number <BLOCK NUMBER>

Technologies / Built With

  • Solidity
  • Foundry
  • Alchemy

Contributing

Contributions are always welcome! Open a PR or an issue!

Email - J.Gabriela
Made with โค๏ธ by catellaTech.

flashloan-foundry-test's People

Contributors

catherinee24 avatar gab0071 avatar catellatech 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.