Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TypeBox for Environment Variable Management #95

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MaxUint256 } from "@uniswap/permit2-sdk";
import { Wallet, utils } from "ethers";
import { Context, Logger } from "../types/context";
import { PermitReward, TokenType } from "../types";
import { Env } from "../types/env";
import { isIssueEvent } from "../types/typeguards";
import { getFastestProvider } from "../utils/get-fastest-provider";

Expand All @@ -26,10 +27,10 @@ const types = {
],
};

export interface PermitPayload {
export interface PermitPayload {
evmNetworkId: number;
nftMinterPrivateKey: string;
nftContractAddress: string;
nftMinterPrivateKey: string; // Consider making this optional if needed
nftContractAddress: string; // Consider making this optional if needed
walletAddress: string;
logger: Logger;
issueNodeId: string;
Expand All @@ -48,7 +49,7 @@ export async function generateErc721PermitSignature(
let _logger: Logger;
let _nftContractAddress: string;
let _evmNetworkId: number;
let _nftMinterPrivateKey: string;
let _nftMinterPrivateKey: string; // This may need to be optional
let _userId: number;
let _walletAddress: string;
let _issueNodeId: string;
Expand All @@ -57,42 +58,71 @@ export async function generateErc721PermitSignature(
let _username = username;

if ("evmNetworkId" in contextOrPermitPayload) {
// Existing logic for PermitPayload
_logger = contextOrPermitPayload.logger;
_nftContractAddress = contextOrPermitPayload.nftContractAddress;
_nftMinterPrivateKey = contextOrPermitPayload.nftMinterPrivateKey;
_nftMinterPrivateKey = contextOrPermitPayload.nftMinterPrivateKey; // This may need to be optional
_evmNetworkId = contextOrPermitPayload.evmNetworkId;
_walletAddress = contextOrPermitPayload.walletAddress;
_issueNodeId = contextOrPermitPayload.issueNodeId;
_organizationName = contextOrPermitPayload.organizationName;
_repositoryName = contextOrPermitPayload.repositoryName;
_userId = contextOrPermitPayload.userId;

} else {
const { NFT_MINTER_PRIVATE_KEY, NFT_CONTRACT_ADDRESS } = contextOrPermitPayload.env;
const { evmNetworkId } = contextOrPermitPayload.config;
const adapters = contextOrPermitPayload.adapters;
const { NFT_MINTER_PRIVATE_KEY, NFT_CONTRACT_ADDRESS } = contextOrPermitPayload.env as Env; // Cast to Env

const { evmNetworkId } = contextOrPermitPayload.config;
const adapters = contextOrPermitPayload.adapters;
_logger = contextOrPermitPayload.logger;
_nftContractAddress = NFT_CONTRACT_ADDRESS;
_evmNetworkId = evmNetworkId;
_nftMinterPrivateKey = NFT_MINTER_PRIVATE_KEY;

// Check if NFT_CONTRACT_ADDRESS is defined
if (!NFT_CONTRACT_ADDRESS) {
const errorMessage = "NFT contract address is not defined in environment variables.";
_logger.error(errorMessage);
throw new Error(errorMessage);
}

// Check if NFT_MINTER_PRIVATE_KEY is defined
if (!NFT_MINTER_PRIVATE_KEY) {
const errorMessage = "NFT minter private key is not defined in environment variables.";
_logger.error(errorMessage);
throw new Error(errorMessage);
}

// Assign values from environment variables
_nftContractAddress = NFT_CONTRACT_ADDRESS;
_evmNetworkId = evmNetworkId;
_nftMinterPrivateKey = NFT_MINTER_PRIVATE_KEY;
_username = username;

if (isIssueEvent(contextOrPermitPayload)) {
_issueNodeId = contextOrPermitPayload.payload.issue.node_id;
_issueNodeId = contextOrPermitPayload.payload.issue.node_id;
} else {
throw new Error("Issue Id is missing.");
}
_organizationName = contextOrPermitPayload.payload.repository.owner.login;
_repositoryName = contextOrPermitPayload.payload.repository.name;

// Fetch organization and repository names
_organizationName = contextOrPermitPayload.payload.repository.owner.login;
_repositoryName = contextOrPermitPayload.payload.repository.name;

// Fetch user data
const { data: userData } = await contextOrPermitPayload.octokit.users.getByUsername({ username: _username });

if (!userData) {
throw new Error(`GitHub user was not found for id ${_username}`);
}
_userId = userData.id;
const walletAddress = await adapters.supabase.wallet.getWalletByUserId(_userId);

// Fetch wallet address by user ID
_userId = userData.id;
const walletAddress = await adapters.supabase.wallet.getWalletByUserId(_userId);

if (!walletAddress) {
_logger.error("No wallet found for user");
throw new Error("No wallet found for user");
}
_walletAddress = walletAddress;

_walletAddress = walletAddress;
}

const provider = await getFastestProvider(_evmNetworkId);
Expand Down
12 changes: 7 additions & 5 deletions src/types/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Type as T } from "@sinclair/typebox";
import { StaticDecode } from "@sinclair/typebox";
import "dotenv/config";

// Define the environment schema with optional fields
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(),
GITHUB_TOKEN: T.String(), // Required
SUPABASE_URL: T.String(), // Required
SUPABASE_KEY: T.String(), // Required
NFT_MINTER_PRIVATE_KEY: T.Optional(T.String()), // Optional field
NFT_CONTRACT_ADDRESS: T.Optional(T.String()), // Optional field
});

// Create a type for static decoding of the schema
export type Env = StaticDecode<typeof envSchema>;
Loading