|
|
@ -6,8 +6,10 @@ import "./NFT.sol"; |
|
|
|
import "./Pool.sol"; |
|
|
|
import "./PledgeType.sol"; |
|
|
|
import "./Utils.sol"; |
|
|
|
import "./SafeMath.sol"; |
|
|
|
|
|
|
|
contract Pledge is Initializable { |
|
|
|
using SafeMath for uint256; |
|
|
|
|
|
|
|
NFT nftContract; |
|
|
|
Pool poolContract; |
|
|
@ -19,7 +21,8 @@ contract Pledge is Initializable { |
|
|
|
mapping(address => uint256[]) public invitationTokens; |
|
|
|
mapping(address => address[]) public invitationAddress; |
|
|
|
mapping(address => PledgeWithdrawRecordType[]) public pledgeWithdrawRecord; |
|
|
|
mapping(address => InvitationWithdrawRecordType[]) public invitationWithdrawRecord; |
|
|
|
mapping(address => InvitationWithdrawRecordType[]) |
|
|
|
public invitationWithdrawRecord; |
|
|
|
address[] public blackList; |
|
|
|
address[] public whiteList; |
|
|
|
mapping(address => PledgeType[]) public pledgeDestoryRecords; |
|
|
@ -28,16 +31,19 @@ contract Pledge is Initializable { |
|
|
|
bool public pledgeStatus; |
|
|
|
uint256 public dayTime; |
|
|
|
|
|
|
|
function initialize(address nftAddress, address poolAddress) public initializer { |
|
|
|
function initialize( |
|
|
|
address nftAddress, |
|
|
|
address poolAddress |
|
|
|
) public initializer { |
|
|
|
nftContract = NFT(nftAddress); |
|
|
|
poolContract = Pool(poolAddress); |
|
|
|
nextTokenId = 1; |
|
|
|
invitationRate = 1; |
|
|
|
invitationRate = 100; |
|
|
|
pledgeStatus = false; |
|
|
|
dayTime = 1 days; |
|
|
|
productInfo.push(ProductInfo(180, 14)); |
|
|
|
productInfo.push(ProductInfo(270, 15)); |
|
|
|
productInfo.push(ProductInfo(360, 16)); |
|
|
|
productInfo.push(ProductInfo(180, 1408)); |
|
|
|
productInfo.push(ProductInfo(270, 1521)); |
|
|
|
productInfo.push(ProductInfo(360, 1623)); |
|
|
|
} |
|
|
|
|
|
|
|
modifier onlyAdmin() { |
|
|
@ -53,19 +59,27 @@ contract Pledge is Initializable { |
|
|
|
_; |
|
|
|
} |
|
|
|
|
|
|
|
function getPledgeRecords(address _owner) public view returns(PledgeType[] memory){ |
|
|
|
function getPledgeRecords( |
|
|
|
address _owner |
|
|
|
) public view returns (PledgeType[] memory) { |
|
|
|
return pledgeRecords[_owner]; |
|
|
|
} |
|
|
|
|
|
|
|
function getPledgeWithdrawRecord(address _owner) public view returns(PledgeWithdrawRecordType[] memory){ |
|
|
|
function getPledgeWithdrawRecord( |
|
|
|
address _owner |
|
|
|
) public view returns (PledgeWithdrawRecordType[] memory) { |
|
|
|
return pledgeWithdrawRecord[_owner]; |
|
|
|
} |
|
|
|
|
|
|
|
function getInvitationWithdrawRecord(address _owner) public view returns(InvitationWithdrawRecordType[] memory){ |
|
|
|
function getInvitationWithdrawRecord( |
|
|
|
address _owner |
|
|
|
) public view returns (InvitationWithdrawRecordType[] memory) { |
|
|
|
return invitationWithdrawRecord[_owner]; |
|
|
|
} |
|
|
|
|
|
|
|
function getPledgeDestoryRecords(address _owner) public view returns(PledgeType[] memory){ |
|
|
|
function getPledgeDestoryRecords( |
|
|
|
address _owner |
|
|
|
) public view returns (PledgeType[] memory) { |
|
|
|
return pledgeDestoryRecords[_owner]; |
|
|
|
} |
|
|
|
|
|
|
@ -73,7 +87,9 @@ contract Pledge is Initializable { |
|
|
|
return productInfo; |
|
|
|
} |
|
|
|
|
|
|
|
function getOwnerAllPledgeInfo( address _ownerAddress ) public view returns (PledgeType[] memory result) { |
|
|
|
function getOwnerAllPledgeInfo( |
|
|
|
address _ownerAddress |
|
|
|
) public view returns (PledgeType[] memory result) { |
|
|
|
uint256[] memory tokens = getOwnerAllTokens(_ownerAddress); |
|
|
|
result = new PledgeType[](tokens.length); |
|
|
|
for (uint256 i = 0; i < tokens.length; i++) { |
|
|
@ -83,9 +99,15 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
|
|
|
|
function bindRecommend(address _referrer) internal { |
|
|
|
require(recommendObj[msg.sender].key == address(0),"Already have recommenders"); |
|
|
|
require( |
|
|
|
recommendObj[msg.sender].key == address(0), |
|
|
|
"Already have recommenders" |
|
|
|
); |
|
|
|
require(msg.sender != _referrer, "You can't recommend yourself."); |
|
|
|
require(recommendObj[_referrer].referrer != msg.sender,"Can't recommend each other"); |
|
|
|
require( |
|
|
|
recommendObj[_referrer].referrer != msg.sender, |
|
|
|
"Can't recommend each other" |
|
|
|
); |
|
|
|
recommendObj[msg.sender] = RecommendObjType( |
|
|
|
msg.sender, |
|
|
|
_referrer, |
|
|
@ -103,8 +125,10 @@ contract Pledge is Initializable { |
|
|
|
uint256 rate = _invitationPledge.rate; |
|
|
|
uint256 day = _invitationPledge.pledgeDay; |
|
|
|
uint256 tokenId = _invitationPledge.tokenId; |
|
|
|
uint256 contribute = (((amount * rate) / 365) * day) / 100; |
|
|
|
recommendObj[msg.sender].contribute += contribute; |
|
|
|
// uint256 contribute = (((amount * rate) / 365) * day) / 100; |
|
|
|
uint256 contribute = amount.mul(rate).div(365).mul(day).div(10000); |
|
|
|
|
|
|
|
recommendObj[msg.sender].contribute = contribute.add(recommendObj[msg.sender].contribute); |
|
|
|
|
|
|
|
if (invitationTokens[recommendAddr].length == 0) { |
|
|
|
invitationTokens[recommendAddr] = new uint256[](0); |
|
|
@ -116,7 +140,11 @@ contract Pledge is Initializable { |
|
|
|
invitationPledges[tokenId] = _invitationPledge; |
|
|
|
} |
|
|
|
|
|
|
|
function pledge(uint256 amount, uint256 index, address _referrer) external onlyBlacks { |
|
|
|
function pledge( |
|
|
|
uint256 amount, |
|
|
|
uint256 index, |
|
|
|
address _referrer |
|
|
|
) external onlyBlacks { |
|
|
|
bool _isWhite = Utils.isAdmin(whiteList, msg.sender); |
|
|
|
if (!_isWhite) { |
|
|
|
require(pledgeStatus == false, "The pledge is closed."); |
|
|
@ -133,16 +161,27 @@ contract Pledge is Initializable { |
|
|
|
uint256 startTime = block.timestamp; |
|
|
|
uint256 endTime = block.timestamp + (item.day * dayTime); |
|
|
|
nftContract.mint(msg.sender, tokenId); |
|
|
|
PledgeType memory _pledge = PledgeType(tokenId,startTime,endTime,startTime,amount,item.rate,item.day,msg.sender,false); |
|
|
|
PledgeType memory _pledge = PledgeType( |
|
|
|
tokenId, |
|
|
|
startTime, |
|
|
|
endTime, |
|
|
|
startTime, |
|
|
|
amount, |
|
|
|
item.rate, |
|
|
|
item.day, |
|
|
|
msg.sender, |
|
|
|
false |
|
|
|
); |
|
|
|
pledges[tokenId] = _pledge; |
|
|
|
pledgeRecords[msg.sender].push(_pledge); |
|
|
|
if (recommendObj[msg.sender].key != address(0)) { |
|
|
|
setInvitationIncomes(_pledge); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
function getOwnerAllTokens( address _ownerAddress ) public view returns (uint256[] memory) { |
|
|
|
function getOwnerAllTokens( |
|
|
|
address _ownerAddress |
|
|
|
) public view returns (uint256[] memory) { |
|
|
|
uint256 nftAmount = nftContract.balanceOf(_ownerAddress); |
|
|
|
uint256[] memory tokens = new uint256[](nftAmount); |
|
|
|
for (uint256 i = 0; i < nftAmount; i++) { |
|
|
@ -151,7 +190,9 @@ contract Pledge is Initializable { |
|
|
|
return tokens; |
|
|
|
} |
|
|
|
|
|
|
|
function calculateInterest( PledgeType memory _pledge ) public view returns (uint256) { |
|
|
|
function calculateInterest( |
|
|
|
PledgeType memory _pledge |
|
|
|
) public view returns (uint256) { |
|
|
|
uint256 total_amount = _pledge.pledgeAmount; |
|
|
|
uint256 currentTime = block.timestamp; |
|
|
|
uint256 withdrawTime = _pledge.withdrawTime; |
|
|
@ -160,11 +201,13 @@ contract Pledge is Initializable { |
|
|
|
currentTime = _pledge.endTime; |
|
|
|
} |
|
|
|
uint256 daysPassed = (currentTime - withdrawTime) / dayTime; |
|
|
|
uint256 dailyShare = (((total_amount * rate) / 365) * daysPassed) / 100; |
|
|
|
uint256 dailyShare = total_amount.mul(rate).div(365).mul(daysPassed).div(10000); |
|
|
|
return dailyShare; |
|
|
|
} |
|
|
|
|
|
|
|
function getWithdrawbleAmount( address _owner ) public view returns (uint256) { |
|
|
|
function getWithdrawbleAmount( |
|
|
|
address _owner |
|
|
|
) public view returns (uint256) { |
|
|
|
uint256[] memory tokens = getOwnerAllTokens(_owner); |
|
|
|
if (tokens.length <= 0) return 0; |
|
|
|
uint256 _total_interest = 0; |
|
|
@ -178,26 +221,37 @@ contract Pledge is Initializable { |
|
|
|
|
|
|
|
function withdraAllInterest() external onlyBlacks { |
|
|
|
uint256 _total_interest = getWithdrawbleAmount(msg.sender); |
|
|
|
require(_total_interest > 0, "Withdrawal amount must be greater than 0"); |
|
|
|
require( |
|
|
|
_total_interest > 0, |
|
|
|
"Withdrawal amount must be greater than 0" |
|
|
|
); |
|
|
|
uint256[] memory tokens = getOwnerAllTokens(msg.sender); |
|
|
|
for (uint256 i = 0; i < tokens.length; i++) { |
|
|
|
uint256 _withdrawTime = block.timestamp; |
|
|
|
uint256 _tokenId = tokens[i]; |
|
|
|
require(!pledges[_tokenId].isBlack,"Unable to withdraw, the NFT Tokend is blacklisted."); |
|
|
|
require( |
|
|
|
!pledges[_tokenId].isBlack, |
|
|
|
"Unable to withdraw, the NFT Tokend is blacklisted." |
|
|
|
); |
|
|
|
if (pledges[_tokenId].endTime < _withdrawTime) { |
|
|
|
_withdrawTime = pledges[_tokenId].endTime; |
|
|
|
} |
|
|
|
pledges[_tokenId].withdrawTime = _withdrawTime; |
|
|
|
} |
|
|
|
poolContract.withdraw(msg.sender, _total_interest); |
|
|
|
pledgeWithdrawRecord[msg.sender].push(PledgeWithdrawRecordType(_total_interest,block.timestamp,0,0,2)); |
|
|
|
pledgeWithdrawRecord[msg.sender].push( |
|
|
|
PledgeWithdrawRecordType(_total_interest, block.timestamp, 0, 0, 2) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
function withdrawInterest(uint256 tokenId) external onlyBlacks { |
|
|
|
address _owner = nftContract.ownerOf(tokenId); |
|
|
|
require(_owner == msg.sender, "It's not a contract."); |
|
|
|
PledgeType memory data = pledges[tokenId]; |
|
|
|
require(!data.isBlack,"Unable to withdraw,the NFT Tokend is blacklisted."); |
|
|
|
require( |
|
|
|
!data.isBlack, |
|
|
|
"Unable to withdraw,the NFT Tokend is blacklisted." |
|
|
|
); |
|
|
|
uint256 _interest = calculateInterest(data); |
|
|
|
require(_interest > 0, "Withdrawal amount must be greater than 0"); |
|
|
|
uint256 _withdrawTime = block.timestamp; |
|
|
@ -206,11 +260,20 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
poolContract.withdraw(msg.sender, _interest); |
|
|
|
pledges[tokenId].withdrawTime = _withdrawTime; |
|
|
|
pledgeWithdrawRecord[msg.sender].push(PledgeWithdrawRecordType(_interest,block.timestamp,data.tokenId,data.pledgeDay,1)); |
|
|
|
|
|
|
|
pledgeWithdrawRecord[msg.sender].push( |
|
|
|
PledgeWithdrawRecordType( |
|
|
|
_interest, |
|
|
|
block.timestamp, |
|
|
|
data.tokenId, |
|
|
|
data.pledgeDay, |
|
|
|
1 |
|
|
|
) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
function getOwnerInvitationPledges( address addr ) public view returns (PledgeType[] memory result) { |
|
|
|
function getOwnerInvitationPledges( |
|
|
|
address addr |
|
|
|
) public view returns (PledgeType[] memory result) { |
|
|
|
uint256[] memory _tokens = invitationTokens[addr]; |
|
|
|
result = new PledgeType[](_tokens.length); |
|
|
|
for (uint256 i = 0; i < _tokens.length; i++) { |
|
|
@ -220,7 +283,9 @@ contract Pledge is Initializable { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
function getOwnerAllInvitationWithdrawAmout(address owner) public view returns(uint256){ |
|
|
|
function getOwnerAllInvitationWithdrawAmout( |
|
|
|
address owner |
|
|
|
) public view returns (uint256) { |
|
|
|
uint256 _income = 0; |
|
|
|
PledgeType[] memory _pledges = getOwnerInvitationPledges(owner); |
|
|
|
for (uint256 i = 0; i < _pledges.length; i++) { |
|
|
@ -231,8 +296,13 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
|
|
|
|
function withdraInvitationAllInterest() external onlyBlacks { |
|
|
|
uint256 _total_interest = getOwnerAllInvitationWithdrawAmout(msg.sender); |
|
|
|
require(_total_interest > 0, "Withdrawal amount must be greater than 0"); |
|
|
|
uint256 _total_interest = getOwnerAllInvitationWithdrawAmout( |
|
|
|
msg.sender |
|
|
|
); |
|
|
|
require( |
|
|
|
_total_interest > 0, |
|
|
|
"Withdrawal amount must be greater than 0" |
|
|
|
); |
|
|
|
uint256[] memory tokens = invitationTokens[msg.sender]; |
|
|
|
for (uint256 i = 0; i < tokens.length; i++) { |
|
|
|
uint256 _withdrawTime = block.timestamp; |
|
|
@ -244,7 +314,9 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
uint256 currentTime = block.timestamp; |
|
|
|
poolContract.withdraw(msg.sender, _total_interest); |
|
|
|
invitationWithdrawRecord[msg.sender].push(InvitationWithdrawRecordType(_total_interest,currentTime)); |
|
|
|
invitationWithdrawRecord[msg.sender].push( |
|
|
|
InvitationWithdrawRecordType(_total_interest, currentTime) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
function withdrawInvitationInterest(uint256 tokenId) external onlyBlacks { |
|
|
@ -268,10 +340,14 @@ contract Pledge is Initializable { |
|
|
|
poolContract.withdraw(msg.sender, _interest); |
|
|
|
uint256 currentTime = block.timestamp; |
|
|
|
invitationPledges[tokenId].withdrawTime = _withdrawTime; |
|
|
|
invitationWithdrawRecord[msg.sender].push(InvitationWithdrawRecordType(_interest,currentTime)); |
|
|
|
invitationWithdrawRecord[msg.sender].push( |
|
|
|
InvitationWithdrawRecordType(_interest, currentTime) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
function getAllInvitationMember( address addr ) public view returns (RecommendObjType[] memory result) { |
|
|
|
function getAllInvitationMember( |
|
|
|
address addr |
|
|
|
) public view returns (RecommendObjType[] memory result) { |
|
|
|
address[] memory _addresss = invitationAddress[addr]; |
|
|
|
result = new RecommendObjType[](_addresss.length); |
|
|
|
for (uint256 i = 0; i < _addresss.length; i++) { |
|
|
@ -305,7 +381,10 @@ contract Pledge is Initializable { |
|
|
|
pledgeStatus = _status; |
|
|
|
} |
|
|
|
|
|
|
|
function addBlackOrWhiteList(address[] memory _blacks,uint256 _type) external onlyAdmin { |
|
|
|
function addBlackOrWhiteList( |
|
|
|
address[] memory _blacks, |
|
|
|
uint256 _type |
|
|
|
) external onlyAdmin { |
|
|
|
require(_blacks.length > 0, "Enter at least one address"); |
|
|
|
if (_type == 1) { |
|
|
|
address[] memory blacks = Utils.addAdmin(blackList, _blacks); |
|
|
@ -317,7 +396,10 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function delBlackOrWhiteList(address[] memory _blacks,uint256 _type) external onlyAdmin{ |
|
|
|
function delBlackOrWhiteList( |
|
|
|
address[] memory _blacks, |
|
|
|
uint256 _type |
|
|
|
) external onlyAdmin { |
|
|
|
require(_blacks.length > 0, "Enter at least one address"); |
|
|
|
if (_type == 1) { |
|
|
|
address[] memory blacks = Utils.deleteAdmin(blackList, _blacks); |
|
|
@ -329,18 +411,26 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function getBlackOrWhiteList(uint256 _type) public view returns (address[] memory) { |
|
|
|
function getBlackOrWhiteList( |
|
|
|
uint256 _type |
|
|
|
) public view returns (address[] memory) { |
|
|
|
return _type == 1 ? blackList : whiteList; |
|
|
|
} |
|
|
|
|
|
|
|
function setProductInfo(uint256[] memory _days,uint256[] memory _rates) external onlyAdmin { |
|
|
|
function setProductInfo( |
|
|
|
uint256[] memory _days, |
|
|
|
uint256[] memory _rates |
|
|
|
) external onlyAdmin { |
|
|
|
delete productInfo; |
|
|
|
for (uint256 i = 0; i < 3; i++) { |
|
|
|
productInfo.push(ProductInfo(_days[i], _rates[i])); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function getDetails(uint256 tokenId,uint256 _type) public view returns(PledgeType memory){ |
|
|
|
function getDetails( |
|
|
|
uint256 tokenId, |
|
|
|
uint256 _type |
|
|
|
) public view returns (PledgeType memory) { |
|
|
|
if (_type == 1) { |
|
|
|
return pledges[tokenId]; |
|
|
|
} |
|
|
@ -368,4 +458,8 @@ contract Pledge is Initializable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function setDayTime(uint256 time) external onlyAdmin { |
|
|
|
dayTime = time; |
|
|
|
} |
|
|
|
|
|
|
|
} |