Skip to content

Commit 885ccfe

Browse files
committed
fix: add SafeCastLib.toUint32 for fromSupply
1 parent 686d969 commit 885ccfe

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

contracts/core/SharesFactoryV1.sol

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity 0.8.25;
44

55
import "@openzeppelin/contracts/access/Ownable2Step.sol";
66
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
7+
import { SafeCastLib } from "solady/utils/SafeCastLib.sol";
78
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
89
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
910
import { IShare } from "../interface/IShare.sol";
@@ -309,7 +310,7 @@ contract SharesFactoryV1 is Ownable2Step, ReentrancyGuard {
309310
uint256 fromSupply = IShare(ERC1155).shareFromSupply(shareId);
310311
uint256 actualReferralFeePercent = referral != address(0) ? referralFeePercent : 0;
311312

312-
buyPrice = getSubTotal(uint32(fromSupply), quantity, curveType);
313+
buyPrice = getSubTotal(SafeCastLib.toUint32(fromSupply), quantity, curveType);
313314
referralFee = (buyPrice * actualReferralFeePercent) / 1 ether;
314315
creatorFee = (buyPrice * creatorFeePercent) / 1 ether;
315316
buyPriceAfterFee = buyPrice + referralFee + creatorFee;
@@ -341,7 +342,7 @@ contract SharesFactoryV1 is Ownable2Step, ReentrancyGuard {
341342
uint256 actualReferralFeePercent = referral != address(0) ? referralFeePercent : 0;
342343
require(fromSupply >= quantity, "Exceeds supply");
343344

344-
sellPrice = getSubTotal(uint32(fromSupply) - quantity, quantity, curveType);
345+
sellPrice = getSubTotal(SafeCastLib.toUint32(fromSupply) - quantity, quantity, curveType);
345346
referralFee = (sellPrice * actualReferralFeePercent) / 1 ether;
346347
creatorFee = (sellPrice * creatorFeePercent) / 1 ether;
347348
sellPriceAfterFee = sellPrice - referralFee - creatorFee;

test/unit/SharesFactory.t.sol

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pragma solidity 0.8.25;
33

44
import { console } from "forge-std/console.sol";
5+
import { SafeCastLib } from "solady/utils/SafeCastLib.sol";
56
import { SharesFactoryV1 } from "contracts/core/SharesFactoryV1.sol";
67
import { IYieldAggregator } from "contracts/interface/IYieldAggregator.sol";
78
import { BaseTest } from "../BaseTest.t.sol";
@@ -92,11 +93,6 @@ contract SharesFactoryTests is BaseTest {
9293
assertEq(bobShareBal, 2);
9394
}
9495

95-
function test_buyShareWithHighVolume() public view {
96-
(uint256 buyPriceAfterFee,,,) = sharesFactory.getBuyPriceAfterFee(0, 100000, referralReceiver);
97-
console.log("test_buyShareWithHighVolume", buyPriceAfterFee);
98-
}
99-
10096
function test_sellShares() public {
10197
uint256 aliceBalBefore = addrAlice.balance;
10298
uint256 bobBalBefore = addrBob.balance;
@@ -385,6 +381,19 @@ contract SharesFactoryTests is BaseTest {
385381
}
386382

387383
function test_getBuyPriceAfterFeeFailed() public {
384+
// When the quantity is 5_000 that reach th
385+
uint256 gasBefore = gasleft();
386+
sharesFactory.getBuyPriceAfterFee(0, 5_000, referralReceiver);
387+
uint256 gasAfter = gasleft();
388+
console.log("gas usage", gasBefore - gasAfter);
389+
390+
// Expect revert if supply is over `2**32 -1` (uint32)
391+
vm.expectRevert();
392+
sharesFactory.getSubTotal(SafeCastLib.toUint32(2**32), 1, 0);
393+
394+
// Expect success if supply is lower than `2**32` (uint32)
395+
sharesFactory.getSubTotal(SafeCastLib.toUint32(2**32 - 1), 1, 0);
396+
388397
vm.expectRevert(bytes("Invalid shareId"));
389398
sharesFactory.getBuyPriceAfterFee(999, 0, referralReceiver);
390399

@@ -525,7 +534,6 @@ contract SharesFactoryTests is BaseTest {
525534

526535
function _buyShare(address sender, uint256 shareId, uint32 quantity, address referral) internal {
527536
(uint256 buyPriceAfterFee,,,) = sharesFactory.getBuyPriceAfterFee(shareId, quantity, referral);
528-
// console.log("buyPriceAfterFee", buyPriceAfterFee, shareId, quantity, referral);
529537
vm.prank(address(sender));
530538
sharesFactory.buyShare{ value: buyPriceAfterFee }(shareId, quantity, referral);
531539
}

0 commit comments

Comments
 (0)