Skip to content

Commit

Permalink
feat/nfts (#172)
Browse files Browse the repository at this point in the history
* added active checks

* restored pre-commit

* added field

* fix

* added field

* refactored storage

* refactored

* fix

* init new methods

* refactored uk lib

* snapshot and canCreate

* started opt GPVote

* refactored & optimized

* fixes

* opt

* fixed

* stack too deep

* rm nftAddress

* fixes

* added min

* upd nft power

* typo

* removed nft snapshots

* added createProposalAndVote func

* fixed TSP bought event

* merged logic fixes

* refactored nft info

* fixed tests

* docs

* cov 100

* fix

* fix

* fixes

* fix

* fix

* fix

* fix

* fix & tests

* fix

* rm core param

* fix

* fix

* refactored nfts powers

* tests

* warning fixes

* tests

---------

Co-authored-by: Artem Chystiakov <artem.ch31@gmail.com>
  • Loading branch information
dovgopoly and Arvolear authored Oct 30, 2023
1 parent 693bc10 commit b2b30da
Show file tree
Hide file tree
Showing 19 changed files with 790 additions and 495 deletions.
29 changes: 23 additions & 6 deletions contracts/gov/ERC721/powers/AbstractERC721Power.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ abstract contract AbstractERC721Power is

function getNftMaxPower(uint256 tokenId) public view virtual returns (uint256);

function getNftMinPower(uint256 tokenId) public view virtual returns (uint256);

function getNftPower(uint256 tokenId) public view virtual returns (uint256);

function getNftInfo(uint256 tokenId) external view virtual returns (NftInfoView memory info) {
info.rawInfo = _nftInfos[tokenId];

info.maxPower = getNftMaxPower(tokenId);
info.minPower = getNftMinPower(tokenId);
info.currentPower = getNftPower(tokenId);
}

Expand Down Expand Up @@ -200,7 +203,7 @@ abstract contract AbstractERC721Power is
}

function _recalculateRawNftPower(uint256 tokenId) internal {
if (!_exists(tokenId) || block.timestamp < powerCalcStartTimestamp) {
if (!_isActiveNft(tokenId)) {
return;
}

Expand All @@ -218,16 +221,13 @@ abstract contract AbstractERC721Power is
}

function _getRawNftPower(uint256 tokenId) internal view returns (uint256) {
if (!_exists(tokenId) || block.timestamp < powerCalcStartTimestamp) {
if (!_isActiveNft(tokenId)) {
return 0;
}

uint256 collateral = _nftInfos[tokenId].currentCollateral;

// Calculate the minimum possible power based on the collateral of the nft
uint256 maxNftPower = _getRawNftMaxPower(tokenId);
uint256 minNftPower = maxNftPower.ratio(collateral, _getNftRequiredCollateral(tokenId));
minNftPower = maxNftPower.min(minNftPower);
uint256 minNftPower = _getRawNftMinPower(tokenId);

// Get last update and current power. Or set them to default if it is first iteration
uint64 lastUpdate = _nftInfos[tokenId].lastUpdate;
Expand Down Expand Up @@ -260,12 +260,29 @@ abstract contract AbstractERC721Power is
return localRawPower == 0 ? nftMaxRawPower : localRawPower;
}

function _getRawNftMinPower(uint256 tokenId) internal view returns (uint256) {
if (!_isActiveNft(tokenId)) {
return 0;
}

uint256 maxNftPower = _getRawNftMaxPower(tokenId);

return
maxNftPower
.ratio(_nftInfos[tokenId].currentCollateral, _getNftRequiredCollateral(tokenId))
.min(maxNftPower);
}

function _getNftRequiredCollateral(uint256 tokenId) internal view returns (uint256) {
uint256 requiredCollateralForNft = _nftInfos[tokenId].requiredCollateral;

return requiredCollateralForNft == 0 ? nftRequiredCollateral : requiredCollateralForNft;
}

function _isActiveNft(uint256 tokenId) internal view returns (bool) {
return _exists(tokenId) && block.timestamp >= powerCalcStartTimestamp;
}

function _onlyBeforePowerCalc() internal view {
require(
block.timestamp < powerCalcStartTimestamp,
Expand Down
4 changes: 4 additions & 0 deletions contracts/gov/ERC721/powers/ERC721EquivalentPower.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ contract ERC721EquivalentPower is AbstractERC721Power {
return _getEquivalentPower(_getRawNftMaxPower(tokenId));
}

function getNftMinPower(uint256 tokenId) public view override returns (uint256) {
return _getEquivalentPower(_getRawNftMinPower(tokenId));
}

function getNftPower(uint256 tokenId) public view override returns (uint256) {
return _getEquivalentPower(_getRawNftPower(tokenId));
}
Expand Down
4 changes: 4 additions & 0 deletions contracts/gov/ERC721/powers/ERC721RawPower.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ contract ERC721RawPower is AbstractERC721Power {
return _getRawNftMaxPower(tokenId).min(totalRawPower);
}

function getNftMinPower(uint256 tokenId) public view override returns (uint256) {
return _getRawNftMinPower(tokenId).min(totalRawPower);
}

function getNftPower(uint256 tokenId) public view override returns (uint256) {
return _getRawNftPower(tokenId).min(totalRawPower);
}
Expand Down
14 changes: 14 additions & 0 deletions contracts/gov/GovPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ contract GovPool is
_unlock(msg.sender);
_unlock(delegatee);

_updateNftPowers(nftIds);

_govUserKeeper.delegateTokens.exec(delegatee, amount);
_govUserKeeper.delegateNfts.exec(delegatee, nftIds);

Expand Down Expand Up @@ -281,6 +283,8 @@ contract GovPool is
nft.safeTransferFrom(address(this), address(_govUserKeeper), nftIds[i]);
}

_updateNftPowers(nftIds);

_govUserKeeper.delegateNftsTreasury(delegatee, nftIds);
}

Expand All @@ -300,6 +304,8 @@ contract GovPool is

_unlock(delegatee);

_updateNftPowers(nftIds);

_govUserKeeper.undelegateTokens.exec(delegatee, amount);
_govUserKeeper.undelegateNfts.exec(delegatee, nftIds);

Expand All @@ -321,6 +327,8 @@ contract GovPool is

_unlock(msg.sender);

_updateNftPowers(nftIds);

_govUserKeeper.undelegateTokensTreasury.exec(delegatee, amount);
_govUserKeeper.undelegateNftsTreasury.exec(delegatee, nftIds);

Expand Down Expand Up @@ -555,6 +563,8 @@ contract GovPool is
uint256[] calldata voteNftIds,
bool isVoteFor
) internal {
_updateNftPowers(voteNftIds);

_proposals.vote(_userInfos, proposalId, voteAmount, voteNftIds, isVoteFor);
}

Expand All @@ -572,6 +582,10 @@ contract GovPool is
}
}

function _updateNftPowers(uint256[] calldata nftIds) internal {
_govUserKeeper.updateNftPowers(nftIds);
}

function _unlock(address user) internal {
_userInfos.unlockInProposals(user);
}
Expand Down
Loading

0 comments on commit b2b30da

Please sign in to comment.