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

calculate processing fee (#424) #669

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions contracts/accounting/AccountingService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ contract AccountingService is
_changeTargetBalance(INCREASE, instanceStore, productNftId, AmountLib.zero(), feeAmount);
}

function increaseProductFeesForPool(
InstanceStore instanceStore,
NftId productNftId,
Amount feeAmount
)
external
virtual
restricted()
{
_checkNftType(productNftId, PRODUCT());
_changeTargetBalance(INCREASE, instanceStore, productNftId, AmountLib.zero(), feeAmount);
}


function decreaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount)
external
Expand Down
2 changes: 2 additions & 0 deletions contracts/accounting/IAccountingService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface IAccountingService is
function increaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) external;
function decreaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) external;

function increaseProductFeesForPool(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) external;

function increaseDistributionBalance(InstanceStore instanceStore, NftId distributionNftId, Amount amount, Amount feeAmount) external;
function decreaseDistributionBalance(InstanceStore instanceStore, NftId distributionNftId, Amount amount, Amount feeAmount) external;

Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/unpermissioned/SimpleProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ contract SimpleProduct is
function processPayout(
NftId policyNftId,
PayoutId payoutId
) public {
_processPayout(policyNftId, payoutId);
) public returns (Amount netPayoutAmount, Amount processingFeeAmount) {
(netPayoutAmount, processingFeeAmount) = _processPayout(policyNftId, payoutId);
}

