From eebe1a2729394a3a4a77331d77f36d3a50b318c8 Mon Sep 17 00:00:00 2001 From: luisantoniocrag Date: Sat, 19 Oct 2024 15:54:28 -0600 Subject: [PATCH 1/4] refactor: optional env variables --- src/types/env.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types/env.ts b/src/types/env.ts index a816449..8994469 100644 --- a/src/types/env.ts +++ b/src/types/env.ts @@ -6,8 +6,8 @@ export const envSchema = T.Object({ GITHUB_TOKEN: T.String(), SUPABASE_URL: T.String(), SUPABASE_KEY: T.String(), - NFT_MINTER_PRIVATE_KEY: T.String(), - NFT_CONTRACT_ADDRESS: T.String(), + NFT_MINTER_PRIVATE_KEY: T.Optional(T.String()), + NFT_CONTRACT_ADDRESS: T.Optional(T.String()), }); export type Env = StaticDecode; From e2937dd523e5b705427bec33cc31ded429d22edf Mon Sep 17 00:00:00 2001 From: luisantoniocrag Date: Sat, 19 Oct 2024 16:28:30 -0600 Subject: [PATCH 2/4] fix: validate required env varibles --- src/handlers/generate-erc721-permit.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/handlers/generate-erc721-permit.ts b/src/handlers/generate-erc721-permit.ts index 39ebced..70aed84 100644 --- a/src/handlers/generate-erc721-permit.ts +++ b/src/handlers/generate-erc721-permit.ts @@ -67,7 +67,8 @@ export async function generateErc721PermitSignature( _repositoryName = contextOrPermitPayload.repositoryName; _userId = contextOrPermitPayload.userId; } else { - const { NFT_MINTER_PRIVATE_KEY, NFT_CONTRACT_ADDRESS } = contextOrPermitPayload.env; + const { NFT_MINTER_PRIVATE_KEY = "", NFT_CONTRACT_ADDRESS = "" } = contextOrPermitPayload.env; + const { evmNetworkId } = contextOrPermitPayload.config; const adapters = contextOrPermitPayload.adapters; _logger = contextOrPermitPayload.logger; From 49c8a231e8570c03c3a4b5bf7640210428d8ab92 Mon Sep 17 00:00:00 2001 From: luisantoniocrag Date: Mon, 4 Nov 2024 16:55:14 -0600 Subject: [PATCH 3/4] feat: throw error when nft minter pk or contract addr is not defined --- src/handlers/generate-erc721-permit.ts | 21 +++++++++++++-------- tests/generate-erc721-permit.test.ts | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/handlers/generate-erc721-permit.ts b/src/handlers/generate-erc721-permit.ts index 70aed84..4df3174 100644 --- a/src/handlers/generate-erc721-permit.ts +++ b/src/handlers/generate-erc721-permit.ts @@ -67,11 +67,22 @@ export async function generateErc721PermitSignature( _repositoryName = contextOrPermitPayload.repositoryName; _userId = contextOrPermitPayload.userId; } else { - const { NFT_MINTER_PRIVATE_KEY = "", NFT_CONTRACT_ADDRESS = "" } = contextOrPermitPayload.env; + const { NFT_MINTER_PRIVATE_KEY, NFT_CONTRACT_ADDRESS } = contextOrPermitPayload.env; + + _logger = contextOrPermitPayload.logger; + + if (!NFT_MINTER_PRIVATE_KEY) { + _logger.error("NFT minter private key is not defined"); + throw new Error("NFT minter private key is not defined"); + } + + if (!NFT_CONTRACT_ADDRESS) { + _logger.error("NFT contract address is not defined"); + throw new Error("NFT contract address is not defined"); + } const { evmNetworkId } = contextOrPermitPayload.config; const adapters = contextOrPermitPayload.adapters; - _logger = contextOrPermitPayload.logger; _nftContractAddress = NFT_CONTRACT_ADDRESS; _evmNetworkId = evmNetworkId; _nftMinterPrivateKey = NFT_MINTER_PRIVATE_KEY; @@ -103,12 +114,6 @@ export async function generateErc721PermitSignature( throw new Error("Provider is not defined"); } - if (!_nftContractAddress) { - const errorMessage = "NFT contract address is not defined"; - _logger.error(errorMessage); - throw new Error(errorMessage); - } - let adminWallet; try { diff --git a/tests/generate-erc721-permit.test.ts b/tests/generate-erc721-permit.test.ts index b65da8c..86b2638 100644 --- a/tests/generate-erc721-permit.test.ts +++ b/tests/generate-erc721-permit.test.ts @@ -117,7 +117,7 @@ describe("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, "123", "contribution")).rejects.toThrow("Failed to" + " instantiate wallet"); + await expect(generateErc721PermitSignature(context, "123", "contribution")).rejects.toThrow("NFT minter" + " private key" + " is not defined"); expect(context.logger.error).toHaveBeenCalled(); }); From 9c3526e57388903e4d26bbbd112676b2b4bff374 Mon Sep 17 00:00:00 2001 From: luisantoniocrag Date: Tue, 5 Nov 2024 16:26:10 -0600 Subject: [PATCH 4/4] fix: avoid error strings duplication --- src/handlers/generate-erc721-permit.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/handlers/generate-erc721-permit.ts b/src/handlers/generate-erc721-permit.ts index 4df3174..569c273 100644 --- a/src/handlers/generate-erc721-permit.ts +++ b/src/handlers/generate-erc721-permit.ts @@ -72,13 +72,15 @@ export async function generateErc721PermitSignature( _logger = contextOrPermitPayload.logger; if (!NFT_MINTER_PRIVATE_KEY) { - _logger.error("NFT minter private key is not defined"); - throw new Error("NFT minter private key is not defined"); + const errorMessage = "NFT minter private key is not defined"; + _logger.error(errorMessage); + throw new Error(errorMessage); } if (!NFT_CONTRACT_ADDRESS) { - _logger.error("NFT contract address is not defined"); - throw new Error("NFT contract address is not defined"); + const errorMessage = "NFT contract address is not defined"; + _logger.error(errorMessage); + throw new Error(errorMessage); } const { evmNetworkId } = contextOrPermitPayload.config;