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

Gas: Merge callbacks to Unlock on purchase #126

Open
code423n4 opened this issue Nov 24, 2021 · 2 comments
Open

Gas: Merge callbacks to Unlock on purchase #126

code423n4 opened this issue Nov 24, 2021 · 2 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

HardlyDifficult

Vulnerability details

Impact

Save 1.7k gas on a key purchase when there's no discount, and 3.2k gas when discounts are added. A minor improvement, but this also seems easier to follow in trace logs or with a tool like Tenderly.

Proof of Concept

In the PublicLock purchase remove the calls to _purchasePriceFor and recordKeyPurchase and replace it with uint inMemoryKeyPrice = _purchase(_recipient, _referrer, _data); which was implemented as:

 function _purchase(
    address _recipient,
    address _referrer,
    bytes memory _data
  ) internal
    returns (uint minKeyPrice)
  {
    if(address(onKeyPurchaseHook) != address(0))
    {
      minKeyPrice = onKeyPurchaseHook.keyPurchasePrice(msg.sender, _recipient, _referrer, _data);
    }
    else
    {
      minKeyPrice = keyPrice;
    }

    return unlockProtocol.recordKeyPurchaseAndGetDiscount(_recipient, minKeyPrice, _referrer);
  }

Then in Unlock, add a new function:

  function recordKeyPurchaseAndGetDiscount(
    address recipient,
    uint minKeyPrice,
    address referrer
  ) public returns (uint inMemoryKeyPrice) {
    if(minKeyPrice > 0)
    {
      (uint unlockDiscount, uint unlockTokens) = computeAvailableDiscountFor(recipient, minKeyPrice);
      inMemoryKeyPrice = minKeyPrice;
      if (unlockDiscount > 0)
      {
        // INVALID_DISCOUNT_FROM_UNLOCK error assumed from safe math
        inMemoryKeyPrice -= unlockDiscount;
        recordConsumedDiscount(unlockDiscount, unlockTokens);
      }
    }

    // Record price without any tips
    recordKeyPurchase(inMemoryKeyPrice, referrer);
  }

This approach clones the current behavior, but moves logic into Unlock instead of going back and forth between PublicLock and Unlock.

Tools Used

yarn test and diff the gas reporter table.

Recommended Mitigation Steps

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 24, 2021
code423n4 added a commit that referenced this issue Nov 24, 2021
@0xleastwood
Copy link
Collaborator

I'll let @julien51 comment on this one.

@julien51
Copy link
Collaborator

Makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants