Skip to content

Commit

Permalink
whiteList_players
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMansour committed Jun 24, 2023
1 parent 9cb0f5a commit 4727ddc
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 26 deletions.
41 changes: 39 additions & 2 deletions packages/hardhat/contracts/Quest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./libraries/Deposit.sol";
import "./libraries/Models.sol";
import "./libraries/IExecutable.sol";
import "hardhat/console.sol";

contract Quest is IExecutable {
using SafeMath for uint256;
Expand All @@ -22,7 +23,7 @@ contract Quest is IExecutable {
address public aragonGovernAddress;
address payable public fundsRecoveryAddress;
uint32 public maxPlayers; // 0 for unlimited players

bool public isWhiteList;
Models.Claim[] public claims;
Models.Deposit public createDeposit;
Models.Deposit public playDeposit;
Expand All @@ -33,6 +34,14 @@ contract Quest is IExecutable {
event QuestClaimed(bytes evidence, address player, uint256 amount);
event QuestPlayed(address player, uint256 timestamp);
event QuestUnplayed(address player, uint256 timestamp);
event WhiteListChanged(address[] whiteListPlayers, uint256 timestamp);
modifier OnlyCreator() {
require(
msg.sender == questCreator,
"Only creator can call this function"
);
_;
}

constructor(
string memory _questTitle,
Expand All @@ -44,8 +53,18 @@ contract Quest is IExecutable {
Models.Deposit memory _createDeposit,
Models.Deposit memory _playDeposit,
address _questCreator,
uint32 _maxPlayers
uint32 _maxPlayers,
bool _isWhiteList
) {
// uint32 check = _isWhiteList ? 1 : 0;
// console.log("check:", check);
// console.log("Max players:", maxPlayers);
// console.log("Maxplayers * check = ", maxPlayers * check);
require(
!(_maxPlayers > 0 && _isWhiteList),
// _maxPlayers * check == 0,
"ERROR: Can't create a whiteListed quest with max players greater than 0 (infinity)"
);
questTitle = _questTitle;
questDetailsRef = _questDetailsRef;
rewardToken = _rewardToken;
Expand All @@ -58,6 +77,7 @@ contract Quest is IExecutable {

isCreateDepositReleased = false;
maxPlayers = _maxPlayers;
isWhiteList = _isWhiteList;
}

/*
Expand Down Expand Up @@ -169,6 +189,10 @@ contract Quest is IExecutable {
* emit QuestPlayed with player and timestamp
*/
function play(address _player) external {
require(
isWhiteList == false,
"ERROR: Can't self register and play a whitelisted Quest"
);
require(
msg.sender == _player || msg.sender == questCreator,
"ERROR: Sender not player nor creator"
Expand All @@ -187,6 +211,15 @@ contract Quest is IExecutable {
emit QuestPlayed(_player, block.timestamp);
}

function setWhiteList(address[] memory _players) external OnlyCreator {
require(
isWhiteList == true,
"ERROR: Can't set the white list to a non-whitelisted contract"
);
playerList = _players;
emit WhiteListChanged(_players, block.timestamp);
}

/**
* Unregister a player from the quest. (sender could be the player or quest creator)
* @param _player Player address.
Expand All @@ -197,6 +230,10 @@ contract Quest is IExecutable {
* emit QuestUnplayed with player and timestamp
*/
function unplay(address _player) external {
require(
isWhiteList == false,
"ERROR: can't unplay a whitelisted quest"
);
require(
msg.sender == _player || msg.sender == questCreator,
"ERROR: Sender not player nor creator"
Expand Down
12 changes: 8 additions & 4 deletions packages/hardhat/contracts/QuestFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ contract QuestFactory is Ownable {
address playDepositToken,
uint256 playDepositAmount,
address creator,
uint32 maxPlayers
uint32 maxPlayers,
bool isWhiteList
);

event CreateDepositChanged(
Expand Down Expand Up @@ -94,7 +95,8 @@ contract QuestFactory is Ownable {
IERC20 _rewardToken,
uint256 _expireTime,
address payable _fundsRecoveryAddress,
uint32 _maxPlayers
uint32 _maxPlayers,
bool _isWhiteList
) external returns (address) {
Quest quest = new Quest(
_questTitle,
Expand All @@ -106,7 +108,8 @@ contract QuestFactory is Ownable {
Models.Deposit(createDeposit.token, createDeposit.amount),
Models.Deposit(playDeposit.token, playDeposit.amount),
msg.sender,
_maxPlayers
_maxPlayers,
_isWhiteList
);

// Collect deposit from quest creator and send it to quest
Expand All @@ -124,7 +127,8 @@ contract QuestFactory is Ownable {
address(playDeposit.token),
playDeposit.amount,
msg.sender,
_maxPlayers
_maxPlayers,
_isWhiteList
);

return address(quest);
Expand Down
155 changes: 155 additions & 0 deletions packages/hardhat/test/Quest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,39 @@ describe("[Contract] Quest", function () {
// Assert
await expect(act()).to.be.revertedWith("ERROR: Player already exists");
});
it("SHOULD revert if it's a whitelisted quest", async () => {
// Arrange
const quest = await deployQuest(
"fakeTitle",
"0x",
rewardToken,
epochNow + 3600, // in 1 hour
govern.address,
creator.address,
fromNumber(0),
createDepositToken,
depositAmount,
playDepositToken,
depositAmount,
creator,
0,
true
);
const playerInitialBalance = fromNumber(1000);
await playDepositToken
.connect(player)
.mint(player.address, playerInitialBalance);
await playDepositToken
.connect(player)
.approve(quest.address, depositAmount);
// Act
const act = () => quest.connect(player).play(player.address);

// Assert
await expect(act()).to.be.revertedWith(
"ERROR: Can't self register and play a whitelisted Quest"
);
});
});

describe("unplay()", () => {
Expand Down Expand Up @@ -869,6 +902,128 @@ describe("[Contract] Quest", function () {
// Assert
await expect(act()).to.be.revertedWith("ERROR: player not in list");
});
it("SHOULD revert WHEN quest is whitelisted", async () => {
// Arrange
const quest = await deployQuest(
"fakeTitle",
"0x",
rewardToken,
epochNow + 3600, // in 1 hour
govern.address,
creator.address,
fromNumber(0),
createDepositToken,
depositAmount,
playDepositToken,
depositAmount,
creator,
0,
true
);
const playerInitialBalance = fromNumber(1000);
await playDepositToken
.connect(player)
.mint(player.address, playerInitialBalance);
await playDepositToken
.connect(player)
.approve(quest.address, depositAmount);

// Act
const act = () => quest.connect(player).unplay(player.address);

// Assert
await expect(act()).to.be.revertedWith(
"ERROR: can't unplay a whitelisted quest"
);
});
});
describe.only("constructor()", () => {
it("SHOULD revert if max players greater than 0 and isWhiteList", async () => {
//Arrange
const maxPlayers = 3;

//Act
const act = () =>
deployQuest(
"fakeTitle",
"0x",
rewardToken,
epochNow + 3600, // in 1 hour
govern.address,
creator.address,
fromNumber(0),
createDepositToken,
depositAmount,
playDepositToken,
depositAmount,
creator,
maxPlayers,
true
);
//Assert
await expect(act()).to.be.revertedWith(
"ERROR: Can't create a whiteListed quest with max players greater than 0 (infinity)"
);
});
});
describe("setWhiteList()", () => {
it("SHOULD set the player list to the one in the parameters", async () => {
const quest = await deployQuest(
"fakeTitle",
"0x",
rewardToken,
epochNow + 3600, // in 1 hour
govern.address,
creator.address,
fromNumber(0),
createDepositToken,
depositAmount,
playDepositToken,
depositAmount,
creator,
0,
true
);
const act = () =>
quest.connect(creator).setWhiteList([player.address, other.address]);
await expect(act()).to.emit(quest, "WhiteListChanged");
expect(await quest.getPlayers()).to.deep.eq([
player.address,
other.address,
]);
expect(await quest.canExecute(player.address)).to.eq(true);
expect(await quest.canExecute(other.address)).to.eq(true);
});
it("SHOULD revert if it's not a whitelisted quest", async () => {
const quest = await deployQuest(
"fakeTitle",
"0x",
rewardToken,
epochNow + 3600, // in 1 hour
govern.address,
creator.address,
fromNumber(0),
createDepositToken,
depositAmount,
playDepositToken,
depositAmount,
creator,
0,
false
);
// const act = () =>
// quest
// .connect(govern)
// .claim(evidence, player.address, claimAmount, false);

// // Assert
// await expect(act()).to.be.revertedWith("ERROR: No evidence");
const act = () =>
quest.connect(creator).setWhiteList([player.address, other.address]);
await expect(act()).to.be.revertedWith(
"ERROR: Can't set the white list to a non-whitelisted contract"
);
});
});

describe("getPlayers()", () => {
Expand Down
20 changes: 15 additions & 5 deletions packages/hardhat/test/QuestFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe("[Contract] QuestFactory", function () {
const detailIPFS = "0x";
const expireTime = 0; // Unix Epoch 0
const maxPlayers = 1;
const isWhiteList = false;
await createDepositToken
.connect(owner)
.approve(questFactoryContract.address, depositAmount);
Expand All @@ -91,7 +92,8 @@ describe("[Contract] QuestFactory", function () {
rewardToken.address,
expireTime,
owner.address,
maxPlayers
maxPlayers,
isWhiteList
)
).to.emit(questFactoryContract, "QuestCreated");
});
Expand All @@ -102,6 +104,7 @@ describe("[Contract] QuestFactory", function () {
const detailIPFS = "0x";
const expireTime = 0; // Unix Epoch 0
const maxPlayers = 1;
const isWhiteList = false;

await createDepositToken
.connect(owner)
Expand All @@ -115,7 +118,8 @@ describe("[Contract] QuestFactory", function () {
rewardToken.address,
expireTime,
owner.address,
maxPlayers
maxPlayers,
isWhiteList
)
);

Expand All @@ -131,6 +135,7 @@ describe("[Contract] QuestFactory", function () {
const detailIPFS = "0x";
const expireTime = 0; // Unix Epoch 0
const maxPlayers = 1;
const isWhiteList = false;

await createDepositToken
.connect(owner)
Expand All @@ -143,7 +148,8 @@ describe("[Contract] QuestFactory", function () {
rewardToken.address,
expireTime,
owner.address,
maxPlayers
maxPlayers,
isWhiteList
);

// Assert
Expand Down Expand Up @@ -183,6 +189,7 @@ describe("[Contract] QuestFactory", function () {
it("already created quests SHOULD keep old deposit WHEN change deposit", async () => {
// Arrange
const maxPlayers = 1;
const isWhiteList = false;
await createDepositToken
.connect(owner)
.approve(questFactoryContract.address, depositAmount);
Expand All @@ -195,7 +202,8 @@ describe("[Contract] QuestFactory", function () {
rewardToken.address,
0,
owner.address,
maxPlayers
maxPlayers,
isWhiteList
)
);
const quest = new Quest__factory(owner).attach(questAddress);
Expand Down Expand Up @@ -243,6 +251,7 @@ describe("[Contract] QuestFactory", function () {
it("already created quests SHOULD keep old deposit WHEN change deposit", async () => {
// Arrange
const maxPlayers = 1;
const isWhiteList = false;
await createDepositToken
.connect(owner)
.approve(questFactoryContract.address, depositAmount);
Expand All @@ -255,7 +264,8 @@ describe("[Contract] QuestFactory", function () {
rewardToken.address,
0,
owner.address,
maxPlayers
maxPlayers,
isWhiteList
)
);
const quest = new Quest__factory(owner).attach(questAddress);
Expand Down
Loading

0 comments on commit 4727ddc

Please sign in to comment.