Giter Site home page Giter Site logo

ez-flashloan's Introduction

EZ-Flashloan

This repo has been deprecated, in favor of the official Flash Loans Truffle Box


A barebones solidity (^0.5.15) template for Aave's flashloans

Join our #development channel on the Aave Discord

Prerequisite

  • Basic knowledge of Solidity
  • Basic knowledge of Truffle
  • Basic knowledge of Javascript
  • An Infura account and API key

Flashing

  1. In Flashloan.sol, code your logic into the executeOperation() function
  2. When ready, call flashloan() on your contract
  3. Important: your contract must have enough funds of whatever asset you are borrowing to payback the flashloan fee.
  4. If not deploying on mainnet, then change the addressProvider in ./aave/FlashLoanReceiverBase.sol to the relevant address

Set up for 'simple' flash lending

If you do not need to work across protocols, such as simple flash loan testing, then the following instructions apply.

  1. Clone this repo and in the cloned repo directory, install the necessary dependencies :
    npm install
    
  2. In truffle-config.js, add the details of the network you wish to use, e.g. Ropsten.
  • Important: make sure you are using an ethereum account that has enough ETH and token assets to pay back the loan. This is your deployment account
  • Rename env file to .env and include your infura key and deployment account's private key (don't commit this file to Git!)
  1. In Flashloan.sol, line 31, change the asset address to the relevant asset address for the network you chose.

  2. In ./aave/FlashLoanReceiverBase.sol, change the addressProvider to the relevant LendingPoolAddressesProvider for the network you chose.

  3. In a terminal window in your repo directory, replace NAME_OF_YOUR_NETWORK with the network name from step 2 and run:

    truffle console --network NAME_OF_YOUR_NETWORK
    
  4. You are now connected to the network you chose. In the same terminal window:

    migrate --reset
    
  5. Your contract is now deployed on the network you chose. Call your contract's flashloan function within the truffle console with:

    let f = await Flashloan.deployed()
    await f.flashloan()
    

    Be patient as your transaction gets processed and mined.

  6. If your implementation is correct, then the transaction will succeed. If it fails/reverts, a reason will be given.

    • if you didn't make any changes to this template and just deployed it, then the call to f.flashloan() will fail as the contract is not funded with any DAI, so cannot make the flashloan repayment (which includes the amount borrowed + a fee). Solve this by getting DAI (or the relevant asset) and transferring it to your contract.

Set up for cross protocol flash lending

If you are working across protocols, such as using the flash loaned amount on another #DeFi protocol, sometimes it is easier to fork mainnet and use each protocol's production contracts and production ERC20 tokens.

  1. Clone this repo and in the cloned repo directory, install the necessary dependencies :

    npm install
    
  2. (Install and) Run Ganache, preferably the CLI version

  3. In truffle-config.js, ensure the details for the development network match up with your running Ganache instance

  4. To minimise set up steps with Aave's lending pools, use Ganache's fork feature. This will 'fork' mainnet into your Ganache instance. Open terminal, replace YOUR_INFURA_KEY in the following and run:

    ganache-cli --fork https://mainnet.infura.io/v3/YOUR_INFURA_KEY -i 1
    
  5. In a new terminal window in your repo directory, run:

    truffle console
    
  6. Migrate your Flashloan contract to your instance of Ganache with:

    migrate --reset
    
  7. Your contract is now deployed on your local Ganache, which is mirroring mainnet. Call your contract's flashloan function within the truffle console with:

    let f = await Flashloan.deployed()
    await f.flashloan()
    

    Be patient as your ganache instance works its magic.

  8. If your implementation is correct, then the transaction will succeed. If it fails/reverts, a reason will be given.

    • if you didn't make any changes to this template and just deployed it, then the call to f.flashloan() will fail as the contract is not funded with any DAI, so cannot make the flashloan repayment (which includes the amount borrowed + a fee). Solve this by getting DAI (or the relevant asset) and transferring it to your contract.

