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

Feature: Allows whitehats to use custom logic to track balances #31

Merged
merged 1 commit into from
Dec 16, 2023
Merged

Feature: Allows whitehats to use custom logic to track balances #31

merged 1 commit into from
Dec 16, 2023

Conversation

infosec-us-team
Copy link
Contributor

Vulnerability Type

This commit introduces the snapshotBalance(address _user, IERC20[] memory _tokens, uint256[] memory _amounts) function, which allows the use of custom logic to track balances for specific tokens and amounts.

This functionality is useful for tracking virtual balances, for example:

  • Tracking an attacker's virtual balance of underlying tokens inside a smart contract that holds funds from several users at the same time, and over time accrues interest on these funds.

Function details:

  • snapshotBalance takes three arguments:
    • _user: The address of the attacker to be tracked.
    • _tokens: An array of IERC20 tokens to track the balance for.
    • _amounts: An array of corresponding token amounts to snapshot.
  • The function returns an array of TokenBalance structs, each containing the address of the token and its snapshotted balance as an int256.

Usage

An example of usage is: Tracking the virtual balance of a loan in GammaSwap protocol before and after executing an exploit, then printing the profit.

// Initialize an array of tokens to track
IERC20[] tokens;
tokens.push(WETH);
tokens.push(USDC);

// Get the virtual balance of underlying collateral for a loan, in a GammaSwap pool
IGammaPool.LoanData memory data = viewer.loan(address(pool), loanId);
uint256[] memory amounts = new uint256[](2);
amounts[0] = data.tokensHeld[0];
amounts[1] = data.tokensHeld[1];

// Snapshot the balance of the attacker
snapshotBalance(address(this), tokens, amounts);

// Run the exploit
exploit();

// Get the new virtual balance of underlying collateral
data = viewer.loan(address(pool), loanId);
amounts[0] = data.tokensHeld[0];
amounts[1] = data.tokensHeld[1];

// Snapshot the balance of the attacker
snapshotBalance(address(this), tokens, amounts);

// Print the profit of the attacker
printProfit(address(this));

PR Checklist

  • Attack Template
  • Attack Example
  • Test
  • Readme Updated

This commit introduces the `snapshotBalance(address _user, IERC20[] memory _tokens, uint256[] memory _amounts)` function, which allows the use of custom logic to track balances for specific tokens and amounts.

 This functionality is useful for tracking virtual balances and advanced vaults, for example:

- Tracking an attacker's virtual balance of underlying tokens inside a smart contract that holds funds from several users at the same time, and over time accrues interest on these funds.

**Function details:**

* `snapshotBalance` takes three arguments:
    * `_user`: The address of the attacker to be tracked.
    * `_tokens`: An array of IERC20 tokens to track the balance for.
    * `_amounts`: An array of corresponding token amounts to snapshot.
* The function returns an array of `TokenBalance` structs, each containing the address of the token and its snapshotted balance as an `int256`.
@janbro janbro self-requested a review December 16, 2023 12:22
Copy link
Collaborator

@janbro janbro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@janbro janbro merged commit 2c26129 into immunefi-team:main Dec 16, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants