Skip to content

Commit

Permalink
refactor Fee.fixedFee to Amount (closes #651)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Aug 30, 2024
1 parent 9042a07 commit ab535f1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 36 deletions.
4 changes: 2 additions & 2 deletions contracts/accounting/IAccountingService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ interface IAccountingService is
NftId nftId,
string feeName,
UFixed previousFractionalFee,
uint256 previousFixedFee,
Amount previousFixedFee,
UFixed newFractionalFee,
uint256 newFixedFee
Amount newFixedFee
);

function decreaseComponentFees(InstanceStore instanceStore, NftId componentNftId, Amount feeAmount) external;
Expand Down
12 changes: 6 additions & 6 deletions contracts/product/PricingService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ contract PricingService is
revert ErrorPricingServiceTargetWalletAmountsMismatch();
}

if (premium.distributionOwnerFeeFixAmount.toInt() < feeInfo.minDistributionOwnerFee.fixedFee) {
if (premium.distributionOwnerFeeFixAmount < feeInfo.minDistributionOwnerFee.fixedFee) {
revert ErrorPricingServiceFeeCalculationMismatch(
premium.distributionFeeFixAmount,
premium.distributionFeeVarAmount,
Expand Down Expand Up @@ -185,19 +185,19 @@ contract PricingService is
premium.netPremiumAmount = netPremiumAmount;
premium.fullPremiumAmount = netPremiumAmount;

Amount t = AmountLib.toAmount(feeInfo.productFee.fixedFee);
Amount t = feeInfo.productFee.fixedFee;
premium.productFeeFixAmount = t;
premium.fullPremiumAmount = premium.fullPremiumAmount + t;

t = AmountLib.toAmount(feeInfo.poolFee.fixedFee);
t = feeInfo.poolFee.fixedFee;
premium.poolFeeFixAmount = t;
premium.fullPremiumAmount = premium.fullPremiumAmount + t;

t = AmountLib.toAmount(bundleInfo.fee.fixedFee);
t = bundleInfo.fee.fixedFee;
premium.bundleFeeFixAmount = t;
premium.fullPremiumAmount = premium.fullPremiumAmount + t;

t = AmountLib.toAmount(feeInfo.distributionFee.fixedFee);
t = feeInfo.distributionFee.fixedFee;
premium.distributionFeeFixAmount = t;
premium.fullPremiumAmount = premium.fullPremiumAmount + t;
}
Expand Down Expand Up @@ -267,7 +267,7 @@ contract PricingService is
Amount commissionAmount = premium.netPremiumAmount.multiplyWith(distributorTypeInfo.commissionPercentage);
premium.commissionAmount = commissionAmount;
premium.discountAmount = premium.fullPremiumAmount.multiplyWith(referralInfo.discountPercentage);
premium.distributionOwnerFeeFixAmount = AmountLib.toAmount(minDistributionOwnerFee.fixedFee);
premium.distributionOwnerFeeFixAmount = minDistributionOwnerFee.fixedFee;
premium.distributionOwnerFeeVarAmount = premium.distributionFeeVarAmount - commissionAmount - premium.discountAmount;
premium.premiumAmount = premium.fullPremiumAmount - premium.discountAmount;
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/shared/IComponentService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ interface IComponentService is
NftId nftId,
string feeName,
UFixed previousFractionalFee,
uint256 previousFixedFee,
Amount previousFixedFee,
UFixed newFractionalFee,
uint256 newFixedFee
Amount newFixedFee
);

//-------- component ----------------------------------------------------//
Expand Down
15 changes: 7 additions & 8 deletions contracts/type/Fee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ import {UFixed, UFixedLib} from "./UFixed.sol";

struct Fee {
UFixed fractionalFee;
// TODO migrate fixed fee to Amount
uint256 fixedFee;
Amount fixedFee;
}

library FeeLib {

/// @dev Return a zero fee struct (0, 0)
function zero() public pure returns (Fee memory fee) {
return Fee(UFixed.wrap(0), 0);
return Fee(UFixed.wrap(0), AmountLib.zero());
}

/// @dev Converts the uint256 to a fee struct.
function toFee(
UFixed fractionalFee,
uint256 fixedFee
) public pure returns (Fee memory fee) {
return Fee(fractionalFee, fixedFee);
return Fee(fractionalFee, AmountLib.toAmount(fixedFee));
}

/// @dev Calculates fee and net amounts for the provided parameters
Expand All @@ -41,14 +40,14 @@ library FeeLib {
if(gtz(fee)) {
UFixed fractionalAmount =
amount.toUFixed() * fee.fractionalFee;
feeAmount = AmountLib.toAmount(fractionalAmount.toInt() + fee.fixedFee);
feeAmount = AmountLib.toAmount(fractionalAmount.toInt()) + fee.fixedFee;
netAmount = netAmount - feeAmount;
}
}

/// @dev Return the percent fee struct (x%, 0)
function percentageFee(uint8 percent) public pure returns (Fee memory fee) {
return Fee(UFixedLib.toUFixed(percent, -2), 0);
return Fee(UFixedLib.toUFixed(percent, -2), AmountLib.zero());
}

// pure free functions for operators
Expand All @@ -57,10 +56,10 @@ library FeeLib {
}

function gtz(Fee memory fee) public pure returns (bool) {
return UFixed.unwrap(fee.fractionalFee) > 0 || fee.fixedFee > 0;
return UFixed.unwrap(fee.fractionalFee) > 0 || fee.fixedFee.gtz();
}

function eqz(Fee memory fee) public pure returns (bool) {
return fee.fixedFee == 0 && UFixed.unwrap(fee.fractionalFee) == 0;
return fee.fixedFee.eqz() && UFixed.unwrap(fee.fractionalFee) == 0;
}
}
8 changes: 4 additions & 4 deletions test/TestDistribution.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ contract TestDistribution is GifTest {

Fee memory distributionFee = feeInfo.distributionFee;
assertEq(distributionFee.fractionalFee.toInt(), 0, "distribution fee not 0 (fractional)");
assertEq(distributionFee.fixedFee, 0, "distribution fee not 0 (fixed)");
assertEq(distributionFee.fixedFee.toInt(), 0, "distribution fee not 0 (fixed)");

Fee memory minDistributionOwnerFee = feeInfo.minDistributionOwnerFee;
assertEq(minDistributionOwnerFee.fractionalFee.toInt(), 0, "min distribution owner fee not 0 (fractional)");
assertEq(minDistributionOwnerFee.fixedFee, 0, "min distribution owner fee fee not 0 (fixed)");
assertEq(minDistributionOwnerFee.fixedFee.toInt(), 0, "min distribution owner fee fee not 0 (fixed)");

Fee memory newMinDistributionOwnerFee = FeeLib.toFee(UFixedLib.toUFixed(12,0), 34);
Fee memory newDistributionFee = FeeLib.toFee(UFixedLib.toUFixed(123,0), 456);
Expand All @@ -51,10 +51,10 @@ contract TestDistribution is GifTest {
feeInfo = instanceReader.getFeeInfo(productNftId);
distributionFee = feeInfo.distributionFee;
assertEq(distributionFee.fractionalFee.toInt(), 123, "unexpected distribution fee (fractional))");
assertEq(distributionFee.fixedFee, 456, "unexpected distribution fee not (fixed)");
assertEq(distributionFee.fixedFee.toInt(), 456, "unexpected distribution fee not (fixed)");

minDistributionOwnerFee = feeInfo.minDistributionOwnerFee;
assertEq(minDistributionOwnerFee.fractionalFee.toInt(), 12, "unexpected min distribution owner fee (fractional)");
assertEq(minDistributionOwnerFee.fixedFee, 34, "unexpected min distribution owner fee not 0 (fixed)");
assertEq(minDistributionOwnerFee.fixedFee.toInt(), 34, "unexpected min distribution owner fee not 0 (fixed)");
}
}
16 changes: 8 additions & 8 deletions test/TestPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ contract TestPool is GifTest {

Fee memory poolFee = feeInfo.poolFee;
assertEq(poolFee.fractionalFee.toInt(), 0, "pool fee not 0 (fractional)");
assertEq(poolFee.fixedFee, 0, "pool fee not 0 (fixed)");
assertEq(poolFee.fixedFee.toInt(), 0, "pool fee not 0 (fixed)");

Fee memory stakingFee = feeInfo.stakingFee;
assertEq(stakingFee.fractionalFee.toInt(), 0, "staking fee not 0 (fractional)");
assertEq(stakingFee.fixedFee, 0, "staking fee not 0 (fixed)");
assertEq(stakingFee.fixedFee.toInt(), 0, "staking fee not 0 (fixed)");

Fee memory performanceFee = feeInfo.performanceFee;
assertEq(performanceFee.fractionalFee.toInt(), 0, "performance fee not 0 (fractional)");
assertEq(performanceFee.fixedFee, 0, "performance fee fee not 0 (fixed)");
assertEq(performanceFee.fixedFee.toInt(), 0, "performance fee fee not 0 (fixed)");

Fee memory newPoolFee = FeeLib.toFee(UFixedLib.toUFixed(111,0), 222);
Fee memory newStakingFee = FeeLib.toFee(UFixedLib.toUFixed(333,0), 444);
Expand All @@ -140,11 +140,11 @@ contract TestPool is GifTest {
performanceFee = feeInfo.performanceFee;

assertEq(poolFee.fractionalFee.toInt(), 111, "pool fee not 111 (fractional)");
assertEq(poolFee.fixedFee, 222, "pool fee not 222 (fixed)");
assertEq(poolFee.fixedFee.toInt(), 222, "pool fee not 222 (fixed)");
assertEq(stakingFee.fractionalFee.toInt(), 333, "staking fee not 333 (fractional)");
assertEq(stakingFee.fixedFee, 444, "staking fee not 444 (fixed)");
assertEq(stakingFee.fixedFee.toInt(), 444, "staking fee not 444 (fixed)");
assertEq(performanceFee.fractionalFee.toInt(), 555, "performance fee not 555 (fractional)");
assertEq(performanceFee.fixedFee, 666, "performance fee not 666 (fixed)");
assertEq(performanceFee.fixedFee.toInt(), 666, "performance fee not 666 (fixed)");
}

function test_poolCreateBundle() public {
Expand Down Expand Up @@ -299,7 +299,7 @@ contract TestPool is GifTest {
IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
assertEq(bundleInfo.poolNftId.toInt(), poolNftId.toInt(), "unexpected pool nft id");
assertEq(bundleInfo.fee.fractionalFee.toInt(), initialStakingFee.fractionalFee.toInt(), "unexpected fractional bundle fee");
assertEq(bundleInfo.fee.fixedFee, initialStakingFee.fixedFee, "unexpected fixed bundle fee");
assertEq(bundleInfo.fee.fixedFee.toInt(), initialStakingFee.fixedFee.toInt(), "unexpected fixed bundle fee");

assertEq(netStakedAmount, 9000, "net staked amount not 9000");

Expand Down Expand Up @@ -433,7 +433,7 @@ contract TestPool is GifTest {
IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);
Fee memory bundleFee = bundleInfo.fee;
assertEq(bundleFee.fractionalFee.toInt(), 111, "bundle fee not 111");
assertEq(bundleFee.fixedFee, 222, "bundle fee not 222");
assertEq(bundleFee.fixedFee.toInt(), 222, "bundle fee not 222");
}

function _createBundle() internal returns (NftId bundleNftId) {
Expand Down
12 changes: 6 additions & 6 deletions test/TestProduct.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ contract TestProduct is GifTest {
IComponents.FeeInfo memory feeInfo = instanceReader.getFeeInfo(productNftId);
Fee memory productFee = feeInfo.productFee;
assertEq(productFee.fractionalFee.toInt(), 0, "product fee not 0");
assertEq(productFee.fixedFee, 0, "product fee not 0");
assertEq(productFee.fixedFee.toInt(), 0, "product fee not 0");
Fee memory processingFee = feeInfo.processingFee;
assertEq(processingFee.fractionalFee.toInt(), 0, "processing fee not 0");
assertEq(processingFee.fixedFee, 0, "processing fee not 0");
assertEq(processingFee.fixedFee.toInt(), 0, "processing fee not 0");
}


Expand All @@ -72,10 +72,10 @@ contract TestProduct is GifTest {
IComponents.FeeInfo memory feeInfo = instanceReader.getFeeInfo(productNftId);
Fee memory productFee = feeInfo.productFee;
assertEq(productFee.fractionalFee.toInt(), 0, "product fee not 0");
assertEq(productFee.fixedFee, 0, "product fee not 0");
assertEq(productFee.fixedFee.toInt(), 0, "product fee not 0");
Fee memory processingFee = feeInfo.processingFee;
assertEq(processingFee.fractionalFee.toInt(), 0, "processing fee not 0");
assertEq(processingFee.fixedFee, 0, "processing fee not 0");
assertEq(processingFee.fixedFee.toInt(), 0, "processing fee not 0");

Fee memory newProductFee = FeeLib.toFee(UFixedLib.toUFixed(123,0), 456);
Fee memory newProcessingFee = FeeLib.toFee(UFixedLib.toUFixed(789,0), 101112);
Expand All @@ -87,11 +87,11 @@ contract TestProduct is GifTest {
feeInfo = instanceReader.getFeeInfo(productNftId);
productFee = feeInfo.productFee;
assertEq(productFee.fractionalFee.toInt(), 123, "product fee not 123");
assertEq(productFee.fixedFee, 456, "product fee not 456");
assertEq(productFee.fixedFee.toInt(), 456, "product fee not 456");

processingFee = feeInfo.processingFee;
assertEq(processingFee.fractionalFee.toInt(), 789, "processing fee not 789");
assertEq(processingFee.fixedFee, 101112, "processing fee not 101112");
assertEq(processingFee.fixedFee.toInt(), 101112, "processing fee not 101112");
}

function test_productCalculatePremium() public {
Expand Down

0 comments on commit ab535f1

Please sign in to comment.