@entangle-labs/udf-sdk
Version:
SDK for interacting with UDF Oracle
147 lines (106 loc) • 2.95 kB
Markdown
- [Installation](
- [Quick Start](
- [API Reference](
- [Examples](
- [Error Handling](
You can install the package using npm:
```bash
npm install @entangle-labs/udf-sdk
```
Or using yarn:
```bash
yarn add @entangle-labs/udf-sdk
```
```typescript
import { UdfSdk } from '@entangle-labs/udf-sdk';
// Initialize SDK
const sdk = new UdfSdk();
// Get latest price data
async function getEthPrice() {
// Get median price
const median = await sdk.getMedian('ETH/USD');
console.log('ETH/USD Median Price:', median);
// Get all votes
const votes = await sdk.getVotes(['ETH/USD']);
console.log('All votes:', votes);
// Get update call data
const callData = await sdk.getCallData(['ETH/USD']);
console.log('Update call data:', callData);
}
```
```typescript
constructor(baseUrl?: string)
```
Creates a new instance of UdfSdk.
- `baseUrl` (optional): Base URL for the UDF API. Defaults to 'https://udfsnap.ent-dx.com/'
#### Methods
##### getMedian(feedKey: string): Promise<number>
Gets the median price for a specific feed.
- `feedKey`: The feed identifier (e.g., 'ETH/USD')
- Returns: Promise resolving to the median price value
```typescript
const median = await sdk.getMedian('ETH/USD');
```
Gets all votes for specified feeds.
- `feedKeys`: Array of feed identifiers
- Returns: Promise resolving to array of decoded votes
```typescript
const votes = await sdk.getVotes(['ETH/USD', 'BTC/USD']);
```
Gets the update call data for specified feeds.
- `feedKeys`: Array of feed identifiers
- Returns: Promise resolving to update call data string
```typescript
const callData = await sdk.getCallData(['ETH/USD']);
```
```typescript
async function getMultipleFeeds() {
const sdk = new UdfSdk();
const feeds = ['ETH/USD', 'BTC/USD'];
// Get all feed data
const votes = await sdk.getVotes(feeds);
// Process votes
for (const vote of votes) {
console.log(`${vote.feedKey}: ${vote.value} (from ${vote.publisher})`);
}
}
```
```typescript
const sdk = new UdfSdk('https://your-custom-url.com');
```
The SDK throws errors in the following cases:
1. Network Errors
```typescript
try {
await sdk.getVotes(['ETH/USD']);
} catch (error) {
if (error.message.includes('HTTP error')) {
console.error('Network error:', error);
}
}
```
2. Invalid Data Errors
```typescript
try {
const median = await sdk.getMedian('ETH/USD');
} catch (error) {
if (error.message.includes('No votes provided')) {
console.error('No data available:', error);
}
}
```
- Node.js 16 or higher