Skip to content

Commit

Permalink
move retrievePayment to Library
Browse files Browse the repository at this point in the history
  • Loading branch information
danoctavian committed Jul 25, 2022
1 parent c6fc94c commit f11a6a9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
39 changes: 1 addition & 38 deletions contracts/modules/cover/Cover.sol
Original file line number Diff line number Diff line change
Expand Up @@ -474,44 +474,7 @@ contract Cover is ICover, MasterAwareV2, IStakingPoolBeacon, ReentrancyGuard {
address commissionDestination
) internal {

// add commission
uint commission = premium * commissionRatio / COMMISSION_DENOMINATOR;

if (paymentAsset == 0) {

uint premiumWithCommission = premium + commission;
require(msg.value >= premiumWithCommission, "Cover: Insufficient ETH sent");

uint remainder = msg.value - premiumWithCommission;

if (remainder > 0) {
// solhint-disable-next-line avoid-low-level-calls
(bool ok, /* data */) = address(msg.sender).call{value: remainder}("");
require(ok, "Cover: Returning ETH remainder to sender failed.");
}

// send commission
if (commission > 0) {
(bool ok, /* data */) = address(commissionDestination).call{value: commission}("");
require(ok, "Cover: Sending ETH to commission destination failed.");
}

return;
}

IPool _pool = pool();

(
address payoutAsset,
/*uint8 decimals*/
) = _pool.coverAssets(paymentAsset);

IERC20 token = IERC20(payoutAsset);
token.safeTransferFrom(msg.sender, address(_pool), premium);

if (commission > 0) {
token.safeTransferFrom(msg.sender, commissionDestination, commission);
}
CoverUtilsLib.retrievePayment(premium, paymentAsset, commissionRatio, commissionDestination, pool());
}

function retrieveNXMPayment(uint price, uint commissionRatio, address commissionDestination) internal {
Expand Down
52 changes: 52 additions & 0 deletions contracts/modules/cover/CoverUtilsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

pragma solidity ^0.8.9;

import "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts-v4/token/ERC20/utils/SafeERC20.sol";

import "../../interfaces/ICover.sol";
import "../../interfaces/ICoverNFT.sol";
import "../../interfaces/IProductsV1.sol";
import "../../interfaces/IQuotationData.sol";
import "../../interfaces/ITokenController.sol";
import "../../libraries/SafeUintCast.sol";
import "../../interfaces/IPool.sol";
import "./MinimalBeaconProxy.sol";


library CoverUtilsLib {
using SafeERC20 for IERC20;

uint private constant GLOBAL_CAPACITY_DENOMINATOR = 10_000;
uint private constant COMMISSION_DENOMINATOR = 10_000;

struct MigrateParams {
uint coverId;
Expand Down Expand Up @@ -194,4 +200,50 @@ library CoverUtilsLib {

return coverNFT.ownerOf(coverId);
}

function retrievePayment(
uint premium,
uint8 paymentAsset,
uint16 commissionRatio,
address commissionDestination,
IPool _pool
) external {

// add commission
uint commission = premium * commissionRatio / COMMISSION_DENOMINATOR;

if (paymentAsset == 0) {

uint premiumWithCommission = premium + commission;
require(msg.value >= premiumWithCommission, "Cover: Insufficient ETH sent");

uint remainder = msg.value - premiumWithCommission;

if (remainder > 0) {
// solhint-disable-next-line avoid-low-level-calls
(bool ok, /* data */) = address(msg.sender).call{value: remainder}("");
require(ok, "Cover: Returning ETH remainder to sender failed.");
}

// send commission
if (commission > 0) {
(bool ok, /* data */) = address(commissionDestination).call{value: commission}("");
require(ok, "Cover: Sending ETH to commission destination failed.");
}

return;
}

(
address payoutAsset,
/*uint8 decimals*/
) = _pool.coverAssets(paymentAsset);

IERC20 token = IERC20(payoutAsset);
token.safeTransferFrom(msg.sender, address(_pool), premium);

if (commission > 0) {
token.safeTransferFrom(msg.sender, commissionDestination, commission);
}
}
}

0 comments on commit f11a6a9

Please sign in to comment.