Skip to content

Commit

Permalink
fix(quest budget): revise approval handling amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Quazia committed Jun 3, 2024
1 parent 4212f67 commit b72007a
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 80 deletions.
178 changes: 178 additions & 0 deletions broadcast/QuestBudget.s.sol/11155111/run-1717450347.json

Large diffs are not rendered by default.

178 changes: 178 additions & 0 deletions broadcast/QuestBudget.s.sol/11155111/run-1717450764.json

Large diffs are not rendered by default.

136 changes: 68 additions & 68 deletions broadcast/QuestBudget.s.sol/11155111/run-latest.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion contracts/QuestBudget.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ contract QuestBudget is Budget, IERC1155Receiver, ReentrancyGuard {
string memory projectName_,
uint256 referralRewardFee_
) public virtual onlyAuthorized returns (address) {
rewardTokenAddress_.safeApprove(address(questFactory), rewardAmount_);
uint256 maxTotalRewards = totalParticipants_ * rewardAmount_;
uint256 questFee = uint256(IQuestFactory(questFactory).questFee());
uint256 maxProtocolReward = (maxTotalRewards * questFee) / 10_000; // Assuming questFee is 2000
uint256 approvalAmount = maxTotalRewards + maxProtocolReward;
rewardTokenAddress_.safeApprove(address(questFactory), approvalAmount);
return IQuestFactory(questFactory).createERC20Quest(
txHashChainId_,
rewardTokenAddress_,
Expand Down
3 changes: 2 additions & 1 deletion contracts/interfaces/IQuestFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ interface IQuestFactory {
string memory actionType,
string memory questName
) external pure returns (string memory);

function questFee() external view returns (uint16);

// Create
function create1155QuestAndQueue(
address rewardTokenAddress_,
Expand Down
28 changes: 19 additions & 9 deletions test/QuestBudget.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,22 @@ contract QuestBudgetTest is Test, TestUtils, IERC1155Receiver {
uint256 endTime_ = block.timestamp + 1 days;
uint256 startTime_ = block.timestamp;
uint256 totalParticipants_ = 10;
uint256 rewardAmount_ = 100 ether;
uint256 rewardAmount_ = 1 ether;
string memory questId_ = "testQuest";
string memory actionType_ = "testAction";
string memory questName_ = "Test Quest";
string memory projectName_ = "Test Project";
uint256 referralRewardFee_ = 10 ether;


uint256 maxTotalRewards = totalParticipants_ * rewardAmount_;
uint256 questFee = uint256(mockQuestFactory.questFee());
uint256 maxProtocolReward = (maxTotalRewards * questFee) / 10_000; // Assuming questFee is 2000
uint256 approvalAmount = maxTotalRewards + maxProtocolReward;
mockERC20.mint(address(this), approvalAmount);
// Ensure the budget has enough tokens for the reward
mockERC20.approve(address(questBudget), rewardAmount_);
mockERC20.approve(address(questBudget), approvalAmount);
questBudget.allocate(
_makeFungibleTransfer(Budget.AssetType.ERC20, address(mockERC20), address(this), rewardAmount_)
_makeFungibleTransfer(Budget.AssetType.ERC20, address(mockERC20), address(this), approvalAmount)
);

// Create the new quest
Expand All @@ -410,7 +415,7 @@ contract QuestBudgetTest is Test, TestUtils, IERC1155Receiver {
assertTrue(questAddress != address(0));

// Ensure the quest contract has the correct reward amount
assertEq(IERC20(rewardTokenAddress_).balanceOf(questAddress), rewardAmount_);
assertEq(IERC20(rewardTokenAddress_).balanceOf(questAddress), approvalAmount);
}

///////////////////////////
Expand All @@ -424,16 +429,21 @@ contract QuestBudgetTest is Test, TestUtils, IERC1155Receiver {
uint256 endTime_ = block.timestamp + 1 days;
uint256 startTime_ = block.timestamp;
uint256 totalParticipants_ = 10;
uint256 rewardAmount_ = 100 ether;
uint256 rewardAmount_ = 1 ether;
string memory questId_ = "testQuest";
string memory actionType_ = "testAction";
string memory questName_ = "Test Quest";
string memory projectName_ = "Test Project";
uint256 referralRewardFee_ = 10 ether;

uint256 maxTotalRewards = totalParticipants_ * rewardAmount_;
uint256 questFee = uint256(mockQuestFactory.questFee());
uint256 maxProtocolReward = (maxTotalRewards * questFee) / 10_000; // Assuming questFee is 2000
uint256 approvalAmount = maxTotalRewards + maxProtocolReward;
mockERC20.mint(address(this), approvalAmount);
// Ensure the budget has enough tokens for the reward
mockERC20.approve(address(questBudget), rewardAmount_);
bytes memory allocateBytes = _makeFungibleTransfer(Budget.AssetType.ERC20, address(mockERC20), address(this), rewardAmount_);
mockERC20.approve(address(questBudget), approvalAmount);
bytes memory allocateBytes = _makeFungibleTransfer(Budget.AssetType.ERC20, address(mockERC20), address(this), approvalAmount);
questBudget.allocate(allocateBytes);
console.logBytes(allocateBytes);

Expand All @@ -456,7 +466,7 @@ contract QuestBudgetTest is Test, TestUtils, IERC1155Receiver {
assertTrue(questAddress != address(0));

// Ensure the quest contract has the correct reward amount
assertEq(IERC20(rewardTokenAddress_).balanceOf(questAddress), rewardAmount_);
assertEq(IERC20(rewardTokenAddress_).balanceOf(questAddress), approvalAmount);

vm.expectEmit();

Expand Down
9 changes: 8 additions & 1 deletion test/mocks/QuestFactoryMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ contract QuestFactoryMock {
return false;
}

function questFee() external view returns (uint16) {
return 2000;
}


function createERC20Quest(
uint32 txHashChainId_,
Expand All @@ -71,6 +75,9 @@ contract QuestFactoryMock {
string memory projectName_,
uint256 referralRewardFee_
) external returns (address) {
uint256 maxTotalRewards = totalParticipants_ * rewardAmount_;
uint256 maxProtocolReward = (maxTotalRewards * 2000) / 10_000; // Assuming questFee is 2000
uint256 approvalAmount = maxTotalRewards + maxProtocolReward;
questData = QuestData({
txHashChainId: txHashChainId_,
rewardTokenAddress: rewardTokenAddress_,
Expand All @@ -87,7 +94,7 @@ contract QuestFactoryMock {
});

// Transfer rewardAmount_ tokens from the caller to this contract
require(IERC20(rewardTokenAddress_).transferFrom(msg.sender, address(this), rewardAmount_), "Transfer failed");
require(IERC20(rewardTokenAddress_).transferFrom(msg.sender, address(this), approvalAmount), "Transfer failed");

// Return this contract's address as the "created" quest contract
return address(this);
Expand Down

0 comments on commit b72007a

Please sign in to comment.