Skip to content

Commit

Permalink
Merge pull request #29 from XYOracleNetwork/feature/tzero-types
Browse files Browse the repository at this point in the history
TZero Types
  • Loading branch information
JoelBCarter authored May 28, 2024
2 parents c2b9d09 + fe05a36 commit 6bcc354
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"--forceExit",
"--runInBand",
"--testTimeout=120000",
"packages/payloadset/packages/evm/src/spec/NftMetadataUriToNftMetadata/NftMetadataUriToNftMetadata.spec.ts"
"packages/payloadset/packages/api/src/spec/tzero-marketdata-price-history.spec.ts"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable max-statements */
import { assertEx } from '@xylabs/assert'
import { describeIf } from '@xylabs/jest-helpers'
import { JsonObject, toJsonString } from '@xylabs/object'
import { JsonObject } from '@xylabs/object'
import { HDWallet } from '@xyo-network/account'
import { ManifestWrapper, PackageManifestPayload } from '@xyo-network/manifest'
import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
import { ModuleFactory } from '@xyo-network/module-model'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { asSentinelInstance } from '@xyo-network/sentinel-model'
import { asSentinelInstance, SentinelInstance } from '@xyo-network/sentinel-model'
import { asWitnessInstance } from '@xyo-network/witness-model'

import { ApiCallJsonResult, ApiCallResultSchema, ApiCallSchema, ApiUriTemplateCall, isApiCallErrorResult } from '../Payload'
Expand All @@ -15,47 +15,68 @@ import tzeroMarketdataManifest from './tzero-marketdata-price-history.json'

