Files
smart-updown-token/relayer/src/config.ts
T

66 lines
2.2 KiB
TypeScript

import { Connection, clusterApiUrl, Keypair, PublicKey } from "@solana/web3.js";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
export const SIDE_UP = 0;
export const SIDE_DOWN = 1;
/**
* Runtime configuration. Everything can be overridden via environment
* variables; defaults target the local validator.
*/
export interface Config {
/** RPC endpoint of the cluster (localnet by default). */
rpcUrl: string;
/** Localnet allows naive airdrops (to bettors and the house vault). */
allowAirdrop: boolean;
/** Program id. Defaults to the value embedded in the IDL. */
programId: PublicKey;
/** House admin/relayer keypair file path. */
adminKeypairPath: string;
/** Initial parameters used by `init` (must match on-chain constants). */
minAmountLamports: number;
maxAmountLamports: number;
multiplierBps: number;
expirySeconds: number;
}
function env(name: string, fallback: string): string {
return process.env[name] ?? fallback;
}
function envNum(name: string, fallback: number): number {
const raw = process.env[name];
if (!raw) return fallback;
const n = Number(raw);
if (!Number.isFinite(n)) throw new Error(`Invalid number for env ${name}: ${raw}`);
return n;
}
export function loadConfig(): Config {
const rpcUrl = env("SMART_RPC_URL", "http://127.0.0.1:8899");
const allowAirdrop = env("SMART_ALLOW_AIRDROP", "1") === "1";
const adminKeypairPath =
env("SMART_ADMIN_KEYPAIR", path.join(os.homedir(), ".config/solana/id.json"));
return {
rpcUrl,
allowAirdrop,
programId: PublicKey.default, // placeholder; real value read from the IDL below
adminKeypairPath,
minAmountLamports: envNum("SMART_MIN_AMOUNT", 100_000_000), // 0.1 SOL
maxAmountLamports: envNum("SMART_MAX_AMOUNT", 10_000_000_000), // 10 SOL
multiplierBps: envNum("SMART_MULTIPLIER_BPS", 13_000), // 1.3x
expirySeconds: envNum("SMART_EXPIRY_SECONDS", 15),
};
}
export function loadKeypair(pathOrJson: string): Keypair {
const resolved = pathOrJson.startsWith("~")
? path.join(os.homedir(), pathOrJson.slice(1))
: path.resolve(pathOrJson);
const raw = fs.readFileSync(resolved, "utf8");
return Keypair.fromSecretKey(Uint8Array.from(JSON.parse(raw)));
}