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

feedback/audit #34

Merged
merged 10 commits into from
Jul 2, 2024
5 changes: 3 additions & 2 deletions contracts/core/SharesFactoryV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

pragma solidity 0.8.25;

import "@openzeppelin/contracts/access/Ownable2Step.sol";

Check warning on line 5 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

global import of path @openzeppelin/contracts/access/Ownable2Step.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 5 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

global import of path @openzeppelin/contracts/access/Ownable2Step.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

Check warning on line 6 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

global import of path @openzeppelin/contracts/security/ReentrancyGuard.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 6 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

global import of path @openzeppelin/contracts/security/ReentrancyGuard.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import { SafeCastLib } from "solady/utils/SafeCastLib.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IShare } from "../interface/IShare.sol";
Expand Down Expand Up @@ -88,7 +89,7 @@
}

function getSubTotal(uint32 fromSupply, uint32 quantity, uint8 curveType) public view returns (uint256) {
(uint96 basePrice, uint32 inflectionPoint, uint128 inflectionPrice, uint128 linearPriceSlope,) = getCurve(curveType);

Check warning on line 92 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

Line length must be no more than 123 but current length is 125

Check warning on line 92 in contracts/core/SharesFactoryV1.sol

View workflow job for this annotation

GitHub Actions / lint (18)

Line length must be no more than 123 but current length is 125
return _subTotal(fromSupply, quantity, basePrice, inflectionPoint, inflectionPrice, linearPriceSlope);
}

Expand Down Expand Up @@ -309,7 +310,7 @@
uint256 fromSupply = IShare(ERC1155).shareFromSupply(shareId);
uint256 actualReferralFeePercent = referral != address(0) ? referralFeePercent : 0;

buyPrice = getSubTotal(uint32(fromSupply), quantity, curveType);
buyPrice = getSubTotal(SafeCastLib.toUint32(fromSupply), quantity, curveType);
0xashu marked this conversation as resolved.
Show resolved Hide resolved
referralFee = (buyPrice * actualReferralFeePercent) / 1 ether;
creatorFee = (buyPrice * creatorFeePercent) / 1 ether;
buyPriceAfterFee = buyPrice + referralFee + creatorFee;
Expand Down Expand Up @@ -341,7 +342,7 @@
uint256 actualReferralFeePercent = referral != address(0) ? referralFeePercent : 0;
require(fromSupply >= quantity, "Exceeds supply");

sellPrice = getSubTotal(uint32(fromSupply) - quantity, quantity, curveType);
sellPrice = getSubTotal(SafeCastLib.toUint32(fromSupply) - quantity, quantity, curveType);
0xashu marked this conversation as resolved.
Show resolved Hide resolved
referralFee = (sellPrice * actualReferralFeePercent) / 1 ether;
creatorFee = (sellPrice * creatorFeePercent) / 1 ether;
sellPriceAfterFee = sellPrice - referralFee - creatorFee;
Expand Down
20 changes: 14 additions & 6 deletions test/unit/SharesFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.25;

import { console } from "forge-std/console.sol";
import { SafeCastLib } from "solady/utils/SafeCastLib.sol";
import { SharesFactoryV1 } from "contracts/core/SharesFactoryV1.sol";
import { IYieldAggregator } from "contracts/interface/IYieldAggregator.sol";
import { BaseTest } from "../BaseTest.t.sol";
Expand Down Expand Up @@ -92,11 +93,6 @@ contract SharesFactoryTests is BaseTest {
assertEq(bobShareBal, 2);
}

function test_buyShareWithHighVolume() public view {
(uint256 buyPriceAfterFee,,,) = sharesFactory.getBuyPriceAfterFee(0, 100000, referralReceiver);
console.log("test_buyShareWithHighVolume", buyPriceAfterFee);
}

function test_sellShares() public {
uint256 aliceBalBefore = addrAlice.balance;
uint256 bobBalBefore = addrBob.balance;
Expand Down Expand Up @@ -385,6 +381,19 @@ contract SharesFactoryTests is BaseTest {
}

function test_getBuyPriceAfterFeeFailed() public {
// When the quantity is 5_000 that reach th
uint256 gasBefore = gasleft();
sharesFactory.getBuyPriceAfterFee(0, 5_000, referralReceiver);
uint256 gasAfter = gasleft();
console.log("gas usage", gasBefore - gasAfter);

// Expect revert if supply is over `2**32 -1` (uint32)
vm.expectRevert();
sharesFactory.getSubTotal(SafeCastLib.toUint32(2**32), 1, 0);

// Expect success if supply is lower than `2**32` (uint32)
sharesFactory.getSubTotal(SafeCastLib.toUint32(2**32 - 1), 1, 0);

vm.expectRevert(bytes("Invalid shareId"));
sharesFactory.getBuyPriceAfterFee(999, 0, referralReceiver);

Expand Down Expand Up @@ -525,7 +534,6 @@ contract SharesFactoryTests is BaseTest {

function _buyShare(address sender, uint256 shareId, uint32 quantity, address referral) internal {
(uint256 buyPriceAfterFee,,,) = sharesFactory.getBuyPriceAfterFee(shareId, quantity, referral);
// console.log("buyPriceAfterFee", buyPriceAfterFee, shareId, quantity, referral);
vm.prank(address(sender));
sharesFactory.buyShare{ value: buyPriceAfterFee }(shareId, quantity, referral);
}
Expand Down
Loading