Skip to content

Commit fcb98e5

Browse files
committed
chore: remove beta tag, add fn docs, and improve addReflectorEntries testing
1 parent 8f514d9 commit fcb98e5

File tree

4 files changed

+218
-528
lines changed

4 files changed

+218
-528
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blend-capital/blend-sdk",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "Javascript SDK for the Blend Protocol",
55
"type": "module",
66
"scripts": {
@@ -18,7 +18,7 @@
1818
],
1919
"publishConfig": {
2020
"access": "public",
21-
"tag": "beta"
21+
"tag": "latest"
2222
},
2323
"repository": {
2424
"type": "git",

src/oracle.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
2519
export 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+
*/
3038
export 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+
*/
6883
export 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+
*/
93122
export function addReflectorEntries(tx: Transaction): Transaction {
94123
if (tx.toEnvelope().switch() !== xdr.EnvelopeType.envelopeTypeTx()) {
95124
return tx;

0 commit comments

Comments
 (0)