Skip to content

Commit

Permalink
refactor: simplify the env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hhio618 committed Oct 16, 2024
1 parent 5b20c35 commit c22e4c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
52 changes: 24 additions & 28 deletions scripts/github-action-permit-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,30 @@ import { Database } from "../src/adapters/supabase/types/database";
import { generatePayoutPermit } from "../src/handlers";
import { Context } from "../src/types/context";
import { PermitGenerationSettings, PermitRequest } from "../src/types/plugin-input";
import { Value } from "@sinclair/typebox/value";
import { envSchema } from "../src/types/env";
import * as fs from "fs";

function getEnvVar(key: string) {
return (
process.env[key] ||
(() => {
throw new Error(`Environment variable ${key} is required`);
})()
);
}
/**
* Generates all the permits based on the current github workflow dispatch.
*/
export async function generatePermitsFromGithubWorkflowDispatch() {
// These are necessary to ensure the type checks and tests pass.
process.env["NFT_MINTER_PRIVATE_KEY"] = "";
process.env["NFT_CONTRACT_ADDRESS"] = "";
const env = Value.Decode(envSchema, process.env);

if (!env.EVM_NETWORK_ID) {
throw new Error("EVM_NETWORK_ID env not provided or empty");
}
if (!env.EVM_PRIVATE_KEY) {
throw new Error("EVM_PRIVATE_KEY env not provided or empty");
}
if (!env.EVM_TOKEN_ADDRESS) {
throw new Error("EVM_TOKEN_ADDRESS env not provided or empty");
}
if (!env.PAYMENT_REQUESTS) {
throw new Error("PAYMENT_REQUESTS env not provided or empty");
}
const EVM_NETWORK_ID = getEnvVar("EVM_NETWORK_ID");
const EVM_PRIVATE_KEY = getEnvVar("EVM_PRIVATE_KEY");
const EVM_TOKEN_ADDRESS = getEnvVar("EVM_TOKEN_ADDRESS");
const PAYMENT_REQUESTS = getEnvVar("PAYMENT_REQUESTS");
const GITHUB_TOKEN = getEnvVar("GITHUB_TOKEN");
const SUPABASE_URL = getEnvVar("SUPABASE_URL");
const SUPABASE_KEY = getEnvVar("SUPABASE_KEY");

console.log(`Received: ${env.PAYMENT_REQUESTS}`);
const userAmounts = JSON.parse(env.PAYMENT_REQUESTS);
console.log(`Received: ${PAYMENT_REQUESTS}`);
const userAmounts = JSON.parse(PAYMENT_REQUESTS);

// Populate the permitRequests from the user_amounts payload

Expand All @@ -42,25 +38,25 @@ export async function generatePermitsFromGithubWorkflowDispatch() {
type: "ERC20",
username: user,
amount: amount,
tokenAddress: env.EVM_TOKEN_ADDRESS,
tokenAddress: EVM_TOKEN_ADDRESS,
}))
);

const config: PermitGenerationSettings = {
evmNetworkId: Number(env.EVM_NETWORK_ID),
evmPrivateEncrypted: env.EVM_PRIVATE_KEY,
evmNetworkId: Number(EVM_NETWORK_ID),
evmPrivateEncrypted: EVM_PRIVATE_KEY,
permitRequests: permitRequests,
};

const octokit = new Octokit({ auth: env.GITHUB_TOKEN });
const supabaseClient = createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY);
const octokit = new Octokit({ auth: GITHUB_TOKEN });
const supabaseClient = createClient<Database>(SUPABASE_URL, SUPABASE_KEY);

const context: Context = {
eventName: "workflow_dispatch",
config: config,
octokit,
payload: userAmounts,
env,
env: undefined,
logger: {
debug(message: unknown, ...optionalParams: unknown[]) {
console.debug(message, ...optionalParams);
Expand All @@ -84,7 +80,7 @@ export async function generatePermitsFromGithubWorkflowDispatch() {
context.adapters = createAdapters(supabaseClient, context);

const permits = await generatePayoutPermit(context, config.permitRequests);
await returnDataToKernel(env.GITHUB_TOKEN, "todo_state", permits);
await returnDataToKernel(GITHUB_TOKEN, "todo_state", permits);
const out = Buffer.from(JSON.stringify(permits)).toString("base64");
fs.writeFile(process.argv[2], out, (err) => {
if (err) {
Expand Down
3 changes: 3 additions & 0 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export async function generateErc721PermitSignature(
_repositoryName = contextOrPermitPayload.repositoryName;
_userId = contextOrPermitPayload.userId;
} else {
if (!contextOrPermitPayload.env) {
throw new Error(`env is undefined`);
}
const { NFT_MINTER_PRIVATE_KEY, NFT_CONTRACT_ADDRESS } = contextOrPermitPayload.env;
const { evmNetworkId } = contextOrPermitPayload.config;
const adapters = contextOrPermitPayload.adapters;
Expand Down
2 changes: 1 addition & 1 deletion src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export interface Context<T extends WebhookEventName = SupportedEvents> {
octokit: InstanceType<typeof Octokit>;
adapters: ReturnType<typeof createAdapters>;
config: PermitGenerationSettings;
env: Env;
env?: Env;
logger: Logger;
}
4 changes: 0 additions & 4 deletions src/types/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export const envSchema = T.Object({
SUPABASE_KEY: T.String(),
NFT_MINTER_PRIVATE_KEY: T.String(),
NFT_CONTRACT_ADDRESS: T.String(),
EVM_NETWORK_ID: T.Optional(T.String()),
EVM_PRIVATE_KEY: T.Optional(T.String()),
EVM_TOKEN_ADDRESS: T.Optional(T.String()),
PAYMENT_REQUESTS: T.Optional(T.String()),
});

export type Env = StaticDecode<typeof envSchema>;

0 comments on commit c22e4c2

Please sign in to comment.