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

Improve fee withdraw logic #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 19 additions & 6 deletions contracts/FungibleOriginationPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -751,18 +751,31 @@ contract FungibleOriginationPool is
require(success);
// send fees to core
originationCore.receiveFees{value: originationCoreFees}();
// reset accrued origination fees amount
originationCoreFees = 0;
} else {
transferAmount =
purchaseToken.balanceOf(address(this)) -
originationCoreFees;
purchaseToken.safeTransfer(owner(), transferAmount);
purchaseToken.safeTransfer(
address(originationCore),
originationCoreFees
);
// handle timelocked tokens
try
purchaseToken.transfer(
address(originationCore),
originationCoreFees
)
{
// reset accrued origination fees amount
originationCoreFees = 0;
} catch {
// if transfer to core fails don't reset the fee amount
// approve tokens to origination core to be transferred
purchaseToken.safeIncreaseAllowance(
address(originationCore),
originationCoreFees
);
}
}
// reset accrued origination fees amount
originationCoreFees = 0;

emit PurchaseTokenClaim(owner(), transferAmount);
}
Expand Down
37 changes: 32 additions & 5 deletions contracts/OriginationCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.so
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import "./interface/IPoolDeployer.sol";
import "./interface/INFTDeployer.sol";
Expand All @@ -21,6 +22,7 @@ contract OriginationCore is
Initializable,
OwnableUpgradeable
{
using SafeERC20 for IERC20;
//--------------------------------------------------------------------------
// State variables
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -61,6 +63,8 @@ contract OriginationCore is
event SetListingFee(uint256 fee);
event CustomListingFeeEnabled(address indexed deployer, uint256 customFee);
event CustomListingFeeDisabled(address indexed deployer);
event TokenFeeWithdraw(address indexed token, uint256 amount);
event EthFeeWithdraw(uint256 amount);

//--------------------------------------------------------------------------
// Constructor / Initializer
Expand Down Expand Up @@ -109,10 +113,11 @@ contract OriginationCore is
* @dev Must pay the listing fee
*
* @param saleParams The token sale params
* @return originationPool address of the deployed pool
*/
function createFungibleListing(
IFungibleOriginationPool.SaleParams calldata saleParams
) external payable {
) external payable returns (address originationPool) {
uint256 feeOwed = customListingFeeEnabled[msg.sender]
? customListingFee[msg.sender]
: listingFee;
Expand All @@ -127,7 +132,7 @@ contract OriginationCore is
);

// Deploy the pool
address originationPool = poolDeployer.deployFungibleOriginationPool(
originationPool = poolDeployer.deployFungibleOriginationPool(
address(proxyAdmin)
);
// Deploy the vesting entry nft if there is a vesting period
Expand Down Expand Up @@ -206,15 +211,37 @@ contract OriginationCore is
""
);
require(success);
emit EthFeeWithdraw(address(this).balance);
} else {
bool success = IERC20(_feeToken).transfer(
uint256 fees = IERC20(_feeToken).balanceOf(address(this));
IERC20(_feeToken).safeTransfer(
msg.sender,
IERC20(_feeToken).balanceOf(address(this))
fees
);
require(success);
emit TokenFeeWithdraw(_feeToken, fees);
}
}

/**
* @dev Withdraw unclaimed fees from origination pool
* @dev Callable only if there are any unclaimed fees
* @dev Callable only after owner claims sale purchase tokens
*
* @param _pool the pool address
* @param _feeToken The token address of the fee to claim
*/
function withdrawFees(address _pool, address _feeToken) external {
require(
xTokenManager.isRevenueController(msg.sender),
"Only callable by revenue controller."
);

uint256 feeAmount = IFungibleOriginationPool(_pool).originationCoreFees();

IERC20(_feeToken).transferFrom(_pool, msg.sender, feeAmount);
emit TokenFeeWithdraw(_feeToken, feeAmount);
}

/**
* @dev Function used by origination pools to send the origination fees to this contract
* @dev Only used after a token sale has finished successfully on claiming the tokens
Expand Down
2 changes: 2 additions & 0 deletions contracts/interface/IFungibleOriginationPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ interface IFungibleOriginationPool {
address vestingEntryNFT,
SaleParams calldata saleParams
) external;

function originationCoreFees() external view returns (uint256);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"chai": "^4.3.4",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.2",
"hardhat": "^2.9.2",
"hardhat": "^2.12.6",
"hardhat-deploy": "^0.9.23",
"hardhat-deploy-ethers": "^0.3.0-beta.12",
"prettier": "^2.5.1",
Expand Down