Skip to content

Commit

Permalink
v3.0.4 - CapxCommunityQuestContracts: fn added for withdrawing questR…
Browse files Browse the repository at this point in the history
…ewards.
  • Loading branch information
sanjaybaskaran01 committed Jan 10, 2024
1 parent c5680b1 commit 4287c4d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 73 deletions.
90 changes: 58 additions & 32 deletions contracts/CapxCommunityQuest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ contract CapxCommunityQuest is
return currCapxQuest.rewardToken;
}

function disableQuest(
function withdrawQuestRewards(
uint256 _questNumber
) external nonReentrant onlyForger {
CapxQuestDetails storage currCapxQuest = communityQuestDetails[
Expand All @@ -215,30 +215,22 @@ contract CapxCommunityQuest is
pendingRewards
) revert NoRewardsToWithdraw();

lastKnownBalance[currCapxQuest.rewardToken] -= pendingRewards;
if (pendingRewards > 0) {
lastKnownBalance[currCapxQuest.rewardToken] -= pendingRewards;

require(
IERC20(currCapxQuest.rewardToken).approve(owner(), pendingRewards)
);
IERC20(currCapxQuest.rewardToken).safeTransfer(owner(), pendingRewards);
}

function enableQuest(
uint256 _questNumber,
address _authorizedCaller
) external nonReentrant onlyForger {
CapxQuestDetails storage currCapxQuest = communityQuestDetails[
_questNumber
];
uint256 pendingRewards = currCapxQuest.totalRewardAmountInWei -
currCapxQuest.claimedRewards;
IERC20(currCapxQuest.rewardToken).safeTransferFrom(
_authorizedCaller,
address(this),
pendingRewards
);

lastKnownBalance[currCapxQuest.rewardToken] += pendingRewards;
require(
IERC20(currCapxQuest.rewardToken).approve(
owner(),
pendingRewards
)
);
IERC20(currCapxQuest.rewardToken).safeTransfer(
owner(),
pendingRewards
);
}
currCapxQuest.maxRewardAmountInWei = 0;
currCapxQuest.totalRewardAmountInWei = 0;
}

function updateRewards(
Expand Down Expand Up @@ -282,15 +274,44 @@ contract CapxCommunityQuest is
currCapxQuest.maxRewardAmountInWei = _maxRewardAmountInWei;
}

function withdrawTokens(
address[] memory tokens
) external nonReentrant onlyForger {
function withdrawAllQuestRewards() external nonReentrant onlyForger {
isCommunityActive = false;
for (uint256 i = 0; i < tokens.length; i++) {
IERC20(tokens[i]).safeTransfer(
_msgSender(),
IERC20(tokens[i]).balanceOf(address(this))
);
for (
uint256 _questNumber = 1;
_questNumber <= communityQuestCount;
_questNumber++
) {
CapxQuestDetails storage currCapxQuest = communityQuestDetails[
_questNumber
];
if (currCapxQuest.totalRewardAmountInWei != 0) {
uint256 pendingRewards = currCapxQuest.totalRewardAmountInWei -
currCapxQuest.claimedRewards;
if (
IERC20(currCapxQuest.rewardToken).balanceOf(address(this)) <
pendingRewards
) revert NoRewardsToWithdraw();

if (pendingRewards > 0) {
lastKnownBalance[
currCapxQuest.rewardToken
] -= pendingRewards;

require(
IERC20(currCapxQuest.rewardToken).approve(
owner(),
pendingRewards
)
);
IERC20(currCapxQuest.rewardToken).safeTransfer(
owner(),
pendingRewards
);
}
}

currCapxQuest.maxRewardAmountInWei = 0;
currCapxQuest.totalRewardAmountInWei = 0;
}
}

Expand All @@ -307,4 +328,9 @@ contract CapxCommunityQuest is
)
);
}

function toggleCommunityActive() external onlyForger returns (bool) {
isCommunityActive = !isCommunityActive;
return isCommunityActive;
}
}
79 changes: 56 additions & 23 deletions contracts/CapxCommunityQuestForger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ contract CapxCommunityQuestForger is
_questId
];

if (currentDetails.rewardType == 2) revert InvalidRewardType();
if (currentDetails.active == true) revert QuestMustBeDisabled();

if (currentDetails.rewardType != 1 && currentDetails.rewardType != 3)
revert InvalidRewardType();

ICapxCommunityQuest(communityAddress).updateRewards(
_msgSender(),
Expand All @@ -387,7 +390,9 @@ contract CapxCommunityQuestForger is
_questId
];

