@@ -16,17 +16,25 @@ const REFLECTOR_ORACLE_ADDRESSES = [
1616 'CAFJZQWSED6YAWZU3GWRTOCNPPCGBN32L7QV43XX5LZLFTK6JLN34DLN' ,
1717] ;
1818
19- interface ReflectorEntry {
20- contract : xdr . ScAddress ;
21- key : number ;
22- roundTimestamp : bigint ;
23- }
24-
2519export interface PriceData {
20+ /**
21+ * The price as a fixed point number with the oracle's decimals.
22+ */
2623 price : bigint ;
24+ /**
25+ * The timestamp of the price in seconds
26+ */
2727 timestamp : number ;
2828}
2929
30+ /**
31+ * Fetch the `lastprice` from an oracle contract for the given token.
32+ * @param network - The network to use
33+ * @param oracle_id - The oracle contract ID
34+ * @param token_id - The token contract ID to fetch the price for
35+ * @returns The PriceData
36+ * @throws Will throw an error if `None` is returned or if the simulation fails.
37+ */
3038export async function getOraclePrice (
3139 network : Network ,
3240 oracle_id : string ,
@@ -55,7 +63,7 @@ export async function getOraclePrice(
5563 // eslint-disable-next-line
5664 // @ts -ignore
5765 price : scValToNative ( price_result [ 0 ] ?. val ( ) ) ,
58- timestamp : scValToNative ( price_result [ 1 ] ?. val ( ) ) ,
66+ timestamp : Number ( scValToNative ( price_result [ 1 ] ?. val ( ) ) ) ,
5967 } ;
6068 }
6169 }
@@ -65,6 +73,13 @@ export async function getOraclePrice(
6573 }
6674}
6775
76+ /**
77+ * Fetch the `decimals` from an oracle contract.
78+ * @param network - The network to use
79+ * @param oracle_id - The oracle contract ID
80+ * @returns The decimals and latest ledger number
81+ * @throws Will throw an error if the simulation fails.
82+ */
6883export async function getOracleDecimals (
6984 network : Network ,
7085 oracle_id : string
@@ -90,6 +105,20 @@ export async function getOracleDecimals(
90105 }
91106}
92107
108+ /**
109+ * Add future Reflector oracle entries to the read-only footprint of a transaction.
110+ * This ensures that if a future oracle round occurs before the transaction is executed,
111+ * the future oracle round will still be included in the footprint.
112+ *
113+ * This only works for Reflector based oracles as it makes assumptions based on how the
114+ * oracle contracts keys are structured.
115+ *
116+ * If more than 100 entries are added to the read-only footprint, it will stop adding. The priority
117+ * is given to the oracle contracts seen first, and the indexes for the contract that are seen first.
118+ *
119+ * @param tx - The transaction to add the reflector entries to.
120+ * @returns A new transaction object with the reflector entries added to the read-only footprint if necessary.
121+ */
93122export function addReflectorEntries ( tx : Transaction ) : Transaction {
94123 if ( tx . toEnvelope ( ) . switch ( ) !== xdr . EnvelopeType . envelopeTypeTx ( ) ) {
95124 return tx ;
0 commit comments