-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
environment.ts
76 lines (72 loc) · 2.64 KB
/
environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { IAgentRuntime } from "@elizaos/core";
import { z } from "zod";
export const solanaEnvSchema = z
.object({
WALLET_SECRET_SALT: z.string().optional(),
})
.and(
z.union([
z.object({
WALLET_SECRET_KEY: z
.string()
.min(1, "Wallet secret key is required"),
WALLET_PUBLIC_KEY: z
.string()
.min(1, "Wallet public key is required"),
}),
z.object({
WALLET_SECRET_SALT: z
.string()
.min(1, "Wallet secret salt is required"),
}),
])
)
.and(
z.object({
SOL_ADDRESS: z.string().min(1, "SOL address is required"),
SLIPPAGE: z.string().min(1, "Slippage is required"),
RPC_URL: z.string().min(1, "RPC URL is required"),
HELIUS_API_KEY: z.string().min(1, "Helius API key is required"),
BIRDEYE_API_KEY: z.string().min(1, "Birdeye API key is required"),
})
);
export type SolanaConfig = z.infer<typeof solanaEnvSchema>;
export async function validateSolanaConfig(
runtime: IAgentRuntime
): Promise<SolanaConfig> {
try {
const config = {
WALLET_SECRET_SALT:
runtime.getSetting("WALLET_SECRET_SALT") ||
process.env.WALLET_SECRET_SALT,
WALLET_SECRET_KEY:
runtime.getSetting("WALLET_SECRET_KEY") ||
process.env.WALLET_SECRET_KEY,
WALLET_PUBLIC_KEY:
runtime.getSetting("SOLANA_PUBLIC_KEY") ||
runtime.getSetting("WALLET_PUBLIC_KEY") ||
process.env.WALLET_PUBLIC_KEY,
SOL_ADDRESS:
runtime.getSetting("SOL_ADDRESS") || process.env.SOL_ADDRESS,
SLIPPAGE: runtime.getSetting("SLIPPAGE") || process.env.SLIPPAGE,
RPC_URL: runtime.getSetting("RPC_URL") || process.env.RPC_URL,
HELIUS_API_KEY:
runtime.getSetting("HELIUS_API_KEY") ||
process.env.HELIUS_API_KEY,
BIRDEYE_API_KEY:
runtime.getSetting("BIRDEYE_API_KEY") ||
process.env.BIRDEYE_API_KEY,
};
return solanaEnvSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
.map((err) => `${err.path.join(".")}: ${err.message}`)
.join("\n");
throw new Error(
`Solana configuration validation failed:\n${errorMessages}`
);
}
throw error;
}
}