if (currentDetails.rewardType == 1) revert InvalidRewardType();
if (currentDetails.active == true) revert QuestMustBeDisabled();
if (currentDetails.rewardType != 2 && currentDetails.rewardType != 3)
revert InvalidRewardType();

if (address(capxReputationScore) == address(0))
revert CapxReputationContractNotInitialised();
Expand Down Expand Up @@ -420,6 +425,8 @@ contract CapxCommunityQuestForger is
_questId
];

if (currentDetails.active == true) revert QuestMustBeDisabled();

if (currentDetails.rewardType == _rewardUpdateData.rewardType)
revert UseRewardTypeSpecificFunctions();

Expand Down Expand Up @@ -605,10 +612,7 @@ contract CapxCommunityQuestForger is
string calldata _communityId,
uint256 _questNumber
) external nonReentrant onlyCommunityAuthorized(_communityId) {
(
address _communityAddress,
string memory _questId
) = getQuestIdAndContractAddress(_communityId, _questNumber);
string memory _questId = getQuestId(_communityId, _questNumber);

if (isCapxCommunityQuest[_questId] == address(0))
revert InvalidQuestNumber();
Expand All @@ -619,20 +623,14 @@ contract CapxCommunityQuestForger is

if (currCapxQuest.active == false) revert QuestAlreadyDisabled();

if (currCapxQuest.rewardType == 1 || currCapxQuest.rewardType == 3) {
ICapxCommunityQuest(_communityAddress).disableQuest(_questNumber);
}
currCapxQuest.active = false;
}

function enableQuest(
string calldata _communityId,
uint256 _questNumber
) external nonReentrant onlyCommunityAuthorized(_communityId) {
(
address _communityAddress,
string memory _questId
) = getQuestIdAndContractAddress(_communityId, _questNumber);
string memory _questId = getQuestId(_communityId, _questNumber);

if (isCapxCommunityQuest[_questId] == address(0))
revert InvalidQuestNumber();
Expand All @@ -643,13 +641,6 @@ contract CapxCommunityQuestForger is

if (currCapxQuest.active == true) revert QuestAlreadyActive();

if (currCapxQuest.rewardType == 1 || currCapxQuest.rewardType == 3) {
ICapxCommunityQuest(_communityAddress).enableQuest(
_questNumber,
_msgSender()
);
}

currCapxQuest.active = true;
}

Expand Down Expand Up @@ -696,12 +687,41 @@ contract CapxCommunityQuestForger is
return (_questId);
}

