Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC profit snapshotting is affected when used in nested functions #24

Open
janbro opened this issue Sep 27, 2023 · 0 comments
Open

PoC profit snapshotting is affected when used in nested functions #24

janbro opened this issue Sep 27, 2023 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@janbro
Copy link
Collaborator

janbro commented Sep 27, 2023

Snapshotting profit is printed incorrectly when used in nested functions. This is because the index of the initial balance snapshot is not recorded and passed to the printProfit function (always uses index 0 as the initial condition). Should be an easy fix to record the index of the initial snapshot and use that when calculating profit. However, formatting is not great. This may just be a limitation of using modifiers for snapshotting functionality, but logs get very messy when snapshotting is used in nested functions. This may be able to be solved with formatting with more information when printing.

ex.

function setUp() public {
        // Fork from specified block chain at block
        vm.createSelectFork("https://rpc.ankr.com/eth"); // , block_number);

        // Deploy attack contract
        attackContract = new AttackContract();

        // Fund attacker contract
        deal(EthereumTokens.USDC, address(attackContract), 1 * 10 ** 10);

        // Tokens to track during snapshotting
        tokens.push(EthereumTokens.USDC);

        setAlias(address(attackContract), "Attacker");

        console.log("\n>>> Initial conditions");
    }

    function testAttack() public snapshot(address(attackContract), tokens) {
        attackContract.initializeAttack();
        doSomething();
    }

    function doSomething() public snapshot(address(attackContract), tokens) {

    }

ex output:

Logs:
  
>>> Initial conditions
  --- USDC balance of [Attacker]:       10000.0 ---
  
  
>>> Initialize attack
  
>>> Execute attack
  
>>> Complete attack
  --- USDC balance of [Attacker]:       0.0 ---
  
  --- USDC balance of [Attacker]:       0.0 ---
  
  ~~~ Profit for [Attacker]
  -----------------------------------------------------------------------------------------
               Token address                    |       Symbol  |       Profit
  -----------------------------------------------------------------------------------------
  0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48    |       USDC    |       -10000.0
  
  --- USDC balance of [Attacker]:       0.0 ---
  
  ~~~ Profit for [Attacker]
  -----------------------------------------------------------------------------------------
               Token address                    |       Symbol  |       Profit
  -----------------------------------------------------------------------------------------
  0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48    |       USDC    |       -10000.0
  
@janbro janbro added bug Something isn't working good first issue Good for newcomers labels Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant