-
Notifications
You must be signed in to change notification settings - Fork 2
/
TimedDealProvider.sol
79 lines (67 loc) · 3.34 KB
/
TimedDealProvider.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../LockProvider/LockDealState.sol";
import "../DealProvider/DealProviderState.sol";
import "../Provider/BasicProvider.sol";
import "@poolzfinance/poolz-helper-v2/contracts/CalcUtils.sol";
contract TimedDealProvider is LockDealState, DealProviderState, BasicProvider {
using CalcUtils for uint256;
/**
* @dev Contract constructor.
* @param _lockDealNFT The address of the LockDealNFT contract.
* @param _provider The address of the LockProvider contract.
*/
constructor(ILockDealNFT _lockDealNFT, address _provider) {
require(address(_lockDealNFT) != address(0x0) && _provider != address(0x0), "invalid address");
provider = ISimpleProvider(_provider);
lockDealNFT = _lockDealNFT;
name = "TimedDealProvider";
}
function _withdraw(
uint256 poolId,
uint256 amount
) internal override firewallProtectedSig(0x9e2bf22c) returns (uint256 withdrawnAmount, bool isFinal) {
(withdrawnAmount, isFinal) = provider.withdraw(poolId, amount);
}
function getWithdrawableAmount(uint256 poolId) public view override returns (uint256) {
uint256[] memory params = getParams(poolId);
uint256 leftAmount = params[0];
uint256 startTime = params[1];
uint256 finishTime = params[2];
uint256 startAmount = params[3];
if (block.timestamp < startTime) return 0;
if (finishTime <= block.timestamp) return leftAmount;
uint256 totalPoolDuration = finishTime - startTime;
uint256 timePassed = block.timestamp - startTime;
uint256 debitableAmount = (startAmount * timePassed) / totalPoolDuration;
return debitableAmount - (startAmount - leftAmount);
}
function split(uint256 oldPoolId, uint256 newPoolId, uint256 ratio) external firewallProtected onlyProvider {
provider.split(oldPoolId, newPoolId, ratio);
uint256 newPoolStartAmount = poolIdToAmount[oldPoolId].calcAmount(ratio);
poolIdToAmount[oldPoolId] -= newPoolStartAmount;
poolIdToAmount[newPoolId] = newPoolStartAmount;
poolIdToTime[newPoolId] = poolIdToTime[oldPoolId];
}
///@param params[0] = leftAmount = startAmount (leftAmount & startAmount must be same while creating pool)
///@param params[1] = startTime
///@param params[2] = finishTime
function _registerPool(uint256 poolId, uint256[] calldata params) internal override firewallProtectedSig(0xb99c642c) {
require(params[2] >= params[1], "Finish time should be greater than start time");
poolIdToTime[poolId] = params[2];
poolIdToAmount[poolId] = params[0];
provider.registerPool(poolId, params);
}
function getParams(uint256 poolId) public view override returns (uint256[] memory params) {
uint256[] memory lockDealProviderParams;
lockDealProviderParams = provider.getParams(poolId);
params = new uint256[](4);
params[0] = lockDealProviderParams[0]; // leftAmount
params[1] = lockDealProviderParams[1]; // startTime
params[2] = poolIdToTime[poolId]; // finishTime
params[3] = poolIdToAmount[poolId]; // startAmount
}
function currentParamsTargetLength() public view override(IProvider, ProviderState) returns (uint256) {
return 1 + provider.currentParamsTargetLength();
}
}