function withdrawTokens(
function withdrawQuestRewards(
string calldata _communityId,
address[] memory tokens
uint256 _questNumber
) external nonReentrant onlyCommunityAuthorized(_communityId) {
string memory _questId = getQuestId(_communityId, _questNumber);
address _communityAddress = isCapxCommunityQuest[_questId];
if (_communityAddress == address(0)) revert InvalidQuestNumber();

CapxQuestDetails storage currCapxQuest = communityQuestDetails[
_questId
];
if (currCapxQuest.active == true) revert QuestMustBeDisabled();
if (currCapxQuest.rewardType != 1 && currCapxQuest.rewardType != 3)
revert InvalidRewardType();
ICapxCommunityQuest(_communityAddress).withdrawQuestRewards(
_questNumber
);
}

function withdrawAllQuestRewards(
string calldata _communityId
) external nonReentrant onlyCommunityOwners(_communityId) {
address communityAddress = community[_communityId];
ICapxCommunityQuest(communityAddress).withdrawTokens(tokens);
for (
uint256 _questNumber = 1;
_questNumber <= communityQuestCount[_communityId];
_questNumber++
) {
string memory _questId = getQuestId(_communityId, _questNumber);
CapxQuestDetails storage currCapxQuest = communityQuestDetails[
_questId
];
currCapxQuest.active = false;
}
ICapxCommunityQuest(communityAddress).withdrawAllQuestRewards();
}

function withdrawETH(
Expand All @@ -710,4 +730,17 @@ contract CapxCommunityQuestForger is
address communityAddress = community[_communityId];
ICapxCommunityQuest(communityAddress).withdrawETH(_msgSender());
}

function toggleCommunityActive(
string calldata _communityId
)
external
onlyCommunityAuthorized(_communityId)
returns (bool isCommunityActive)
{
address communityAddress = community[_communityId];
bool _isCommunityActive = ICapxCommunityQuest(communityAddress)
.toggleCommunityActive();
return _isCommunityActive;
}
}
16 changes: 8 additions & 8 deletions contracts/CapxTokenForger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract contract TokenPoweredByCapx {
string memory name_,
string memory symbol_,
address owner_,
address capxQuestForger_,
address capxCommunityQuestForger_,
uint256 initialSupply_,
uint256 totalCappedSupply_
) public virtual;
Expand All @@ -22,7 +22,7 @@ abstract contract TokenPoweredByCapx {
contract CapxTokenForger is Initializable, UUPSUpgradeable, OwnableUpgradeable, PausableUpgradeable, ReentrancyGuardUpgradeable {

address public tokenPoweredByCapx;
address public capxQuestForger;
address public capxCommunityQuestForger;
mapping(address => bool) public tokensPoweredByCapx;

event NewTokenPoweredByCapx (
Expand Down Expand Up @@ -78,10 +78,10 @@ contract CapxTokenForger is Initializable, UUPSUpgradeable, OwnableUpgradeable,
tokenPoweredByCapx = _tokenPoweredByCapx;
}

function updateCapxQuestForger(
address _capxQuestForger
) external onlyOwner checkIsAddressValid(_capxQuestForger) whenNotPaused {
capxQuestForger = _capxQuestForger;
function updatecapxCommunityQuestForger(
address _capxCommunityQuestForger
) external onlyOwner checkIsAddressValid(_capxCommunityQuestForger) whenNotPaused {
capxCommunityQuestForger = _capxCommunityQuestForger;
}

function createTokenPoweredByCapx(
Expand All @@ -91,7 +91,7 @@ contract CapxTokenForger is Initializable, UUPSUpgradeable, OwnableUpgradeable,
uint256 _initialSupplyInWei,
uint256 _totalCappedSupplyInWei
) external onlyOwner checkIsAddressValid(_owner) nonReentrant() whenNotPaused virtual returns(address _tokenPoweredByCapx) {
require(capxQuestForger != address(0),"CapxQuestForger NOT configured.");
require(capxCommunityQuestForger != address(0),"capxCommunityQuestForger NOT configured.");
require(_totalCappedSupplyInWei != 0,"Token's Maximum Capped Supply cannot be ZERO");
_tokenPoweredByCapx = Clones.clone(tokenPoweredByCapx);
tokensPoweredByCapx[_tokenPoweredByCapx] = true;
Expand All @@ -109,7 +109,7 @@ contract CapxTokenForger is Initializable, UUPSUpgradeable, OwnableUpgradeable,
_name,
_symbol,
_owner,
capxQuestForger,
capxCommunityQuestForger,
_initialSupplyInWei,
_totalCappedSupplyInWei
);
Expand Down
8 changes: 4 additions & 4 deletions contracts/TokenPoweredByCapx.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ contract TokenPoweredByCapx is IERC20, IERC20Metadata, Ownable, Pausable, Initia
string memory name_,
string memory symbol_,
address owner_,
address capxQuestForger_,
address capxCommunityQuestForger_,
uint256 initialSupply_,
uint256 totalCappedSupply_
) checkIsAddressValid(owner_) external {
require(!_initialized,"TokenPoweredByCapx: Already Initialized.");
require(address(capxQuestForger_) != address(0),"TokenPoweredByCapx: Invalid CapxQuestForger.");
require(address(capxCommunityQuestForger_) != address(0),"TokenPoweredByCapx: Invalid capxCommunityQuestForger.");
_name = name_;
_symbol = symbol_;
_decimal = 18;
Expand All @@ -76,8 +76,8 @@ contract TokenPoweredByCapx is IERC20, IERC20Metadata, Ownable, Pausable, Initia

// Transfer Ownership
_transferOwnership(owner_);
authorized[capxQuestForger_] = true;
whitelist[capxQuestForger_] = true;
authorized[capxCommunityQuestForger_] = true;
whitelist[capxCommunityQuestForger_] = true;

// Mint Initial Supply
_mint(owner_, initialSupply_);
Expand Down
9 changes: 3 additions & 6 deletions contracts/interfaces/ICapxCommunityQuest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ interface ICapxCommunityQuest {
) external;

function setQuestDetails(QuestDTO memory quest) external;
function enableQuest(
uint256 _questNumber,
address authorizedCaller
) external;
function disableQuest(uint256 _questNumber) external;
function withdrawTokens(address[] memory tokens) external;
function withdrawQuestRewards(uint256 _questNumber) external;
function withdrawAllQuestRewards() external;
function withdrawETH(address caller) external;
function toggleCommunityActive() external returns (bool);
}
1 change: 1 addition & 0 deletions contracts/interfaces/ICapxCommunityQuestForger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface ICapxCommunityQuestForger {
error AlreadyNotAuthorized();
error UseRewardTypeSpecificFunctions();
error InvalidIOURewards();
error QuestMustBeDisabled();

struct CreateQuest {
address rewardToken;
Expand Down

0 comments on commit 4287c4d

Please sign in to comment.