Skip to content

Commit

Permalink
retrieve oracle data from youves contract
Browse files Browse the repository at this point in the history
replaces coinbase oracle which has been deprecated
  • Loading branch information
skenaja committed Dec 6, 2024
1 parent 10eed9a commit c4c2d01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
6 changes: 4 additions & 2 deletions __tests__/harbinger-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import BigNumber from 'bignumber.js'
* Tests for Harbinger client.
*
* These tests are not hermetic.
*
* Using the new mainnet Youves Oracle contract's view as contract not deployed to sandbox.
*/

const NODE_URL = 'https://sandbox.hover.engineering'
const NODE_URL = 'https://mainnet.smartpy.io'

// Allow extra time for RPCs
jest.setTimeout(30_000) // 30 seconds

// Client under test
const harbingerClient = new HarbingerClient(NODE_URL, CONTRACTS.SANDBOX.HARBINGER_NORMALIZER!)
const harbingerClient = new HarbingerClient(NODE_URL, CONTRACTS.MAIN.YOUVES_PROXY!, CONTRACTS.MAIN.OVEN_PROXY!)

test('harbinger client - gets date', async function () {
// GIVEN a Harbinger Client
Expand Down
23 changes: 15 additions & 8 deletions src/harbinger-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TezosToolkit } from '@taquito/taquito'
import Mutez from './types/mutez'

/** Asset pair to query */
const ASSET_CODE = 'XTZ-USD'
const ASSET_CODE = 'XTZUSDT'

/**
* Price feed data.
Expand All @@ -24,23 +24,30 @@ export default class HarbingerClient {
* Construct a new Oracle Client.
*
* @param nodeUrl The URL of the node to connect to.
* @param normalizerAddress The address of the Normalizer contract to read from.
* @param oracleAddress The address of the Youves Oracle contract to read price from.
* @param ovenProxyAddress The address of the oven proxy that is calling for the oracle data.
*/
public constructor(nodeUrl: string, public readonly normalizerAddress: Address) {
public constructor(
nodeUrl: string,
public readonly oracleAddress: Address,
public readonly ovenProxyAddress: Address,
) {
this.tezos = new TezosToolkit(nodeUrl)
}

/**
* Retrieve price feed data.
*/
public async getPriceData(): Promise<HarbingerPriceFeedData> {
const normalizerContract = await this.tezos.contract.at(this.normalizerAddress)
const normalizerStorage: any = await normalizerContract.storage()
const assetData = await normalizerStorage.assetMap.get(ASSET_CODE)
const oracleContract = await this.tezos.contract.at(this.oracleAddress)
const result = await oracleContract.contractViews
.get_price_with_timestamp(ASSET_CODE)
.executeView({ viewCaller: this.ovenProxyAddress })
const assetData = result

return {
time: new Date(assetData.lastUpdateTime),
price: assetData.computedPrice, // Note: assetData.computedPrice is a BigNumber
time: new Date(Math.floor(new Date(assetData.last_update_timestamp).getTime() / 1000)), // taquito returns timestamp in ms not s
price: assetData.price, // Note: assetData.price is a BigNumber
}
}
}

0 comments on commit c4c2d01

Please sign in to comment.