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

FeeAdapter debit round up #10940

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,18 @@ contract FeeCurrencyAdapter is Initializable, CalledByVm, IFeeCurrencyAdapter {
return value * digitDifference;
}

/**
* @notice Downscales value to the adapted token's native digits.
* @dev Downscale is rounding up in favour of protocol. User possibly can pay a bit more than expected (up to 1 unit of a token).
* Example:
* USDC has 6 decimals and in such case user can pay up to 0.000001 USDC more than expected.
* WBTC (currently not supported by Celo chain as fee currency) has 8 decimals and in such case user can pay up to 0.00000001 WBTC more than expected.
* Considering the current price of WBTC, it's less than 0.0005 USD. Even when WBTC price would be 1 mil USD, it's still would be only 0.01 USD.
* In general it is a very small amount and it is acceptable to round up in favor of the protocol.
* @param value The value to downscale.
*/
function downscale(uint256 value) internal view returns (uint256) {
pahor167 marked this conversation as resolved.
Show resolved Hide resolved
return value / digitDifference;
return (value + digitDifference - 1) / digitDifference;
}

function _setAdaptedToken(address _adaptedToken) internal virtual {
Expand Down
10 changes: 5 additions & 5 deletions packages/protocol/test-sol/stability/FeeCurrencyAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ contract FeeCurrencyAdapter_DebitGasFees is FeeCurrencyAdapterTest {
function test_ShouldRevert_WhenScaledDebitValueIs0() public {
vm.expectRevert("Scaled debit value must be > 0.");
vm.prank(address(0));
feeCurrencyAdapter.debitGasFees(address(this), 1e7);
feeCurrencyAdapter.debitGasFees(address(this), 0);
}

function test_ShouldDebitCorrectAmount_WhenExpectedDigitsOnlyOneBigger() public {
Expand Down Expand Up @@ -372,10 +372,10 @@ contract FeeCurrencyAdapter_UpscaleAndDownScaleTests is FeeCurrencyAdapterTest {
assertEq(feeCurrencyAdapter.downscaleVisible(1e24), 1e12);
}

function test_ShouldReturn0_WhenSmallEnough() public {
assertEq(feeCurrencyAdapter.downscaleVisible(1), 0);
assertEq(feeCurrencyAdapter.downscaleVisible(1e6 - 1), 0);
assertEq(feeCurrencyAdapter.downscaleVisible(1e12 - 1), 0);
function test_ShouldReturn1_WhenSmallEnoughAndRoundingUp() public {
assertEq(feeCurrencyAdapter.downscaleVisible(1), 1);
assertEq(feeCurrencyAdapter.downscaleVisible(1e6 - 1), 1);
assertEq(feeCurrencyAdapter.downscaleVisible(1e12 - 1), 1);
}
}

Expand Down
Loading