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 4b03f8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
50 changes: 23 additions & 27 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,18 +38,18 @@ 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",
Expand Down Expand Up @@ -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
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 4b03f8a

Please sign in to comment.