import { ethers, waffle } from "hardhat"; import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/dist/src/signer-with-address"; import { expect } from "chai"; // types import { Comp } from "../typechain"; import { User } from "./types"; // contract artifacts import CompArtifact from "../artifacts/contracts/Comp.sol/Comp.json"; import { splitSignature } from "ethers/lib/utils"; const { deployContract } = waffle; describe("Token - Unit tests", function () { const compInterface = new ethers.utils.Interface(CompArtifact.abi); let admin: User; let delegatee: User; let comp: Comp; const expiry = 10000000000; before(async function () { const signers: SignerWithAddress[] = await ethers.getSigners(); admin = { signer: signers[0], address: await signers[0].getAddress(), }; delegatee = { signer: signers[1], address: await signers[1].getAddress(), }; // contract deployment comp = (await deployContract(admin.signer, CompArtifact, [ admin.address, ])) as Comp; }); it("successfully deploys", async function () { expect(ethers.utils.isAddress(await comp.address)).to.be.true; }); describe("Delegation", function () { it("should be able to delegate by signature", async function () { const name = await comp.name(); const nonce = await comp.nonces(admin.address); const chainId = await admin.signer.getChainId(); const types = { EIP712Domain: [ { name: "name", type: "string" }, { name: "chainId", type: "uint256" }, { name: "verifyingContract", type: "address" }, ], Delegation: [ { name: "delegatee", type: "address" }, { name: "nonce", type: "uint256" }, { name: "expiry", type: "uint256" }, ], }; const primaryType = "Delegation"; const domain = { name, chainId, verifyingContract: comp.address, }; const message = { delegatee: delegatee.address, nonce, expiry }; const messageFormatted = JSON.stringify({ types, primaryType, domain, message, }); const signedMessage = await admin.signer.signMessage(messageFormatted); const splitedMessage = splitSignature(signedMessage); console.log("used addresses", { admin: admin.address, delegatee: delegatee.address, splitedMessage, }); await comp .connect(admin.signer) .delegateBySig( delegatee.address, nonce, expiry, splitedMessage.v, splitedMessage.r, splitedMessage.s ); const lastNonce = await comp.nonces(admin.address); console.log("Last nonce from contract", lastNonce); }); }); });