Known issues

  • If you are using Ganache to fork a network, then you may have issues with the blockchain archive state every 30 minutes. This is due to your node provider (i.e. Infura) only allowing free users access to 30 minutes of archive state. To solve this, upgrade to a paid plan, or simply restart your ganache instance and redploy your contracts.

Debugging transactions

Truffle Debugger

  1. Get the transaction hash of a recent transaction from the Ganache-cli terminal window. E.g. 0xfda3855a8bc93e66aaac14c50baccfd83e9665f13a7b4cc57b4bc0f010831720 (the transaction hash must be valid for the network you are currently using)

  2. In the truffle console window, replace INSERT_THE_TRANSACTION_HASH with your transaction hash:

    debug INSERT_THE_TRANSACTION_HASH
    
  3. When the debugger has started, the available commands are shown. The important commands to remember are n for next step and v to print the current variables and values.

    • In the terminal window, press the n key to navigate to the next step that the compiler executed.
    • If you are within a function and want to check the values used, press v.
    • To quit the debugger, press q or CTRL+C.

    Unable to debug executeOperation()

    The Truffle debugger does not work too well with proxy / complex calls. You may find that the Truffle debugger returns an error such as:

    TypeError: Cannot read property 'version' of undefined
    at ...
    
    • In this case you can try calling your executeOperation() function directly, instead of having Aave's LendingPool contract invoke the function. This will allow you to debug the function directly, however you will need to supply the relevant parameters (e.g. _amount, _fee, _reserve, etc).
    • Alternatively, see the below 'Testnet Debugging' section

Testnet Debugging

If your contracts are deployed to a testnet (or even mainnet), you can use a service such as Tenderly to debug your transactions. You will get access to all the debugging features of Truffle Debugger + many more.

Making it mainnet ready

Before deploying to mainnet:

  • If doing arbitrage and taking profits, hardcode your profit taking address or set it as a variable in the contract

Deploying to mainnet

  1. Rename env file to .env and include your infura key and deployment wallet private key (don't commit this file to Git!)
  2. In truffle-config.js, uncomment the mainnet section under networks.
  3. Deploy to mainnet with truffle migrate --network mainnet

More information

ez-flashloan's People

Contributors

bogdanhabic avatar emilianobonassi avatar mrdavey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ez-flashloan's Issues

VM Exception while processing transaction: revert

any solution please:

truffle(development)> await f.flashloan()
Thrown:
Error: Returned error: VM Exception while processing transaction: revert
at evalmachine.:1:11
at evalmachine.:2:49
at sigintHandlersWrap (vm.js:269:15)
at Script.runInContext (vm.js:124:14)
at runScript (C:\Users\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:222:1)
at Console.interpret (C:\Users\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:237:1)
at ReplManager.interpret (C:\Users\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\repl.js:129:1)
at bound (domain.js:419:14)
at REPLServer.runBound [as eval] (domain.js:432:12)
at REPLServer.onLine (repl.js:716:10)
at REPLServer.emit (events.js:223:5)
at REPLServer.EventEmitter.emit (domain.js:475:20)
at REPLServer.Interface._onLine (readline.js:315:10)
at REPLServer.Interface._line (readline.js:692:8)
at REPLServer.Interface._ttyWrite (readline.js:1017:14)
at REPLServer.self._ttyWrite (repl.js:793:7)
at ReadStream.onkeypress (readline.js:191:10)
at ReadStream.emit (events.js:223:5)
at ReadStream.EventEmitter.emit (domain.js:475:20)
at emitKeys (internal/readline/utils.js:433:14)
at emitKeys.next ()
at ReadStream.onData (readline.js:1151:36)
at ReadStream.emit (events.js:223:5)
at ReadStream.EventEmitter.emit (domain.js:475:20)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11) {
hijackedStack: 'Error: Returned error: VM Exception while processing transaction: revert\n' +
' at Object.ErrorResponse (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-core-helpers\src\errors.js:29:1)\n' +
' at C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-core-requestmanager\src\index.js:140:1\n' +
' at C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\wrapper.js:112:1\n' +
' at XMLHttpRequest.request.onreadystatechange (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-providers-http\src\index.js:96:1)\n' +
' at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:1)\n' +
' at XMLHttpRequest._setReadyState (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:208:1)\n' +
' at XMLHttpRequest._onHttpResponseEnd (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:318:1)\n' +
' at IncomingMessage. (C:\Users\\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:289:47)\n' +
' at IncomingMessage.emit (events.js:228:7)\n' +
' at IncomingMessage.EventEmitter.emit (domain.js:498:23)\n' +
' at endReadableNT (_stream_readable.js:1185:12)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:81:21)'
}

