Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Nov 6, 2024
1 parent 5715168 commit ae9aad8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "./InstantRewardsV2.sol";

contract InstantRewardsFactory is Ownable2Step {
bool public allowPublicCreation = false;

error AccessDenied();
event InstantRewardsCreated(address indexed instantRewards, address indexed owner);

constructor(address owner) Ownable() {
transferOwnership(owner);
}

function setAllowPublicCreation(bool allowPublicCreation_) external onlyOwner {
allowPublicCreation = allowPublicCreation_;
}

function createInstantRewards(
address signerAddress,
uint256 start,
uint256 end,
string memory name
) external returns (address) {
bool isOwner = owner() == msg.sender;
if (!allowPublicCreation && !isOwner) revert AccessDenied();

InstantRewardsV2 instantRewards = new InstantRewardsV2(signerAddress, owner(), start, end, name);
instantRewards.transferOwnership(owner());
emit InstantRewardsCreated(address(instantRewards), owner());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract InstantRewardsV2 is InstantRewards {
super.claim(claimData);
}

function withdraw(address wallet, uint256 amount) public override {
function withdraw(address wallet, uint256 amount) public override onlyOwner {
if (isActive()) revert InstantRewardStillActive();
super.withdraw(wallet, amount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ClaimData, getSignature } from "./test.helpers";

const HARDHAT_CHAIN_ID = 1337;

describe("Instant Rewards Contract test", () => {
describe("Instant Rewards V2 Contract Compatibility test", () => {
let instantRewards: InstantRewardsV2,
owner: SignerWithAddress,
signer: SignerWithAddress,
Expand Down Expand Up @@ -198,7 +198,7 @@ describe("Instant Rewards Contract test", () => {
to
);

instantRewards.claim(claimDataSigned);
await instantRewards.claim(claimDataSigned);

const tx = instantRewards.claim(claimDataSigned);
await expect(tx).to.revertedWith("TaskAlreadyClaimed");
Expand Down Expand Up @@ -227,7 +227,7 @@ describe("Instant Rewards Contract test", () => {
taskId,
to
);
instantRewards.claim(claimDataSigned);
await instantRewards.claim(claimDataSigned);
}
const claimDataSigned = await getClaimDataSigned(
HARDHAT_CHAIN_ID,
Expand Down Expand Up @@ -275,7 +275,7 @@ describe("Instant Rewards Contract test", () => {
to
);

instantRewards.claim(newClaimDataSigned);
await instantRewards.claim(newClaimDataSigned);

const tx = instantRewards.claim(claimDataSigned);
await expect(tx).to.revertedWith("TaskAlreadyClaimed");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { BigNumber, utils } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";

import { InstantRewardsFactory, InstantRewardsV2 } from "../../typechain-types";
import { ClaimData, getSignature } from "./test.helpers";

const HARDHAT_CHAIN_ID = 1337;

describe("Instant Rewards Contract test", () => {
let instantRewardsFactory: InstantRewardsFactory,
deployer: SignerWithAddress,
owner: SignerWithAddress,
signer: SignerWithAddress,
user: SignerWithAddress,
addrs: SignerWithAddress[];

const encodeTaskId = (taskId: string) => utils.keccak256(utils.defaultAbiCoder.encode(["string"], [taskId]));

const getClaimDataSigned = async (
chainId: number,
verifyingContract: string,
signer: SignerWithAddress,
amount: BigNumber,
sigExpiration: number,
taskId: string,
to: string
) => {
const claimData: ClaimData = {
amount,
sigExpiration,
taskId,
to,
};

const signature = await getSignature(chainId, verifyingContract, signer, claimData);
return {
...claimData,
signature,
};
};

beforeEach(async () => {
[deployer, owner, signer, user, ...addrs] = await ethers.getSigners();
const instantRewardsFactoryF = await ethers.getContractFactory("InstantRewardsFactory");
instantRewardsFactory = (await instantRewardsFactoryF.deploy(owner.address)) as InstantRewardsFactory;
await instantRewardsFactory.deployed();
await instantRewardsFactory.connect(owner).acceptOwnership();
});

it("Should deploy an IR instance", async () => {
const currentBlock = await ethers.provider.getBlock("latest");
const start = currentBlock.timestamp + 1000;
const end = start + 1000;
const name = "Instant Rewards";
const tx = instantRewardsFactory.connect(owner).createInstantRewards(signer.address, start, end, name);
await expect(tx).to.emit(instantRewardsFactory, "InstantRewardsCreated");

const events = await instantRewardsFactory.queryFilter("InstantRewardsCreated");
const address = events[0].args?.instantRewards;
expect(address).to.be.not.undefined;

const instantRewards = (await ethers.getContractAt("InstantRewardsV2", address)) as InstantRewardsV2;
expect(await instantRewards.signerAddress()).to.be.eq(signer.address);
expect(await instantRewards.start()).to.be.eq(start);
expect(await instantRewards.end()).to.be.eq(end);
expect(await instantRewards.name()).to.be.eq(name);

await instantRewards.connect(owner).acceptOwnership();
expect(await instantRewards.owner()).to.be.eq(owner.address);
});

it("Should revert if not owner try to deploy", async () => {
const currentBlock = await ethers.provider.getBlock("latest");
const start = currentBlock.timestamp + 1000;
const end = start + 1000;
const name = "Instant Rewards";
const tx = instantRewardsFactory.createInstantRewards(signer.address, start, end, name);
await expect(tx).to.revertedWith("AccessDenied");
});

it("Should deploy an IR instance with any wallet if it's open", async () => {
await instantRewardsFactory.connect(owner).setAllowPublicCreation(true);

const currentBlock = await ethers.provider.getBlock("latest");
const start = currentBlock.timestamp + 1000;
const end = start + 1000;
const name = "Instant Rewards";
const tx = instantRewardsFactory.createInstantRewards(signer.address, start, end, name);
await expect(tx).to.emit(instantRewardsFactory, "InstantRewardsCreated");
});
});

0 comments on commit ae9aad8

Please sign in to comment.