Skip to content

Commit

Permalink
fix: fixed erc721 tests and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Apr 3, 2024
1 parent 63c3823 commit 414f29f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
6 changes: 6 additions & 0 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export async function generateErc721PermitSignature(context: Context, username:
throw new Error("RPC is not defined");
}

if (!NFT_CONTRACT_ADDRESS) {
const errorMesage = "NFT contract address is not defined";
logger.error(errorMesage);
throw new Error(errorMesage);
}

const beneficiary = await adapters.supabase.wallet.getWalletByUsername(username);
if (!beneficiary) {
logger.error("No wallet found for user");
Expand Down
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PermitGenerationSettings } from "./plugin-input";
import { createAdapters } from "../adapters";
import { Env } from "./env";

export type SupportedEvents = "issue_comment.created" | "workflow_dispatch" | "pull_request.closed";
export type SupportedEvents = "issue_comment.created" | "workflow_dispatch" | "pull_request.closed" | "issues.closed";

export interface Context<T extends WebhookEventName = SupportedEvents> {
eventName: T;
Expand Down
1 change: 1 addition & 0 deletions tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const mockContext = {
},
issue: {
number: 123,
id: 123,
},
pull_request: {
number: 123,
Expand Down
113 changes: 58 additions & 55 deletions tests/generate-erc721-permit.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import { MaxUint256 } from "@uniswap/permit2-sdk";
import { keccak256, toUtf8Bytes } from "ethers";
import { generateErc721PermitSignature } from "../src/handlers/generate-erc721-permit";
import { Context } from "../src/types/context";
import { Env } from "../src/types/env";
import { cypherText, mockContext, NFT_CONTRACT_ADDRESS, SPENDER } from "./constants";

describe.skip("generateErc721PermitSignature", () => {
describe("generateErc721PermitSignature", () => {
let context: Context;
const userId = 123;

Check failure on line 11 in tests/generate-erc721-permit.test.ts

View workflow job for this annotation

GitHub Actions / testing

TypeError: jest.autoMockOn is not a function. (In 'jest.autoMockOn()'

at /home/runner/work/permit-generation/permit-generation/tests/generate-erc721-permit.test.ts:11:3 at /home/runner/work/permit-generation/permit-generation/tests/generate-erc721-permit.test.ts:6:1
// cSpell: disable

jest.autoMockOn();

jest.mock("@supabase/supabase-js", () => {
return {
createClient: jest.fn().mockReturnValue({
from: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockReturnValue({
single: jest.fn().mockResolvedValue({ id: 123 }),
}),
}),
}),
}),
}),
}),
};
});

beforeEach(() => {
process.env.X25519_PRIVATE_KEY = "bHH4PDnwb2bsG9nmIu1KeIIX71twQHS-23wCPfKONls";
process.env.NFT_CONTRACT_ADDRESS = NFT_CONTRACT_ADDRESS;
Expand All @@ -35,7 +21,7 @@ describe.skip("generateErc721PermitSignature", () => {
context = {
...mockContext,
config: {
evmNetworkId: 1,
evmNetworkId: 100,
evmPrivateEncrypted: cypherText,
isNftRewardEnabled: true,
nftMinterPrivateKey: process.env.NFT_MINTER_PRIVATE_KEY,
Expand All @@ -53,44 +39,61 @@ describe.skip("generateErc721PermitSignature", () => {
issueID: 123,
},
} as unknown as Context;
context.env = process.env as Env;
context.eventName = "issues.closed";
jest.mock("@supabase/supabase-js", () => {
return {
createClient: jest.fn().mockReturnValue({
from: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
eq: jest.fn().mockReturnValue({
single: jest.fn().mockResolvedValue({ id: 123 }),
}),
}),
}),
}),
}),
}),
};
});
(context.adapters.supabase.wallet.getWalletByUsername as jest.Mock).mockReturnValue(SPENDER);
(context.adapters.supabase.user.getUserIdByWallet as jest.Mock).mockReturnValue(userId);
});

afterEach(() => {
jest.clearAllMocks();
});

