-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8f3834
commit 805f146
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/zevm-app-contracts/test/instant-rewards/instant-rewards.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
packages/zevm-app-contracts/test/instant-rewards/test.helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |