Algo Ssstablecoinsss

First Flight #30
Beginner FriendlyDeFi
100 EXP
View results
Submission Details
Severity: low
Invalid

Suboptimal gas consumption in get_account_collateral_value

Summary

The _get_account_collateral_value function can be optimized by skipping zero-amount collateral positions when calculating total collateral value, reducing unnecessary price feed calls and calculations.

Proof of Code

Add the following optimized functions in dsc_engine.vy:

@internal
@view
def _get_account_information_optimized(user: address) -> (uint256, uint256):
total_dsc_minted: uint256 = self.user_to_dsc_minted[user]
collateral_value_in_usd: uint256 = self._get_account_collateral_value_optimized(user)
return total_dsc_minted, collateral_value_in_usd
@internal
@view
def _get_account_collateral_value_optimized(user: address) -> uint256:
total_collateral_value_in_usd: uint256 = 0
for token: address in COLLATERAL_TOKENS:
amount: uint256 = self.user_to_token_address_to_amount_deposited[user][
token
]
if amount > 0:
price: uint256 = self._get_usd_value(token, amount)
total_collateral_value_in_usd += price
return total_collateral_value_in_usd
@external
@view
def get_account_information_optimized(user: address) -> (uint256, uint256):
return self._get_account_information_optimized(user)
@external
@view
def get_account_collateral_value_optimized(user: address) -> uint256:
return self._get_account_collateral_value_optimized(user)

Add this test to tests/unit/test_dsc_engine.py:

@pytest.mark.gas_profile
def test_get_account_information_gas_comparison(dsce, weth, eth_usd):
"""Compare gas usage between original and optimized get_account_information"""
USER = boa.env.generate_address()
# Setup initial price
INITIAL_PRICE = 2_000 * 10**8 # $2000 USD
eth_usd.updateAnswer(INITIAL_PRICE)
# Setup test amounts
DEPOSIT_AMOUNT = to_wei(1, "ether")
MINT_AMOUNT = to_wei(500, "ether")
# Setup user position
weth.mint(USER, DEPOSIT_AMOUNT)
with boa.env.prank(USER):
weth.approve(dsce.address, DEPOSIT_AMOUNT)
dsce.deposit_collateral_and_mint_dsc(weth, DEPOSIT_AMOUNT, MINT_AMOUNT)
# Get values using both methods
original_minted, original_collateral = dsce.get_account_information(USER)
optimized_minted, optimized_collateral = dsce.get_account_information_optimized(USER)
# Verify both methods return same values
assert original_minted == optimized_minted, "Minted amounts should match"
assert original_collateral == optimized_collateral, "Collateral values should match"

Reported Gas Usage:

Function Gas Usage
get_account_information 1259
get_account_information_optimized 913

The optimized function reduces gas usage by approximately 27%, as demonstrated in the test results.
As more collateral assets are added, the gas savings from this optimization will scale, providing even greater benefits.

Tools Used

  • Manual Review

Updates

Lead Judging Commences

bube Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.