describe('tZero', () => {
const symbol = 'XYLB'

const apiKey = process.env.TZERO_MARKETDATA_API_KEY

describeIf(apiKey)('price-history', () => {
type TZeroMarketdataSandboxResponse = JsonObject
it('specifying symbol', async () => {
const mnemonic = 'later puppy sound rebuild rebuild noise ozone amazing hope broccoli crystal grief'
const wallet = await HDWallet.fromPhrase(mnemonic)
const locator = new ModuleFactoryLocator()

locator.register(
new ModuleFactory(ApiCallWitness, {
headers: { 'x-apikey': apiKey },
}),
)
interface TZeroHistoricalPrice extends JsonObject {
close: number | null
date: string
high: number | null
low: number | null
open: number | null
symbol: string
volume: number
}
interface TZeroPriceHistoryResponse extends JsonObject {
currentPage: number
priceHistories: TZeroHistoricalPrice[]
size: number
totalCount: number
totalPages: number
}
let sentinel: SentinelInstance

beforeAll(async () => {
const wallet = await HDWallet.random()
const locator = new ModuleFactoryLocator()
locator.register(new ModuleFactory(ApiCallWitness, { headers: { 'x-apikey': apiKey } }))
const manifest = new ManifestWrapper(tzeroMarketdataManifest as PackageManifestPayload, wallet, locator)

const node = await manifest.loadNodeFromIndex(0)

const mods = await node.resolve('*')

expect(mods.length).toBeGreaterThan(1)

const resolvedWitness = await node.resolve('ApiCallWitness')
expect(resolvedWitness).toBeDefined()

const witness = asWitnessInstance(resolvedWitness)
expect(witness).toBeDefined()

const sentinel = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinel).toBeDefined()

const sentinelInstance = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinelInstance).toBeDefined()
sentinel = assertEx(sentinelInstance)
})
it('specifying symbol', async () => {
const call: ApiUriTemplateCall = { params: { symbol }, schema: ApiCallSchema }

const report = await sentinel?.report([call])

const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<TZeroMarketdataSandboxResponse>>(ApiCallResultSchema))
const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<TZeroPriceHistoryResponse>>(ApiCallResultSchema))
expect(apiCallResult).toBeDefined()
console.log(`Result: ${toJsonString(apiCallResult, 10)}`)
expect(isApiCallErrorResult(apiCallResult)).toBe(false)
expect(apiCallResult?.data).toBeObject()
const data = assertEx(apiCallResult?.data)
expect(data.currentPage).toEqual(expect.any(Number))
expect(data.priceHistories).toBeArray()
expect(data.priceHistories.length).toBeGreaterThan(0)
for (const priceHistory of data.priceHistories) {
expect(priceHistory.symbol).toEqual(expect.any(String))
expect(priceHistory.date).toEqual(expect.any(String))
expect(priceHistory.open).toBeOneOf([null, expect.any(Number)])
expect(priceHistory.high).toBeOneOf([null, expect.any(Number)])
expect(priceHistory.low).toBeOneOf([null, expect.any(Number)])
expect(priceHistory.close).toBeOneOf([null, expect.any(Number)])
expect(priceHistory.volume).toEqual(expect.any(Number))
}
expect(data.size).toEqual(expect.any(Number))
expect(data.totalCount).toEqual(expect.any(Number))
expect(data.totalPages).toEqual(expect.any(Number))
})
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable max-statements */
import { assertEx } from '@xylabs/assert'
import { describeIf } from '@xylabs/jest-helpers'
import { JsonObject, toJsonString } from '@xylabs/object'
import { JsonObject } from '@xylabs/object'
import { HDWallet } from '@xyo-network/account'
import { ManifestWrapper, PackageManifestPayload } from '@xyo-network/manifest'
import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
import { ModuleFactory } from '@xyo-network/module-model'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { asSentinelInstance } from '@xyo-network/sentinel-model'
import { asSentinelInstance, SentinelInstance } from '@xyo-network/sentinel-model'
import { asWitnessInstance } from '@xyo-network/witness-model'

import { ApiCallJsonResult, ApiCallResultSchema, ApiCallSchema, ApiUriTemplateCall, isApiCallErrorResult } from '../Payload'
Expand All @@ -15,47 +15,74 @@ import tzeroMarketdataManifest from './tzero-marketdata-snapshots-symbol.json'

describe('tZero', () => {
const symbol = 'XYLB'

const apiKey = process.env.TZERO_MARKETDATA_SANDBOX_API_KEY

const apiKey = process.env.TZERO_MARKETDATA_API_KEY
describeIf(apiKey)('snapshots', () => {
type TZeroMarketdataSandboxResponse = JsonObject
it('specifying symbol', async () => {
const mnemonic = 'later puppy sound rebuild rebuild noise ozone amazing hope broccoli crystal grief'
const wallet = await HDWallet.fromPhrase(mnemonic)
const locator = new ModuleFactoryLocator()
interface TZeroMarketdataSnapshotResponse extends JsonObject {
askPrice: number | null
askPriceRate: number | null
askQtyBookTotal: number | null
askQuantity: number | null
bidPrice: number | null
bidPriceRate: number | null
bidQtyBookTotal: number | null
bidQuantity: number | null
high: number | null
lastPrice: number | null
lastQuantity: number | null
low: number | null
open: number | null
prevClosePx: number | null
symbol: string
timestamp: string
volume: number
}

locator.register(
new ModuleFactory(ApiCallWitness, {
headers: { 'x-apikey': apiKey },
}),
)
let sentinel: SentinelInstance

beforeAll(async () => {
const wallet = await HDWallet.random()
const locator = new ModuleFactoryLocator()
locator.register(new ModuleFactory(ApiCallWitness, { headers: { 'x-apikey': apiKey } }))
const manifest = new ManifestWrapper(tzeroMarketdataManifest as PackageManifestPayload, wallet, locator)

const node = await manifest.loadNodeFromIndex(0)

const mods = await node.resolve('*')

expect(mods.length).toBeGreaterThan(1)

const resolvedWitness = await node.resolve('ApiCallWitness')
expect(resolvedWitness).toBeDefined()

const witness = asWitnessInstance(resolvedWitness)
expect(witness).toBeDefined()
const sentinelInstance = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinelInstance).toBeDefined()
sentinel = assertEx(sentinelInstance)
})

const sentinel = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinel).toBeDefined()

it('specifying symbol', async () => {
const call: ApiUriTemplateCall = { params: { symbol }, schema: ApiCallSchema }

const report = await sentinel?.report([call])

const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<TZeroMarketdataSandboxResponse>>(ApiCallResultSchema))
const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<TZeroMarketdataSnapshotResponse>>(ApiCallResultSchema))
expect(apiCallResult).toBeDefined()
console.log(`Result: ${toJsonString(apiCallResult, 10)}`)
expect(isApiCallErrorResult(apiCallResult)).toBe(false)
expect(apiCallResult?.data).toBeObject()
const data = assertEx(apiCallResult?.data)
expect(data.askPrice).toBeOneOf([null, expect.any(Number)])
expect(data.askPriceRate).toBeOneOf([null, expect.any(Number)])
expect(data.askQtyBookTotal).toBeOneOf([null, expect.any(Number)])
expect(data.askQuantity).toBeOneOf([null, expect.any(Number)])
expect(data.bidPrice).toBeOneOf([null, expect.any(Number)])
expect(data.bidPriceRate).toBeOneOf([null, expect.any(Number)])
expect(data.bidQtyBookTotal).toBeOneOf([null, expect.any(Number)])
expect(data.bidQuantity).toBeOneOf([null, expect.any(Number)])
expect(data.high).toBeOneOf([null, expect.any(Number)])
expect(data.lastPrice).toBeOneOf([null, expect.any(Number)])
expect(data.lastQuantity).toBeOneOf([null, expect.any(Number)])
expect(data.low).toBeOneOf([null, expect.any(Number)])
expect(data.open).toBeOneOf([null, expect.any(Number)])
expect(data.prevClosePx).toEqual(expect.any(Number))
expect(data.symbol).toEqual(expect.any(String))
expect(data.timestamp).toEqual(expect.any(String))
expect(data.volume).toEqual(expect.any(Number))
})
})
})
Original file line number Diff line number Diff line change
@@ -1,59 +1,92 @@
/* eslint-disable max-statements */
import { assertEx } from '@xylabs/assert'
import { describeIf } from '@xylabs/jest-helpers'
import { JsonObject, toJsonString } from '@xylabs/object'
import { JsonObject } from '@xylabs/object'
import { HDWallet } from '@xyo-network/account'
import { ManifestWrapper, PackageManifestPayload } from '@xyo-network/manifest'
import { ModuleFactoryLocator } from '@xyo-network/module-factory-locator'
import { ModuleFactory } from '@xyo-network/module-model'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { asSentinelInstance } from '@xyo-network/sentinel-model'
import { asSentinelInstance, SentinelInstance } from '@xyo-network/sentinel-model'
import { asWitnessInstance } from '@xyo-network/witness-model'

