Skip to content

Commit

Permalink
chore: change prototype overload for permit generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Apr 10, 2024
1 parent d75081f commit 2d16e85
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
60 changes: 25 additions & 35 deletions src/handlers/generate-erc20-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,42 @@ import { Permit, TokenType } from "../types";
import { decryptKeys } from "../utils/keys";
import { getPayoutConfigByNetworkId } from "../utils/payoutConfigByNetworkId";

export async function generateErc20PermitSignature(
userId: number,
amount: number,
evmNetworkId: number,
evmPrivateEncrypted: string,
walletAddress: string,
issueId: number,
logger: Logger
): Promise<Permit>;
export async function generateErc20PermitSignature(userId: number, amount: number, context: Context): Promise<Permit>;
export async function generateErc20PermitSignature(
userId: number,
amount: number,
contextOrNetworkId: Context | number,
evmPrivateEncrypted?: string,
walletAddress?: string,
issueId?: number,
logger?: Logger
): Promise<Permit> {
export interface Payload {
evmNetworkId: number;
evmPrivateEncrypted: string;
walletAddress: string;
issueId: number;
logger: Logger;
}

export async function generateErc20PermitSignature(payload: Payload, userId: number, amount: number): Promise<Permit>;
export async function generateErc20PermitSignature(context: Context, userId: number, amount: number): Promise<Permit>;
export async function generateErc20PermitSignature(contextOrPayload: Context | Payload, userId: number, amount: number): Promise<Permit> {
let _logger: Logger;
let _userId: number;
const _userId = userId;
let _walletAddress: string | null | undefined;
let _issueId: number;
let _evmNetworkId: number;
let _evmPrivateEncrypted: string;

if (typeof contextOrNetworkId === "number") {
_logger = logger as Logger;
_userId = userId as number;
_walletAddress = walletAddress as string;
_evmNetworkId = contextOrNetworkId;
_evmPrivateEncrypted = evmPrivateEncrypted as string;
_issueId = issueId as number;
if ("issueId" in contextOrPayload) {
_logger = contextOrPayload.logger as Logger;
_walletAddress = contextOrPayload.walletAddress;
_evmNetworkId = contextOrPayload.evmNetworkId;
_evmPrivateEncrypted = contextOrPayload.evmPrivateEncrypted;
_issueId = contextOrPayload.issueId;
} else {
const config = contextOrNetworkId.config;
_logger = contextOrNetworkId.logger;
const config = contextOrPayload.config;
_logger = contextOrPayload.logger;
const { evmNetworkId, evmPrivateEncrypted } = config;
const { wallet } = contextOrNetworkId.adapters.supabase;
_userId = userId;
const { wallet } = contextOrPayload.adapters.supabase;
_walletAddress = await wallet.getWalletByUserId(_userId);
_evmNetworkId = evmNetworkId;
_evmPrivateEncrypted = evmPrivateEncrypted;
if ("issue" in contextOrNetworkId.payload) {
_issueId = contextOrNetworkId.payload.issue.id;
} else if ("pull_request" in contextOrNetworkId.payload) {
_issueId = contextOrNetworkId.payload.pull_request.id;
if ("issue" in contextOrPayload.payload) {
_issueId = contextOrPayload.payload.issue.id;
} else if ("pull_request" in contextOrPayload.payload) {
_issueId = contextOrPayload.payload.pull_request.id;
} else {
throw new Error("Issue Id is missing");
}
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export interface PermitPayload {
userName: string;
}

export async function generateErc721PermitSignature(userId: number, contributionType: string, permitPayload: PermitPayload): Promise<Permit>;
export async function generateErc721PermitSignature(userId: number, contributionType: string, context: Context): Promise<Permit>;
export async function generateErc721PermitSignature(permitPayload: PermitPayload, userId: number, contributionType: string): Promise<Permit>;
export async function generateErc721PermitSignature(context: Context, userId: number, contributionType: string): Promise<Permit>;
export async function generateErc721PermitSignature(
contextOrPermitPayload: Context | PermitPayload,
userId: number,
contributionType: string,
contextOrPermitPayload: Context | PermitPayload
contributionType: string
): Promise<Permit> {
let _logger: Logger;
let _nftContractAddress: string;
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/generate-payout-permit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Permit } from "../types/permits";
import { Permit } from "../types";
import { Context } from "../types/context";
import { generateErc20PermitSignature } from "./generate-erc20-permit";
import { generateErc721PermitSignature } from "./generate-erc721-permit";
Expand All @@ -19,10 +19,10 @@ export async function generatePayoutPermit(context: Context, permitRequests: Per
let permit: Permit;
switch (type) {
case "ERC20":
permit = await generateErc20PermitSignature(userId, amount, context);
permit = await generateErc20PermitSignature(context, userId, amount);
break;
case "ERC721":
permit = await generateErc721PermitSignature(userId, contributionType, context);
permit = await generateErc721PermitSignature(context, userId, contributionType);
break;
default:
context.logger.error(`Invalid permit type: ${type}`);
Expand Down
6 changes: 3 additions & 3 deletions tests/generate-erc20-permit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("generateErc20PermitSignature", () => {

context.config.evmPrivateEncrypted = cypherText;

const result = await generateErc20PermitSignature(SPENDER, amount, context);
const result = await generateErc20PermitSignature(context, SPENDER, amount);

expect(result).toBeDefined();
expect(result).not.toContain("Permit not generated");
Expand All @@ -69,7 +69,7 @@ describe("generateErc20PermitSignature", () => {
it("should throw error when evmPrivateEncrypted is not defined", async () => {
const amount = 0;

await expect(generateErc20PermitSignature(SPENDER, amount, context)).rejects.toThrow("Private key is not" + " defined");
await expect(generateErc20PermitSignature(context, SPENDER, amount)).rejects.toThrow("Private key is not" + " defined");
expect(context.logger.fatal).toHaveBeenCalledWith("Private key is not defined");
});

Expand All @@ -80,7 +80,7 @@ describe("generateErc20PermitSignature", () => {
(context.adapters.supabase.wallet.getWalletByUserId as jest.Mock).mockReturnValue(null);

await expect(async () => {
await generateErc20PermitSignature(SPENDER, amount, context);
await generateErc20PermitSignature(context, SPENDER, amount);
}).rejects.toThrow();

expect(context.logger.error).toHaveBeenCalledWith("ERC20 Permit generation error: Wallet not found");
Expand Down
10 changes: 5 additions & 5 deletions tests/generate-erc721-permit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("generateErc721PermitSignature", () => {
const contributionType = "contribution";
const userId = 123;

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

const organizationName = "test";
const repositoryName = "test";
Expand All @@ -98,18 +98,18 @@ describe("generateErc721PermitSignature", () => {

it("should throw an error if RPC is not defined", async () => {
context.config.evmNetworkId = 123;
await expect(generateErc721PermitSignature(123, "contribution", context)).rejects.toThrow("No config setup for evmNetworkId: 123");
await expect(generateErc721PermitSignature(context, 123, "contribution")).rejects.toThrow("No config" + " setup for evmNetworkId: 123");
});

it("should throw an error if NFT minter private key is not defined", async () => {
delete process.env.NFT_MINTER_PRIVATE_KEY;
await expect(generateErc721PermitSignature(123, "contribution", context)).rejects.toThrow("Failed to instantiate wallet");
await expect(generateErc721PermitSignature(context, 123, "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(123, "contribution", context)).rejects.toThrow("NFT contract address is not defined");
await expect(generateErc721PermitSignature(context, 123, "contribution")).rejects.toThrow("NFT contract" + " address" + " is not defined");
expect(context.logger.error).toHaveBeenCalled();
});

Expand All @@ -121,7 +121,7 @@ describe("generateErc721PermitSignature", () => {

(context.adapters.supabase.user.getUserIdByWallet as jest.Mock).mockReturnValue(null);

await expect(generateErc721PermitSignature(123, "contribution", context)).rejects.toThrow("No wallet found for user");
await expect(generateErc721PermitSignature(context, 123, "contribution")).rejects.toThrow("No wallet" + " found" + " for" + " user");
expect(context.logger.error).toHaveBeenCalledWith("No wallet found for user");
});
});

0 comments on commit 2d16e85

Please sign in to comment.