Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Aug 23, 2024
1 parent d8f3834 commit 805f146
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { utils } from "ethers";
import { ethers } from "hardhat";

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

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

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

beforeEach(async () => {
[owner, signer, user, ...addrs] = await ethers.getSigners();
const instantRewardsFactory = await ethers.getContractFactory("InstantRewards");

instantRewards = await instantRewardsFactory.deploy(signer.address, owner.address);

await instantRewards.deployed();
});

it("Should claim", async () => {
const currentBlock = await ethers.provider.getBlock("latest");
const sigExpiration = currentBlock.timestamp + 1000;
const amount = utils.parseEther("1");
const taskId = encodeTaskId("1/1/1");
const to = owner.address;

// transfer some funds to the contract
await owner.sendTransaction({
to: instantRewards.address,
value: amount,
});

const claimData: ClaimData = {
amount,
sigExpiration,
taskId,
to,
};

const signature = await getSignature(signer, claimData);
const claimDataSigned = {
...claimData,
signature,
};

const tx = instantRewards.claim(claimDataSigned);
await expect(tx).to.emit(instantRewards, "Claimed").withArgs(owner.address, taskId, amount);
});

it("Should transfer ownership", async () => {
{
const ownerAddr = await instantRewards.owner();
expect(ownerAddr).to.be.eq(owner.address);
}
await instantRewards.transferOwnership(user.address);
{
const ownerAddr = await instantRewards.owner();
expect(ownerAddr).to.be.eq(user.address);
}
});
});
33 changes: 33 additions & 0 deletions packages/zevm-app-contracts/test/instant-rewards/test.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { BigNumber } from "ethers";
import { ethers } from "hardhat";

export interface Signature {
r: string;
s: string;
v: number;
}

export interface ClaimData {
amount: BigNumber;
sigExpiration: number;
taskId: string;
to: string;
}

export interface ClaimDataSigned extends ClaimData {
signature: Signature;
}

export const getSignature = async (signer: SignerWithAddress, claimData: ClaimData) => {
let payload = ethers.utils.defaultAbiCoder.encode(
["address", "uint256", "bytes32", "uint256"],
[claimData.to, claimData.sigExpiration, claimData.taskId, claimData.amount]
);

const payloadHash = ethers.utils.keccak256(payload);

// This adds the message prefix
const signature = await signer.signMessage(ethers.utils.arrayify(payloadHash));
return ethers.utils.splitSignature(signature);
};

0 comments on commit 805f146

Please sign in to comment.