import { ApiCallJsonResult, ApiCallResultSchema, ApiCallSchema, ApiUriTemplateCall, isApiCallErrorResult } from '../Payload'
import { ApiCallWitness } from '../Witness'
import tzeroMarketdataManifest from './tzero-marketdata-snapshots.json'

describe('tZero', () => {
const apiKey = process.env.TZERO_MARKETDATA_SANDBOX_API_KEY
const apiKey = process.env.TZERO_MARKETDATA_API_KEY

describeIf(apiKey)('snapshots', () => {
type TZeroMarketdataSandboxResponse = JsonObject
it('specifying symbol', async () => {
const mnemonic = 'later puppy sound rebuild rebuild noise ozone amazing hope broccoli crystal grief'
const wallet = await HDWallet.fromPhrase(mnemonic)
const locator = new ModuleFactoryLocator()
describeIf(apiKey)('public-snapshots', () => {
interface Snapshot extends JsonObject {
askPrice: number
askPriceRate: number | null
askQtyBookTotal: number
askQuantity: number
bidPrice: number
bidPriceRate: number | null
high: number | null
lastPrice: number | null
lastQuantity: number | null
low: number | null
open: number | null
prevClosePx: number
symbol: string
timestamp: string
volume: number
}

locator.register(
new ModuleFactory(ApiCallWitness, {
headers: { 'x-apikey': apiKey },
}),
)
interface TZeroMarketdataSandboxResponse extends JsonObject {
snapshots: Snapshot[]
}

const manifest = new ManifestWrapper(tzeroMarketdataManifest as PackageManifestPayload, wallet, locator)
let sentinel: SentinelInstance

beforeAll(async () => {
const wallet = await HDWallet.random()
const locator = new ModuleFactoryLocator()
locator.register(new ModuleFactory(ApiCallWitness, { headers: { 'x-apikey': apiKey } }))
const manifest = new ManifestWrapper(tzeroMarketdataManifest as PackageManifestPayload, wallet, locator)
const node = await manifest.loadNodeFromIndex(0)

const mods = await node.resolve('*')

expect(mods.length).toBeGreaterThan(1)

const resolvedWitness = await node.resolve('ApiCallWitness')
expect(resolvedWitness).toBeDefined()

const witness = asWitnessInstance(resolvedWitness)
expect(witness).toBeDefined()

const sentinel = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinel).toBeDefined()

const sentinelInstance = asSentinelInstance(await node.resolve('ApiCallSentinel'))
expect(sentinelInstance).toBeDefined()
sentinel = assertEx(sentinelInstance)
})
it('returns all public snapshots', async () => {
const call: ApiUriTemplateCall = { schema: ApiCallSchema }

const report = await sentinel?.report([call])

const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<TZeroMarketdataSandboxResponse>>(ApiCallResultSchema))
expect(apiCallResult).toBeDefined()
console.log(`Result: ${toJsonString(apiCallResult, 10)}`)
expect(isApiCallErrorResult(apiCallResult)).toBe(false)
expect(apiCallResult?.data).toBeObject()
const data = assertEx(apiCallResult?.data)
expect(data.snapshots).toBeArray()
for (const snapshot of data.snapshots) {
expect(snapshot.symbol).toEqual(expect.any(String))
expect(snapshot.high).toBeOneOf([null, expect.any(Number)])
expect(snapshot.low).toBeOneOf([null, expect.any(Number)])
expect(snapshot.open).toBeOneOf([null, expect.any(Number)])
expect(snapshot.volume).toEqual(expect.any(Number))
expect(snapshot.lastPrice).toBeOneOf([null, expect.any(Number)])
expect(snapshot.lastQuantity).toBeOneOf([null, expect.any(Number)])
expect(snapshot.prevClosePx).toEqual(expect.any(Number))
expect(snapshot.bidPrice).toEqual(expect.any(Number))
expect(snapshot.bidPriceRate).toBeOneOf([null, expect.any(Number)])
expect(snapshot.bidQuantity).toEqual(expect.any(Number))
expect(snapshot.bidQtyBookTotal).toEqual(expect.any(Number))
expect(snapshot.askPrice).toEqual(expect.any(Number))
expect(snapshot.askPriceRate).toBeOneOf([null, expect.any(Number)])
expect(snapshot.askQuantity).toEqual(expect.any(Number))
expect(snapshot.askQtyBookTotal).toEqual(expect.any(Number))
expect(snapshot.timestamp).toEqual(expect.any(String))
}
})
})
})

0 comments on commit 6bcc354

Please sign in to comment.