@rholabs/rho-sdk
Version:
Rho Protocol SDK
52 lines (43 loc) • 1.34 kB
text/typescript
import axios from 'axios'
import { OraclePackage } from '../../typings'
export interface OracleRecord {
oraclePackage: OraclePackage
latestRate: string
rateDelta: string
indexValueRay: string
rateTimestamp: number
}
export interface OracleAPIConfig {
oracleServiceUrl: string | string[]
}
export class OracleAPI {
private readonly config: OracleAPIConfig
constructor(config: OracleAPIConfig) {
this.config = config
}
public async getRates() {
const oracleUrls = typeof this.config.oracleServiceUrl === 'string'
? [this.config.oracleServiceUrl]
: this.config.oracleServiceUrl
let oracleRecords: OracleRecord[] = []
for(const oracleUrl of oracleUrls) {
const {
data: records
} = await axios.get<OracleRecord[]>(`${oracleUrl}/records`)
oracleRecords.push(...records)
}
return oracleRecords
}
public async getOraclePackages() {
const rates = await this.getRates()
return rates.map(item => item.oraclePackage)
}
public async getMarketOraclePackage(marketId: string) {
const rates = await this.getRates()
const marketRate = rates.find(item => item.oraclePackage.marketId === marketId)
if(marketRate) {
return marketRate.oraclePackage
}
throw new Error(`Failed to get oracle package for market ${marketId}`)
}
}