-
Notifications
You must be signed in to change notification settings - Fork 35
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
Feat/audit part6 #165
Feat/audit part6 #165
Changes from 3 commits
ec378fe
c6e6ff6
acc40fc
8c2ce1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
24241 | ||
24243 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
119705 | ||
119403 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
971865 | ||
971563 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
331176 | ||
330874 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
338777 | ||
338475 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
141327 | ||
141025 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
174101 | ||
173950 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
180130 | ||
179979 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
134132 | ||
133981 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
305605 | ||
305454 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
21178 | ||
21207 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
348525 | ||
348246 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
163996 | ||
163717 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
239021 | ||
239044 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
164280 | ||
163978 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
109095 | ||
108944 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
115495 | ||
115518 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
131890 | ||
131739 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
164446 | ||
164295 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
149907 | ||
149756 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
7931 | ||
7891 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,10 +122,12 @@ contract Vault is IVault, VaultToken, Ownable { | |
} | ||
|
||
function sync(Currency currency) public override isLocked { | ||
VaultReserve.alreadySettledLastSync(); | ||
if (currency.isNative()) return; | ||
uint256 balance = currency.balanceOfSelf(); | ||
VaultReserve.setVaultReserve(currency, balance); | ||
if (currency.isNative()) { | ||
VaultReserve.setVaultReserve(CurrencyLibrary.NATIVE, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder if we can have a helper function so its clearer eg. and within the helper function, we can document why its set currencyLibrary.Native and 0 amount There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if agreeable, we can create an issue to tackle in another PR (so it doesn't pollute this) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😎hello |
||
} else { | ||
uint256 balance = currency.balanceOfSelf(); | ||
VaultReserve.setVaultReserve(currency, balance); | ||
} | ||
} | ||
|
||
/// @inheritdoc IVault | ||
|
@@ -186,7 +188,7 @@ contract Vault is IVault, VaultToken, Ownable { | |
uint256 reservesNow = currency.balanceOfSelf(); | ||
paid = reservesNow - reservesBefore; | ||
|
||
/// @dev reset the reserve after settled otherwise next sync() call will throw LastSyncNotSettled | ||
/// @dev reset the reserve after settled | ||
VaultReserve.setVaultReserve(CurrencyLibrary.NATIVE, 0); | ||
} else { | ||
// NATIVE token does not require sync call before settle | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
/// @title For calculating a percentage of an amount, using bips | ||
library BipsLibrary { | ||
uint256 internal constant BPS_DENOMINATOR = 10_000; | ||
|
||
/// @notice emitted when an invalid percentage is provided | ||
error InvalidBips(); | ||
|
||
/// @param amount The total amount to calculate a percentage of | ||
/// @param bips The percentage to calculate, in bips | ||
function calculatePortion(uint256 amount, uint256 bips) internal pure returns (uint256) { | ||
if (bips > BPS_DENOMINATOR) revert InvalidBips(); | ||
return (amount * bips) / BPS_DENOMINATOR; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
import {BipsLibrary} from "../../src/libraries/BipsLibrary.sol"; | ||
|
||
contract BipsLibraryTest is Test { | ||
using BipsLibrary for uint256; | ||
|
||
// The block gas limit set in foundry config is 300_000_000 (300M) for testing purposes | ||
uint256 BLOCK_GAS_LIMIT; | ||
|
||
function setUp() public { | ||
BLOCK_GAS_LIMIT = block.gaslimit; | ||
} | ||
|
||
function test_fuzz_calculatePortion(uint256 amount, uint256 bips) public { | ||
amount = bound(amount, 0, uint256(type(uint128).max)); | ||
if (bips > BipsLibrary.BPS_DENOMINATOR) { | ||
vm.expectRevert(BipsLibrary.InvalidBips.selector); | ||
amount.calculatePortion(bips); | ||
} else { | ||
assertEq(amount.calculatePortion(bips), amount * bips / BipsLibrary.BPS_DENOMINATOR); | ||
} | ||
} | ||
|
||
function test_fuzz_gasLimit(uint256 bips) public { | ||
if (bips > BipsLibrary.BPS_DENOMINATOR) { | ||
vm.expectRevert(BipsLibrary.InvalidBips.selector); | ||
block.gaslimit.calculatePortion(bips); | ||
} else { | ||
assertEq(block.gaslimit.calculatePortion(bips), BLOCK_GAS_LIMIT * bips / BipsLibrary.BPS_DENOMINATOR); | ||
} | ||
} | ||
|
||
function test_gasLimit_100_percent() public view { | ||
assertEq(block.gaslimit, block.gaslimit.calculatePortion(10_000)); | ||
} | ||
|
||
function test_gasLimit_1_percent() public view { | ||
// 100 bps = 1% | ||
// 1% of 30M is 300K | ||
assertEq(BLOCK_GAS_LIMIT / 100, block.gaslimit.calculatePortion(100)); | ||
} | ||
|
||
function test_gasLimit_1BP() public view { | ||
// 1bp is 0.01% | ||
// 0.01% of 30M is 300 | ||
assertEq(BLOCK_GAS_LIMIT / 10000, block.gaslimit.calculatePortion(1)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(just comment) this mean likely 300k on eth, and 1mil gas on bsc chain