Cant run npm install on Ubuntu without unfixable errors

Aloha , I am trying to run npm install on my ubuntu 18.4 latptop but i always get these same errors. I run npm audit : ┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Low │ Insecure Credential Storage │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ web3 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ No patch available │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ @truffle/hdwallet-provider │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ @truffle/hdwallet-provider > web3 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/877
└───────────────┴──────────────────────────────────────

│ High │ Arbitrary File Write │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ decompress │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ No patch available │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ @truffle/hdwallet-provider │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ @truffle/hdwallet-provider > web3 > web3-bzz > swarm-js > │
│ │ decompress │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/1217

i click the links to the npm site and i get this: All versions of decompress are vulnerable to Arbitrary File Write. The package fails to prevent extraction of files with relative paths, allowing attackers to write to any folder in the system by including filenames containing../. | No fix is currently available. Consider using an alternative module until a fix is made available.No fix is currently available. Consider using an alternative module until a fix is made available.

what can i do to fix all this ?

f.flashloan returns error

I have a problem every time I run await f.flashloan() in
ganache-cli forking from the main network
I get this error

Thrown:
Error: Returned error: VM Exception while processing transaction: revert at PromiEvent (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\promievent.js:9:1)
at TruffleContract.flashloan (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\execute.js:169:1)
at evalmachine.:1:11
at evalmachine.:2:49
at sigintHandlersWrap (vm.js:269:15)
at Script.runInContext (vm.js:124:14)
at runScript (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:222:1)
at Console.interpret (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\console.js:237:1)
at ReplManager.interpret (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\repl.js:131:1)
at bound (domain.js:419:14)
at REPLServer.runBound [as eval] (domain.js:432:12)
at REPLServer.onLine (repl.js:715:10)
at REPLServer.emit (events.js:210:5)
at REPLServer.EventEmitter.emit (domain.js:475:20)
at REPLServer.Interface._onLine (readline.js:316:10)
at REPLServer.Interface._line (readline.js:693:8)
at REPLServer.Interface._ttyWrite (readline.js:1019:14)
at REPLServer.self._ttyWrite (repl.js:792:7)
at ReadStream.onkeypress (readline.js:191:10)
at ReadStream.emit (events.js:210:5)
at ReadStream.EventEmitter.emit (domain.js:475:20)
at emitKeys (internal/readline/utils.js:433:14)
at emitKeys.next ()
at ReadStream.onData (readline.js:1153:36)
at ReadStream.emit (events.js:210:5)
at ReadStream.EventEmitter.emit (domain.js:475:20)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11) {
hijackedStack: 'Error: Returned error: VM Exception while processing transaction: revert\n' +
' at Object.ErrorResponse (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-core-helpers\src\errors.js:29:1)\n' +
' at C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-core-requestmanager\src\index.js:140:1\n' +
' at C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\wrapper.js:112:1\n' +
' at XMLHttpRequest.request.onreadystatechange (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3-providers-http\src\index.js:96:1)\n' +
' at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:1)\n' +
' at XMLHttpRequest._setReadyState (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:208:1)\n' +
' at XMLHttpRequest._onHttpResponseEnd (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:318:1)\n' +
' at IncomingMessage. (C:\Users\Ljtro\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:289:47)\n' +
' at IncomingMessage.emit (events.js:215:7)\n' +
' at IncomingMessage.EventEmitter.emit (domain.js:498:23)\n' +
' at endReadableNT (_stream_readable.js:1184:12)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:80:21)'

.

.

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.