Skip to content

Commit

Permalink
chore: add env config
Browse files Browse the repository at this point in the history
  • Loading branch information
bullishgopher committed Nov 14, 2023
1 parent e6784ac commit 2a4ebae
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build/src/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions build/src/services/alchemy-service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/services/alchemy-service.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions build/src/services/discord-service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/services/discord-service.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions build/src/utils/env.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/src/utils/env.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/src/utils/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { discordEvents } from "./services/discord-service.js";
import { discordEvents } from './services/discord-service.js';

/**
* Some predefined delay values (in milliseconds).
Expand Down Expand Up @@ -36,4 +36,4 @@ export async function greeter(name: any) {
return await delayedHello(name, Delays.Short);
}

discordEvents()
discordEvents();
5 changes: 3 additions & 2 deletions src/services/alchemy-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Network, Alchemy, Utils } from 'alchemy-sdk';
import { Client } from 'discord.js';
import { shortenAddress } from '../utils/index.js';
import { config } from '../utils/env.js';

const settings = {
apiKey: process.env.ALCHEMY_API_KEY,
apiKey: config.alchemy.apiKey,
network: Network.ETH_SEPOLIA,
};

const alchemy = new Alchemy(settings);

const DISCORD_CHANNEL_ID = process.env.DISCORD_CHANNEL_ID;
const DISCORD_CHANNEL_ID = config.discord.channelId;

export async function getBalance(address: string) {
return alchemy.core.getBalance(address, 'latest');
Expand Down
11 changes: 5 additions & 6 deletions src/services/discord-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dotenv from 'dotenv';
import { Utils } from 'alchemy-sdk';
import { Client, Events, GatewayIntentBits } from 'discord.js';
import {
Expand All @@ -7,8 +6,7 @@ import {
trackAddress,
} from './alchemy-service.js';
import { shortenAddress } from '../utils/index.js';

dotenv.config();
import { config } from '../utils/env.js';

const client = new Client({
intents: [
Expand All @@ -19,8 +17,8 @@ const client = new Client({
],
});

const DISCORD_BOT_TOKEN = process.env.DISCORD_BOT_TOKEN;
const DISCORD_CHANNEL_ID = process.env.DISCORD_CHANNEL_ID;
const DISCORD_BOT_TOKEN = config.discord.botToken;
const DISCORD_CHANNEL_ID = config.discord.channelId;

let trackedAddress = '';

Expand Down Expand Up @@ -58,7 +56,7 @@ export async function discordEvents() {
message.channel.send(`🔎 Now tracking address: ${trackedAddress}`);
console.log(`Tracking address: ${trackedAddress}`);

trackAddress(address, client)
trackAddress(address, client);
} else if (command === '!balance') {
const address = args[0];
if (!address) {
Expand All @@ -68,6 +66,7 @@ export async function discordEvents() {
return;
}
try {
message.channel.send(':hourglass:');
const balance = await getBalance(address);
message.channel.send(
`💰 Balance for address ${shortenAddress(
Expand Down
40 changes: 40 additions & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import dotenv from 'dotenv';

dotenv.config();

export interface DiscordConfig {
botToken: string;
channelId: string;
}

export interface AlchemyConfig {
apiKey: string;
}

export interface IConfig {
discord: DiscordConfig;
alchemy: AlchemyConfig;
}

const getEnv = (key: string, defaultValue?: string): string => {
const value = process.env[key];
if (!value) {
if (!defaultValue) {
throw new Error(
`No environment variable for ${key} - doesn't have a default fallback value`,
);
}
return defaultValue;
}
return value;
};

export const config: IConfig = {
discord: {
botToken: getEnv('DISCORD_BOT_TOKEN', ''),
channelId: getEnv('DISCORD_CHANNEL_ID', ''),
},
alchemy: {
apiKey: getEnv('ALCHEMY_API_KEY', ''),
},
};
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function shortenAddress(address: string) {
if (!address) return "";
return address.slice(0, 6) + "..." + address.slice(-4);
if (!address) return '';
return address.slice(0, 6) + '...' + address.slice(-4);
}

0 comments on commit 2a4ebae

Please sign in to comment.