-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.ts
More file actions
188 lines (169 loc) · 6.51 KB
/
Copy pathconfig.ts
File metadata and controls
188 lines (169 loc) · 6.51 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Shared configuration for examples
*
* Loads configuration from environment variables.
* Copy .env.example to .env and fill in your values.
*/
import { config } from "dotenv";
import { Connection, PublicKey, Keypair } from "@solana/web3.js";
import path from "path";
import { fileURLToPath } from "url";
import {
SwapProvider,
type SwapProviderConfig,
type SwapProviderEntry,
} from "../src";
// Get __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load .env file from examples directory
config({ path: path.join(__dirname, ".env") });
/**
* Get required environment variable or throw error
*/
function getEnvVar(key: string, required: boolean = true): string {
const value = process.env[key];
if (!value && required) {
throw new Error(
`Missing required environment variable: ${key}\n` +
`Please copy .env.example to .env and fill in your values.`
);
}
return value || "";
}
/**
* Solana RPC connection
*/
export function getConnection(): Connection {
const rpcUrl = getEnvVar("SOLANA_RPC_URL");
const commitment = getEnvVar("SOLANA_COMMITMENT", false) || "confirmed";
return new Connection(rpcUrl, commitment as any);
}
/**
* Marginfi configuration
*/
export function getMarginfiConfig() {
return {
environment: getEnvVar("MARGINFI_ENVIRONMENT") as any,
groupPk: new PublicKey(getEnvVar("MARGINFI_GROUP_ADDRESS")),
programId: new PublicKey(getEnvVar("MARGINFI_PROGRAM_ID")),
};
}
/**
* User's marginfi account address
*/
export function getAccountAddress(): PublicKey {
return new PublicKey(getEnvVar("MARGINFI_ACCOUNT_ADDRESS"));
}
/**
* User's wallet public key (for simulation/read-only operations)
*/
export function getWalletPubkey(): PublicKey {
const pubkeyStr = getEnvVar("WALLET_ADDRESS");
try {
return new PublicKey(pubkeyStr);
} catch (error) {
throw new Error("Invalid WALLET_ADDRESS format. Expected base58 public key.");
}
}
/**
* User's wallet keypair (optional - only needed for actual transaction signing)
*/
export function getWallet(): Keypair | null {
const privateKeyStr = getEnvVar("WALLET_PRIVATE_KEY", false);
if (!privateKeyStr) {
return null;
}
try {
const privateKey = JSON.parse(privateKeyStr);
return Keypair.fromSecretKey(Buffer.from(privateKey));
} catch (error) {
throw new Error("Invalid WALLET_PRIVATE_KEY format. Expected JSON array: [1,2,3,...]");
}
}
/**
* Common mint addresses
*/
export const MINTS = {
USDC: new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
USDT: new PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),
SOL: new PublicKey("So11111111111111111111111111111111111111112"),
JITOSOL: new PublicKey("J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn"),
MSOL: new PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),
BSOL: new PublicKey("bSo13r4TkiE4KumL71LsHTPpL2euBYLFx6h9HP3piy1"),
};
/** Universal bridge mints for double-hop swaps (mirrors the app's UNIVERSAL_BRIDGES). */
export const UNIVERSAL_BRIDGE_MINTS = [MINTS.USDC, MINTS.SOL, MINTS.USDT, MINTS.JITOSOL];
/**
* Priority fee (optional)
*/
export function getPriorityFee(): number {
const fee = getEnvVar("PRIORITY_FEE", false);
return fee ? parseFloat(fee) : 0.00001;
}
/**
* Builds the multi-provider swap-engine config the SDK swap builders expect (`swapOpts.swapConfig`),
* mirroring the P0 app's default: TITAN primary with a JUPITER fallback. The builders run
* `runSwapEngine` in-process with `provider` + `fallbackProviders` as co-equal candidates and pick
* the best route that fits the flashloan transaction. Only providers you supply credentials for are
* queried; with no Titan creds it falls back to Jupiter only.
*
* Env vars:
* TITAN_GATEWAY_URL REST gateway base, e.g. https://<host>/api/v1
* TITAN_API_ENDPOINT alternative to TITAN_GATEWAY_URL — a bare host is normalized to
* https://<host>/api/v1 (matches the app)
* TITAN_WS_URL Titan V3 quotes over WS, e.g. wss://<host>/api/v1/ws — derived from the
* base when omitted
* TITAN_API_KEY Titan JWT (used for REST + WS)
* JUPITER_API_KEY omit to use the public, rate-limited endpoint
* JUPITER_BASE_PATH default https://api.jup.ag/swap/v1
* SWAP_SLIPPAGE_MODE "DYNAMIC" | "FIXED" (default DYNAMIC)
* SWAP_SLIPPAGE_BPS default 50
* SWAP_PLATFORM_FEE_BPS default 0
* SWAP_DIRECT_ROUTES_ONLY "true" | "false" (default false)
*/
export function getSwapConfig(): SwapProviderConfig {
const slippageMode = (getEnvVar("SWAP_SLIPPAGE_MODE", false) || "DYNAMIC") as
| "DYNAMIC"
| "FIXED";
const slippageBps = Number(getEnvVar("SWAP_SLIPPAGE_BPS", false) || "50");
const platformFeeBps = Number(getEnvVar("SWAP_PLATFORM_FEE_BPS", false) || "0");
const directRoutesOnly = (getEnvVar("SWAP_DIRECT_ROUTES_ONLY", false) || "false") === "true";
const common = { slippageMode, slippageBps, platformFeeBps, directRoutesOnly };
const jupiterEntry: SwapProviderEntry = {
provider: SwapProvider.JUPITER,
apiConfig: {
basePath: getEnvVar("JUPITER_BASE_PATH", false) || "https://api.jup.ag/swap/v1",
apiKey: getEnvVar("JUPITER_API_KEY", false) || undefined,
},
};
// TITAN primary + JUPITER fallback (the app default) when Titan creds are present. Accept either
// a full gateway URL or a bare host (TITAN_API_ENDPOINT), normalized like the app.
const titanRaw = getEnvVar("TITAN_GATEWAY_URL", false) || getEnvVar("TITAN_API_ENDPOINT", false);
if (titanRaw) {
const basePath = normalizeTitanBase(titanRaw);
return {
provider: SwapProvider.TITAN,
...common,
apiConfig: {
basePath,
wsUrl: getEnvVar("TITAN_WS_URL", false) || deriveTitanWs(basePath),
apiKey: getEnvVar("TITAN_API_KEY", false) || undefined,
},
fallbackProviders: [jupiterEntry],
};
}
// Otherwise Jupiter only.
return { provider: SwapProvider.JUPITER, ...common, apiConfig: jupiterEntry.apiConfig };
}
/** Normalize a Titan gateway value (full URL or bare host) to `https://<host>/api/v1`. */
function normalizeTitanBase(raw: string): string {
let s = raw.trim().replace(/\/+$/, "");
if (!/^https?:\/\//.test(s)) s = `https://${s}`;
if (!s.endsWith("/api/v1")) s = `${s}/api/v1`;
return s;
}
/** Derive the Titan V3 WS endpoint from the REST base: `https://h/api/v1` → `wss://h/api/v1/ws`. */
function deriveTitanWs(base: string): string {
return `${base.replace(/^http/, "ws")}/ws`;
}