Skip to content

Commit

Permalink
tZero Api Witness spec files
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Apr 8, 2024
1 parent 0bc009d commit 0c1adc5
Show file tree
Hide file tree
Showing 62 changed files with 662 additions and 428 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/crypto/packages/nft/packages/nft/packages/witness/src/spec/Witness.Index.spec.ts"
"packages/payloadset/packages/api/src/spec/tzero-marketdata-sanddbox-call.spec.ts"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"apikey",
"Autobuild",
"autocrlf",
"autogen",
Expand Down Expand Up @@ -81,6 +82,7 @@
"loong",
"magick",
"mapbox",
"marketdata",
"Millis",
"mipsle",
"nodenext",
Expand Down Expand Up @@ -110,10 +112,12 @@
"tsup",
"typechain",
"typedarrays",
"tzero",
"userid",
"webp",
"wordlist",
"xylabs"
"xylabs",
"XYLB"
],
"npm.packageManager": "yarn"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@types/jest": "^29.5.12",
"@types/supertest": "^6.0.2",
"@xylabs/eslint-config": "^3.9.0",
"@xylabs/forget": "^3.0.25",
"@xylabs/forget": "^3.1.6",
"@xylabs/ts-scripts-yarn3": "^3.9.0",
"@xylabs/tsconfig": "^3.9.0",
"@xylabs/tsconfig-dom-jest": "^3.9.0",
Expand Down Expand Up @@ -51,7 +51,7 @@
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typedoc": "^0.25.12",
"typedoc": "^0.25.13",
"typescript": "^5.4.4"
},
"resolutions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ethers": "^6.11.1"
},
"devDependencies": {
"@xylabs/jest-helpers": "^3.0.25",
"@xylabs/jest-helpers": "^3.1.6",
"@xylabs/ts-scripts-yarn3": "^3.9.0",
"@xylabs/tsconfig": "^3.9.0",
"jest": "^29.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/object": "^3.0.25",
"@xylabs/object": "^3.1.6",
"@xyo-network/crypto-nft-score-model": "workspace:~",
"@xyo-network/diviner-model": "^2.94.23",
"@xyo-network/payload-model": "^2.94.23",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/object": "^3.0.25",
"@xylabs/object": "^3.1.6",
"@xyo-network/payload-model": "^2.94.23"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/packages/image-thumbnail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/hex": "^3.0.25",
"@xylabs/hex": "^3.1.6",
"@xyo-network/diviner-model": "^2.94.23",
"@xyo-network/diviner-payload-model": "^2.94.23",
"@xyo-network/module-model": "^2.94.23",
Expand Down
6 changes: 3 additions & 3 deletions packages/payload/packages/payments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/assert": "^3.0.25",
"@xylabs/crypto": "^3.0.25",
"@xylabs/hex": "^3.0.25",
"@xylabs/assert": "^3.1.6",
"@xylabs/crypto": "^3.1.6",
"@xylabs/hex": "^3.1.6",
"@xyo-network/account-model": "^2.94.23",
"@xyo-network/boundwitness-builder": "^2.94.23",
"@xyo-network/boundwitness-model": "^2.94.23",
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/packages/xns/plugins/record/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/hex": "^3.0.25",
"@xylabs/hex": "^3.1.6",
"@xyo-network/boundwitness-model": "^2.94.23",
"@xyo-network/boundwitness-validator": "^2.94.23",
"@xyo-network/payload-builder": "^2.94.23",
Expand Down
12 changes: 6 additions & 6 deletions packages/payloadset/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
"url": "https://github.com/XYOracleNetwork/plugins/issues"
},
"dependencies": {
"@xylabs/assert": "^3.0.25",
"@xylabs/axios": "^3.0.25",
"@xylabs/hex": "^3.0.25",
"@xylabs/object": "^3.0.25",
"@xylabs/assert": "^3.1.6",
"@xylabs/axios": "^3.1.6",
"@xylabs/hex": "^3.1.6",
"@xylabs/object": "^3.1.6",
"@xyo-network/abstract-witness": "^2.94.23",
"@xyo-network/hash": "^2.94.23",
"@xyo-network/payload-builder": "^2.94.23",
"@xyo-network/payload-model": "^2.94.23",
"@xyo-network/witness-model": "^2.94.23",
"base64-js": "^1.5.1",
"es6-template-strings": "^2.0.1"
"es6-dynamic-template": "^2.0.0"
},
"devDependencies": {
"@xylabs/jest-helpers": "^3.0.25",
"@xylabs/jest-helpers": "^3.1.6",
"@xylabs/ts-scripts-yarn3": "^3.9.0",
"@xylabs/tsconfig": "^3.9.0",
"@xyo-network/account": "^2.94.23",
Expand Down
2 changes: 1 addition & 1 deletion packages/payloadset/packages/api/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ApiUriCallWitnessConfig = WitnessConfig<

export type ApiUriTemplateCallWitnessConfig = WitnessConfig<
ApiCallWitnessConfigBase & {
params?: Record<string, unknown>
params?: Record<string, string>
uriTemplate: ApiUriTemplateCall['uriTemplate']
}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/payloadset/packages/api/src/Payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const asApiUriCall = AsObjectFactory.create(isApiUriCall)

export type ApiUriTemplateCall = Payload<
ApiCallFields & {
params?: Record<string, unknown>
params?: Record<string, string>
uriTemplate?: string
},
ApiCallSchema
Expand Down
9 changes: 5 additions & 4 deletions packages/payloadset/packages/api/src/Witness.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-statements */
import { assertEx } from '@xylabs/assert'
import { Axios, AxiosError, AxiosJson } from '@xylabs/axios'
import { Hash } from '@xylabs/hex'
Expand All @@ -7,7 +8,7 @@ import { PayloadBuilder } from '@xyo-network/payload-builder'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { WitnessParams } from '@xyo-network/witness-model'
import { fromByteArray } from 'base64-js'
import template from 'es6-template-strings'
import fillTemplate from 'es6-dynamic-template'

import { ApiCallWitnessConfig, ApiCallWitnessConfigSchema, asApiUriCallWitnessConfig, asApiUriTemplateCallWitnessConfig } from './Config'
import { checkIpfsUrl } from './lib'
Expand Down Expand Up @@ -60,11 +61,11 @@ export class ApiCallWitness<TParams extends ApiCallWitnessParams = ApiCallWitnes
if (callUri) {
url = new URL(callUri)
} else if (callUriTemplate) {
url = new URL(template(callUriTemplate, params))
url = new URL(fillTemplate(callUriTemplate, params))
} else if (configUri) {
url = new URL(configUri)
} else if (configUriTemplate) {
url = new URL(template(configUriTemplate, params))
url = new URL(fillTemplate(configUriTemplate, params))
}

if (url) {
Expand Down Expand Up @@ -116,7 +117,7 @@ export class ApiCallWitness<TParams extends ApiCallWitnessParams = ApiCallWitnes
try {
switch (this.accept) {
case 'application/json': {
const axios = new AxiosJson({ headers: this.params.headers, timeout: this.timeout })
const axios = new AxiosJson({ headers: { ...this.params.headers, Accept: 'application/json' }, timeout: this.timeout })
const response = await axios.get<ApiCallJsonResultType>(url)
if (response.status >= 200 && response.status < 300) {
const jsonResult = result as ApiCallJsonResult
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/a5a229a767e3998e13cce8160fd1c2b027317424/packages/manifest/src/schema.json",
"nodes": [
{
"config": {
"accountPath": "0",
"name": "ApiCall",
"schema": "network.xyo.node.config"
},
"modules": {
"public": [
{
"config": {
"accountPath": "0/0",
"language": "javascript",
"name": "ApiCallWitness",
"schema": "network.xyo.api.call.witness.config",
"uriTemplate": "https://gateway-web-markets-staging.tzero.com/mdt/public-pricehistory/${symbol}"
}
},
{
"config": {
"accountPath": "1/0",
"language": "javascript",
"name": "ApiCallSentinel",
"schema": "network.xyo.sentinel.config",
"synchronous": "true",
"tasks": [
{
"input": true,
"module": "ApiCallWitness"
}
]
}
}
]
}
}
],
"schema": "network.xyo.manifest"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/a5a229a767e3998e13cce8160fd1c2b027317424/packages/manifest/src/schema.json",
"nodes": [
{
"config": {
"accountPath": "0",
"name": "ApiCall",
"schema": "network.xyo.node.config"
},
"modules": {
"public": [
{
"config": {
"accountPath": "0/0",
"language": "javascript",
"name": "ApiCallWitness",
"schema": "network.xyo.api.call.witness.config",
"uriTemplate": "https://gateway-web-markets-staging.tzero.com/mdt/public-snapshots/${symbol}"
}
},
{
"config": {
"accountPath": "1/0",
"language": "javascript",
"name": "ApiCallSentinel",
"schema": "network.xyo.sentinel.config",
"synchronous": "true",
"tasks": [
{
"input": true,
"module": "ApiCallWitness"
}
]
}
}
]
}
}
],
"schema": "network.xyo.manifest"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/a5a229a767e3998e13cce8160fd1c2b027317424/packages/manifest/src/schema.json",
"nodes": [
{
"config": {
"accountPath": "0",
"name": "ApiCall",
"schema": "network.xyo.node.config"
},
"modules": {
"public": [
{
"config": {
"accountPath": "0/0",
"language": "javascript",
"name": "ApiCallWitness",
"schema": "network.xyo.api.call.witness.config",
"uriTemplate": "https://gateway-web-markets-staging.tzero.com/mdt/public-snapshots"
}
},
{
"config": {
"accountPath": "0/1",
"language": "javascript",
"name": "ApiCallSentinel",
"schema": "network.xyo.sentinel.config",
"synchronous": "true",
"tasks": [
{
"input": true,
"module": "ApiCallWitness"
}
]
}
}
]
}
}
],
"schema": "network.xyo.manifest"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable max-statements */
import { describeIf } from '@xylabs/jest-helpers'
import { JsonObject, toJsonString } from '@xylabs/object'
import { HDWallet } from '@xyo-network/account'
import { ManifestWrapper, PackageManifestPayload } from '@xyo-network/manifest'
import { ModuleFactory, ModuleFactoryLocator } from '@xyo-network/module-model'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { asSentinelInstance } from '@xyo-network/sentinel-model'
import { asWitnessInstance } from '@xyo-network/witness-model'

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

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

const apiKey = process.env.TZERO_MARKETDATA_SANDBOX_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 },
}),
)

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 call: ApiUriTemplateCall = { params: { symbol }, 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)}`)
})
})
})
Loading

0 comments on commit 0c1adc5

Please sign in to comment.