it.skip("should generate ERC721 permit signature", async () => {
// const issueId = 123;
// const contributionType = "contribution";
// const username = "tester";

(context.adapters.supabase.wallet.getWalletByUsername as jest.Mock).mockReturnValue(SPENDER);
(context.adapters.supabase.user.getUserIdByWallet as jest.Mock).mockReturnValue(123);

// const result = await generateErc721PermitSignature(context, username, contributionType);
//
// const organizationName = "test";
// const repositoryName = "test";
// const issueNumber = issueId.toString();
// const userId = context.config.userId;
// const keys = ["GITHUB_ORGANIZATION_NAME", "GITHUB_REPOSITORY_NAME", "GITHUB_ISSUE_ID", "GITHUB_USERNAME", "GITHUB_CONTRIBUTION_TYPE"];

// if (result && typeof result === "object") {
// expect(result).toBeDefined();
// expect(result.tokenType).toBe("erc721");
// expect(result.permit.permitted.token).toBe(process.env.NFT_CONTRACT_ADDRESS);
// expect(result.permit.permitted.amount).toBe("1");
// expect(result.nftMetadata).toBeDefined();
// expect(result.request.beneficiary).toBe(context.config.spender);
// expect(result.request.deadline).toBe(BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").toString());
// expect(result.request.nonce).toBe(BigInt(keccak256(toUtf8Bytes(`${userId}-${issueId}`))).toString());
// expect(result.request.values).toEqual([organizationName, repositoryName, issueNumber, username, contributionType]);
// expect(result.networkId).toBe(context.config.evmNetworkId);
// const keysHashed = keys.map((key) => keccak256(toUtf8Bytes(key)));
// expect(result.request.keys).toEqual(keysHashed);
// }

// expect(context.logger.error).not.toHaveBeenCalled();
it("should generate ERC721 permit signature", async () => {
const issueId = 123;
const contributionType = "contribution";
const username = "tester";

const result = await generateErc721PermitSignature(context, username, contributionType);

const organizationName = "test";
const repositoryName = "test";
const issueNumber = issueId.toString();
const keys = ["GITHUB_ORGANIZATION_NAME", "GITHUB_REPOSITORY_NAME", "GITHUB_ISSUE_ID", "GITHUB_USERNAME", "GITHUB_CONTRIBUTION_TYPE"];

if (result && typeof result === "object") {
expect(result).toBeDefined();
expect(result.tokenType).toBe("erc721");
expect(result.tokenAddress).toBe(process.env.NFT_CONTRACT_ADDRESS);
expect(result.amount).toBe("1");
expect(result.erc721Request?.metadata).toBeDefined();
expect(result.beneficiary).toBe(SPENDER);
expect(result.deadline).toBe(MaxUint256.toString());
expect(result.nonce).toBe(BigInt(keccak256(toUtf8Bytes(`${userId}-${issueId}`))).toString());
expect(result.erc721Request?.values).toEqual([organizationName, repositoryName, issueNumber, username, contributionType]);
expect(result.networkId).toBe(context.config.evmNetworkId);
const keysHashed = keys.map((key) => keccak256(toUtf8Bytes(key)));
expect(result.erc721Request?.keys).toEqual(keysHashed);
}

expect(context.logger.error).not.toHaveBeenCalled();
});

it("should throw an error if RPC is not defined", async () => {
Expand All @@ -100,14 +103,14 @@ describe.skip("generateErc721PermitSignature", () => {

it("should throw an error if NFT minter private key is not defined", async () => {
delete process.env.NFT_MINTER_PRIVATE_KEY;
await expect(generateErc721PermitSignature(context, "tester", "contribution")).rejects.toThrow("NFT minter private key is not defined");
expect(context.logger.error).toHaveBeenCalledWith("NFT minter private key is not defined");
await expect(generateErc721PermitSignature(context, "tester", "contribution")).rejects.toThrow("Failed to instantiate wallet");
expect(context.logger.error).toHaveBeenCalled();
});

it("should throw an error if NFT contract address is not defined", async () => {
delete process.env.NFT_CONTRACT_ADDRESS;
await expect(generateErc721PermitSignature(context, "tester", "contribution")).rejects.toThrow("NFT contract address is not defined");
expect(context.logger.error).toHaveBeenCalledWith("NFT contract address is not defined");
expect(context.logger.error).toHaveBeenCalled();
});

it("should throw an error if no wallet found for user", async () => {
Expand Down

0 comments on commit 414f29f

Please sign in to comment.