Skip to content

OWR can be simplified and save gas by not tracking distributedFunds #80

@zobront

Description

@zobront

Currently, the OptimisticWithdrawalRecipient contract tracks four variables:

  • distributedFunds: total amount of the token distributed via push or pull
  • fundsPendingWithdrawal: total balance distributed via pull that haven't been claimed yet
  • claimedPrincipalFunds: total amount of funds claimed by the principal recipient
  • pullBalances: individual pull balances that haven't been claimed yet

When _distributeFunds() is called, we perform the following math (simplified to only include relevant updates):

endingDistributedFunds = distributedFunds - fundsPendingWithdrawal + currentBalance;
fundsToBeDistributed = endingDistributedFunds - distributedFunds;
distributedFunds = endingDistributedFunds;

As we can see, distributedFunds is added to the endingDistributedFunds variable and then removed when calculating fundsToBeDistributed, having no impact on the resulting fundsToBeDistributed value.

The distributedFunds variable is not read or used anywhere else on the contract.

Recommendation

We can simplify the math and save substantial gas (a storage write plus additional operations) by not tracking this value at all.

This would allow us to calculate fundsToBeDistributed directly, as follows:

fundsToBeDistributed = currentBalance - fundsPendingWithdrawal;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions