Skip to content

bgd-labs/rescue-mission-phase-2-3

Repository files navigation

Aave Rescue Mission Phase 2 & 3 🚑 👻

rescue

Repository containing all the code needed for Phase 2 & 3 to rescue tokens sent directly to contracts of the Aave ecosystem.

This phase will affect the tokens locked on smart contracts of the Aave liquidity pools: Aave v1 Ethereum, Aave v2 Ethereum, Aave v2 AMM, Aave v3 (all networks).


The following table represents the tokens to rescue from various contracts:

Tokens to Rescue Contract where tokens are stuck Amount Network
AAVE V2 A_RAI AAVE V2 A_RAI 1481.16074087007480402 ETHEREUM
AAVE V1 A_WBTC AAVE V1 POOL 1.92454215 ETHEREUM
USDT AAVE V2 AMM_POOL 20600.057405 ETHEREUM
DAI AAVE V2 POOL 22000 ETHEREUM
GUSD AAVE V2 POOL 19994.86 ETHEREUM
LINK AAVE V1 POOL 4084 ETHEREUM
USDT AAVE V2 A_USDT 11010 ETHEREUM
USDC AAVE V2 POOL 1089.889717 ETHEREUM
WBTC AAVE V2 POOL 0.22994977 POLYGON
AAVE V2 A_DAI AAVE V2 A_DAI 4250.580268097645600939 POLYGON
AAVE V2 A_USDC AAVE V2 A_USDC 514131.378018 POLYGON
USDC AAVE V2 POOL 4515.242949 POLYGON
USDT.e AAVE V2 POOL 1772.206585 AVALANCHE
USDC.e AAVE V2 POOL 2522.408895 AVALANCHE
USDC.e WETH_GATEWAY 14100 AVALANCHE
USDC AAVE V3 POOL 44428.421035 OPTIMISM

Contents

This repository is a follow-up of rescue mission phase 1 and is divided into three parts:

  • Scripts to fetch tokens to rescue off-chain using Dune analytics, generating address/value maps and Merkle trees for each token.
  • Upgraded implementation contracts of Aave where funds are stuck, adding a rescue function.
  • Proposal payloads.
Screenshot 2023-09-08 at 4 40 33 PM

Setup:

To setup the project locally you will need to do:

npm install: You need to install all NodeJs dependencies in order to generate merkle trees.

forge install: The project uses Foundry - so you will need to have it installed, and then install all its dependency.

Make sure to have the following in your .env file

# Tenderly forks
TENDERLY_FORK_URL_MAINNET= // to test all the claims on ethereum
TENDERLY_FORK_URL_POLYGON= // to test all the claims on polygon
TENDERLY_FORK_URL_AVALANCHE= // to test all the claims on avalanche
TENDERLY_FORK_URL_OPTIMISM= // to test all the claims on optimism

# Dune Api Key
DUNE_API_KEY= // to query data needed for rescue from dune


Scripts

  • generate-address-value-map: This script will query all token transfer events to Aave contracts using the dune API. For checking transactions where users have sent underlying token to the aToken contract itself, we filter out the transfer transactions - by removing those happening during "normal" operations such as Deposit, Repay, Liquidation, and Flashloan on the pool contract.

    For checking transactions where users have sent tokens to Aave v1 pool core, we also filter out the transfer transactions caused by Deposit, Repay, Liquidation, and Flashloan operations.

    This script also generates a summary indicating the amount to rescue for every token sent on the contracts. Tokens with a value less than $1000 are ignored.

    npm run generate-json-mainnet
    npm run generate-json-l2
    

    This will generate the json for each token as: address - amounts - transactions.

    Example of a generated file from this command - usdtRescueMap.json

  • generate-merkle-roots: This script will take the above-generated address/value map JSON as input and generate a Merkle tree for each token on each network.

    npm run generate-tree
    

    Example of a generated file from this command: usdtRescueMerkleTree.json

  • generate-json-formatted: To format the address value map generated we can run the following command to format the json map in the token decimals.

    npm run generate-json-formatted
    

    Example of a generated file from this command: usdtRescueMapFormatted.json

  • generate:users-json: Script to generate user resume - which will include the proofs for the users in order to claim the tokens from the distributor contract. There will be one file generated for each network containing all the user summary.

    npm run generate:users-json
    

    Example of a generated file from this command: usersMerkleTrees.json


Contracts

The following Aave contracts are updated by adding a rescue function that can transfer the stuck funds to the Merkle distributor contract.

  • Aave v1 pool
  • Aave v2 amm pool
  • Aave v2 ethereum pool
  • Aave v2 polygon pool
  • Aave v2 avalanche pool
  • Aave v2 aRai contract on ethereum
  • Aave v2 aUsdt contract on ethereum
  • Aave v2 aDai contract on polygon
  • Aave v2 aUsdc contract on polygon

This is different than the previous approach in phase 1, where we were rescuing funds on the initialize method. Now we have a separate method rescueTokens() to rescue funds.

We have a Merkle distributor on each network which will distribute the tokens to the users.

On Ethereum, we will use the same Merkle distributor as in phase one while deploying new Merkle distributors on the other networks.

Implementation addresses of contracts before and after the rescue mission phase 2, 3:

Previous Contract Impl Upgraded Contract Impl
Aave v1 pool Aave v1 pool
Aave v2 amm pool Aave v2 amm pool
Aave v2 ethereum pool Aave v2 ethereum pool
Aave v2 aRai ethereum Aave v2 aRai ethereum
Aave v2 aUsdt ethereum Aave v2 aUsdt ethereum
Aave v2 polygon pool Aave v2 polygon pool
Aave v2 aDai polygon Aave v2 aDai polygon
Aave v2 aUsdc polygon Aave v2 aUsdc polygon
Aave v2 avalanche pool Aave v2 avalanche pool
Aave v3 Pool optimism -

Payloads:

  • Ethereum Payload:
    • Updates v1 pool with rescue function
    • Updates v2 pool with rescue function
    • Updates v2 amm pool with rescue function
    • Updates v2 aRai contract with rescue function
    • Updates v2 aUsdt contract with rescue function
    • Registers MerkleRoot for each token on the Merkle distributor contract.
    • Transfers aRai, aBtc, Usdt, Usdc, Dai, Gusd, Link tokens to the Merkle distributor from the aave contracts where funds were stuck
  • Polygon Payload:
    • Updates v2 pool with rescue function
    • Updates v2 aDai contract with rescue function
    • Updates v2 aUdsc contract with rescue function
    • Registers MerkleRoot for each token on the Merkle distributor contract.
    • Transfers Wbtc, aDai, aUsdc, Usdc tokens to the Merkle distributor contract.
  • Optimism Payload
    • Registers MerkleRoot for token on the Merkle distributor contract.
    • Transfers Usdc token to the Merkle distributor contract.
  • Avalanche Payload:
    • This payload should be called by the owner of addresses provider / pool admin / guardian.
    • Updates v2 pool with rescue function
    • Registers MerkleRoot for each token on the merkle distributor contract.
    • Transfers Usdc.e Usdt.e from the v2 to the merkle distributor contract.
    • Transfers Usdc.e from the wethGateway contract to the merkle distributor contract.

Tests:

To run the tests:

forge test

To test all the claims on tenderly for all the users:

npm run test-eth-claims
npm run test-pol-claims
npm run test-ava-claims
npm run test-opt-claims

License

Copyright © 2023, BGD Labs. Released under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published