Skip to content

Commit

Permalink
fix: trading fee payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Marinković authored and Aleksandar Marinković committed Jun 28, 2023
1 parent a1f7640 commit ea2f9a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
14 changes: 5 additions & 9 deletions src/diamonds/nayms/libs/LibFeeRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,8 @@ library LibFeeRouter {

FeeSchedule memory feeSchedule = _getFeeSchedule(_buyer, LibConstants.FEE_TYPE_TRADING);

uint256 totalReceiverCount;
if (s.tradingCommissionMakerBP > 0) {
totalReceiverCount++;
}

uint256 feeScheduleReceiversCount = feeSchedule.receiver.length;
totalReceiverCount += feeScheduleReceiversCount;
uint256 totalReceiverCount = (s.tradingCommissionMakerBP > 0) ? feeScheduleReceiversCount + 1 : feeScheduleReceiversCount;

cf.feeAllocations = new FeeAllocation[](totalReceiverCount);

Expand All @@ -112,7 +107,7 @@ library LibFeeRouter {
receiverCount++;
}

for (uint256 i; i < feeScheduleReceiversCount; ++i) {
for (uint256 i; i < feeScheduleReceiversCount; i++) {
cf.feeAllocations[receiverCount + i].to = feeSchedule.receiver[i];
cf.feeAllocations[receiverCount + i].basisPoints = feeSchedule.basisPoints[i];
cf.feeAllocations[receiverCount + i].fee = (_buyAmount * feeSchedule.basisPoints[i]) / LibConstants.BP_FACTOR;
Expand All @@ -134,19 +129,20 @@ library LibFeeRouter {
AppStorage storage s = LibAppStorage.diamondStorage();

// Get the fee receivers for this _feeScheduleType
FeeSchedule memory feeSchedule = s.feeSchedules[_buyer][_feeScheduleType];
FeeSchedule memory feeSchedule = _getFeeSchedule(_buyer, _feeScheduleType);

uint256 fee;
// Calculate fees for the market maker
if (s.tradingCommissionMakerBP > 0) {
fee = (_buyAmount * s.tradingCommissionMakerBP) / LibConstants.BP_FACTOR;
totalFees_ += fee;

emit TradingFeePaid(_takerId, _makerId, _tokenId, fee);
LibTokenizedVault._internalTransfer(_takerId, _makerId, _tokenId, fee);
}

uint256 feeScheduleReceiversCount = feeSchedule.receiver.length;
for (uint256 i; i < feeScheduleReceiversCount; ++i) {
for (uint256 i; i < feeScheduleReceiversCount; i++) {
fee = (_buyAmount * feeSchedule.basisPoints[i]) / LibConstants.BP_FACTOR;
totalFees_ += fee;

Expand Down
3 changes: 2 additions & 1 deletion test/NewFees.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ contract NewFeesTest is D03ProtocolDefaults {

assertEq(makerBP, cf.feeAllocations[0].basisPoints, "maker bp is incorrect");

assertEq(nayms.internalBalanceOf(acc2.entityId, wethId), 0.5 ether - cf.totalFees, "entity's weth balance is incorrect");
assertEq(nayms.internalBalanceOf(acc1.entityId, wethId), 0.5 ether + ((0.5 ether * makerBP) / LibConstants.BP_FACTOR), "makers's weth balance is incorrect");
assertEq(nayms.internalBalanceOf(acc2.entityId, wethId), (0.5 ether - cf.totalFees), "taker's weth balance is incorrect");
}

function test_startTokenSale_FirstTokenSale() public {
Expand Down

0 comments on commit ea2f9a9

Please sign in to comment.