function createOracleRequest(
Expand Down
3 changes: 1 addition & 2 deletions contracts/pool/IPoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {InstanceReader} from "../instance/InstanceReader.sol";
import {InstanceStore} from "../instance/InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {PayoutId} from "../type/PayoutId.sol";
import {UFixed} from "../type/UFixed.sol";

interface IPoolService is IService {

Expand Down Expand Up @@ -81,7 +80,7 @@ interface IPoolService is IService {
PayoutId payoutId,
Amount payoutAmount,
address payoutBeneficiary
) external;
) external returns (Amount netPayoutAmount, Amount processingFeeAmount);


/// @dev increase stakes for bundle
Expand Down
14 changes: 10 additions & 4 deletions contracts/pool/PoolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ library PoolLib {
// calculate processing fees if applicable
IComponents.FeeInfo memory feeInfo = instanceReader.getFeeInfo(productNftId);
if(FeeLib.gtz(feeInfo.processingFee)) {
// TODO calculate and set net payout and processing fees
(processingFeeAmount, netPayoutAmount) = FeeLib.calculateFee(feeInfo.processingFee, payoutAmount);
}
}
}
Expand Down Expand Up @@ -271,11 +271,17 @@ library PoolLib {
address payoutBeneficiary
)
external
returns (
Amount netPayoutAmount,
Amount processingFeeAmount
)
{
address beneficiary;

(
Amount netPayoutAmount,
Amount processingFeeAmount,
address beneficiary
netPayoutAmount,
processingFeeAmount,
beneficiary
) = calculatePayoutAmounts(
registry,
instanceReader,
Expand Down
13 changes: 12 additions & 1 deletion contracts/pool/PoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ contract PoolService is
external
virtual
restricted()
returns (
Amount netPayoutAmount,
Amount processingFeeAmount
)
{
// checks
_checkNftType(policyNftId, POLICY());
Expand Down Expand Up @@ -472,7 +476,7 @@ contract PoolService is
payoutAmount);

// interactions
PoolLib.transferTokenAndNotifyPolicyHolder(
(netPayoutAmount, processingFeeAmount) = PoolLib.transferTokenAndNotifyPolicyHolder(
getRegistry(),
instanceReader,
poolTokenHandler,
Expand All @@ -481,6 +485,13 @@ contract PoolService is
payoutId,
payoutAmount,
payoutBeneficiary);

if (processingFeeAmount.gtz()) {
_accountingService.increaseProductFeesForPool(
instanceStore,
productNftId,
processingFeeAmount);
}
}


Expand Down
52 changes: 30 additions & 22 deletions contracts/product/ClaimService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ contract ClaimService is
virtual
restricted()
// nonReentrant() // prevents creating a reinsurance payout in a single tx
returns (Amount netPayoutAmount, Amount processingFeeAmount)
{
// checks
(
Expand All @@ -324,30 +325,37 @@ contract ClaimService is
IPolicy.PolicyInfo memory policyInfo
) = _verifyCallerWithPolicy(policyNftId);

// check that payout exists and is open
IPolicy.PayoutInfo memory payoutInfo = instanceReader.getPayoutInfo(policyNftId, payoutId);
StateId payoutState = instanceReader.getPayoutState(policyNftId, payoutId);
if(payoutState != EXPECTED()) {
revert ErrorClaimServicePayoutNotExpected(policyNftId, payoutId, payoutState);
}
IPolicy.ClaimInfo memory claimInfo;
address payoutBeneficiary;
Amount payoutAmount;

{
// check that payout exists and is open
IPolicy.PayoutInfo memory payoutInfo = instanceReader.getPayoutInfo(policyNftId, payoutId);
payoutBeneficiary = payoutInfo.beneficiary;
payoutAmount = payoutInfo.amount;
StateId payoutState = instanceReader.getPayoutState(policyNftId, payoutId);
if(payoutState != EXPECTED()) {
revert ErrorClaimServicePayoutNotExpected(policyNftId, payoutId, payoutState);
}

// check that payout amount does not violate claim amount
IPolicy.ClaimInfo memory claimInfo = instanceReader.getClaimInfo(policyNftId, payoutId.toClaimId());
if(claimInfo.paidAmount + payoutInfo.amount > claimInfo.claimAmount) {
revert ErrorClaimServicePayoutExceedsClaimAmount(
policyNftId,
payoutId.toClaimId(),
claimInfo.claimAmount,
claimInfo.paidAmount + payoutInfo.amount);
}
// check that payout amount does not violate claim amount
claimInfo = instanceReader.getClaimInfo(policyNftId, payoutId.toClaimId());
if(claimInfo.paidAmount + payoutInfo.amount > claimInfo.claimAmount) {
revert ErrorClaimServicePayoutExceedsClaimAmount(
policyNftId,
payoutId.toClaimId(),
claimInfo.claimAmount,
claimInfo.paidAmount + payoutInfo.amount);
}

// effects
// update and save payout info with instance
payoutInfo.paidAt = TimestampLib.blockTimestamp();
instanceStore.updatePayout(policyNftId, payoutId, payoutInfo, PAID());
// effects
// update and save payout info with instance
payoutInfo.paidAt = TimestampLib.blockTimestamp();
instanceStore.updatePayout(policyNftId, payoutId, payoutInfo, PAID());
}

// update and save claim info with instance
Amount payoutAmount = payoutInfo.amount;
{
ClaimId claimId = payoutId.toClaimId();
// TODO cleanup
Expand Down Expand Up @@ -375,15 +383,15 @@ contract ClaimService is

// effects + interactions (push tokens to beneficiary, product)
// delegate to pool to update book keeping and moving tokens payout
_poolService.processPayout(
(netPayoutAmount, processingFeeAmount) = _poolService.processPayout(
instanceReader,
instanceStore,
policyInfo.productNftId, // product nft id
policyNftId,
policyInfo.bundleNftId,
payoutId,
payoutAmount,
payoutInfo.beneficiary);
payoutBeneficiary);
}

function cancelPayout(
Expand Down
2 changes: 1 addition & 1 deletion contracts/product/IClaimService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ interface IClaimService is
function processPayout(
NftId policyNftId,
PayoutId payoutId
) external;
) external returns (Amount netPayoutAmount, Amount processingFeeAmount);

/// @dev cancels the specified payout. no tokens are moved, payout is set to cancelled.
function cancelPayout(
Expand Down
5 changes: 3 additions & 2 deletions contracts/product/Product.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {COMPONENT, PRODUCT, BUNDLE, APPLICATION, POLICY, CLAIM, PRICE } from "..
import {PayoutId} from "../type/PayoutId.sol";
import {COMPONENT, PRODUCT, APPLICATION, POLICY, CLAIM, PRICE, BUNDLE, RISK } from "../type/ObjectType.sol";
import {ReferralId} from "../type/Referral.sol";
import {RiskId, RiskIdLib} from "../type/RiskId.sol";
import {RiskId} from "../type/RiskId.sol";
import {Seconds} from "../type/Seconds.sol";
import {StateId} from "../type/StateId.sol";
import {Timestamp} from "../type/Timestamp.sol";
Expand Down Expand Up @@ -443,8 +443,9 @@ abstract contract Product is
)
internal
virtual
returns (Amount netPayoutAmount, Amount processingFeeAmount)
{
_getProductStorage()._claimService.processPayout(
(netPayoutAmount, processingFeeAmount) = _getProductStorage()._claimService.processPayout(
policyNftId,
payoutId);
}
Expand Down
1 change: 1 addition & 0 deletions contracts/registry/ServiceAuthorizationV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ contract ServiceAuthorizationV3
_authorize(functions, IAccountingService.decreasePoolBalance.selector, "decreasePoolBalance");
_authorize(functions, IAccountingService.increaseBundleBalanceForPool.selector, "increaseBundleBalanceForPool");
_authorize(functions, IAccountingService.decreaseBundleBalanceForPool.selector, "decreaseBundleBalanceForPool");
_authorize(functions, IAccountingService.increaseProductFeesForPool.selector, "increaseProductFeesForPool");

}

Expand Down
Loading
Loading