-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path05-oracle-prices.ts
More file actions
124 lines (105 loc) · 3.93 KB
/
Copy path05-oracle-prices.ts
File metadata and controls
124 lines (105 loc) · 3.93 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
/**
* Example: Fetch oracle prices
*
* This example shows how to:
* 1. Initialize the Project0Client from config
* 2. Access oracle prices for all banks
* 3. Crank/update oracle prices
*
* Setup:
* 1. Copy .env.example to .env
* 2. Fill in your configuration values
* 3. Run: tsx 05-oracle-prices.ts
*/
import { PublicKey } from "@solana/web3.js";
import { Project0Client, fetchOracleData } from "../src";
import { getConnection, getMarginfiConfig } from "./config";
// ============================================================================
// Main Example
// ============================================================================
async function oraclePricesExample() {
// --------------------------------------------------------------------------
// Step 1: Load Configuration
// --------------------------------------------------------------------------
console.log("\n🔧 Loading configuration...");
const connection = getConnection();
const config = getMarginfiConfig();
console.log(` RPC: ${connection.rpcEndpoint}`);
console.log(` Environment: ${config.environment}`);
// --------------------------------------------------------------------------
// Step 2: Initialize Client
// --------------------------------------------------------------------------
console.log("\n📡 Initializing Project0Client...");
const client = await Project0Client.initialize(connection, config);
console.log("✅ Client initialized with oracle prices");
console.log(`📊 Loaded ${client.banks.length} banks`);
// --------------------------------------------------------------------------
// Step 3: Access Oracle Prices for All Banks
// --------------------------------------------------------------------------
console.log("\n💰 Accessing oracle prices for all banks...\n");
client.bankMap.forEach((bank, bankAddress) => {
const oraclePrice = client.oraclePriceByBank.get(bankAddress);
if (oraclePrice) {
console.log(`Bank: ${bank.mint.toBase58()}`);
console.log(
` Realtime price: $${oraclePrice.priceRealtime.price.toNumber()}`
);
console.log(
` Confidence: ±$${oraclePrice.priceRealtime.confidence.toNumber()}`
);
console.log(
` Timestamp: ${new Date(oraclePrice.timestamp.toNumber() * 1000).toISOString()}`
);
console.log("");
}
});
// --------------------------------------------------------------------------
// Step 4: Manually Refresh Oracle Prices
// --------------------------------------------------------------------------
console.log("🔄 Refreshing oracle prices...");
const updatedOracleData = await fetchOracleData(
client.banks, // Array of all banks
{
pythOpts: {
mode: "on-chain", // or "api" for faster lookups
connection,
},
swbOpts: {
mode: "on-chain",
connection,
},
isolatedBanksOpts: {
fetchPrices: true,
},
}
);
console.log(
`✅ Refreshed ${updatedOracleData.bankOraclePriceMap.size} oracle prices`
);
// --------------------------------------------------------------------------
// Step 5: Access Specific Bank Oracle Price
// --------------------------------------------------------------------------
console.log("\n💵 Accessing specific bank oracle price...");
const usdcMint = new PublicKey(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
);
const usdcOraclePrice = updatedOracleData.mintOraclePriceMap.get(
usdcMint.toBase58()
);
if (usdcOraclePrice) {
console.log(
` USDC Price: $${usdcOraclePrice.priceRealtime.price.toNumber()}`
);
}
}
// ============================================================================
// Run Example
// ============================================================================
oraclePricesExample()
.then(() => {
process.exit(0);
})
.catch((error) => {
console.error("\n❌ Error:", error);
process.exit(1);
});