@xyo-network/crypto-asset-plugin 4.2.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +19 -15
- package/src/lib/spec/averaege.spec.ts +37 -0
- package/src/lib/spec/divineCoinGeckoPrices.spec.ts +36 -0
- package/src/lib/spec/divinePrices.spec.ts +21 -0
- package/src/lib/spec/divineUniswapPrices.spec.ts +20 -0
- package/src/spec/Diviner.spec.ts +39 -0
- package/src/spec/Plugin.spec.ts +17 -0
- package/typedoc.json +0 -5
- package/xy.config.ts +0 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/crypto-asset-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Typescript/Javascript Plugins for XYO Platform",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -28,23 +28,27 @@
|
|
|
28
28
|
},
|
|
29
29
|
"module": "dist/neutral/index.mjs",
|
|
30
30
|
"types": "dist/neutral/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"@xylabs/assert": "^
|
|
33
|
-
"@xylabs/exists": "^
|
|
34
|
-
"@xyo-network/coingecko-crypto-market-payload-plugin": "^
|
|
35
|
-
"@xyo-network/crypto-asset-payload-plugin": "^
|
|
36
|
-
"@xyo-network/diviner-abstract": "^
|
|
37
|
-
"@xyo-network/diviner-model": "^
|
|
38
|
-
"@xyo-network/module-model": "^
|
|
39
|
-
"@xyo-network/payload-builder": "^
|
|
40
|
-
"@xyo-network/payload-model": "^
|
|
41
|
-
"@xyo-network/payloadset-plugin": "^
|
|
42
|
-
"@xyo-network/uniswap-crypto-market-payload-plugin": "^
|
|
36
|
+
"@xylabs/assert": "^5.0.0",
|
|
37
|
+
"@xylabs/exists": "^5.0.0",
|
|
38
|
+
"@xyo-network/coingecko-crypto-market-payload-plugin": "^5.0.0",
|
|
39
|
+
"@xyo-network/crypto-asset-payload-plugin": "^5.0.0",
|
|
40
|
+
"@xyo-network/diviner-abstract": "^5.0.0",
|
|
41
|
+
"@xyo-network/diviner-model": "^5.0.0",
|
|
42
|
+
"@xyo-network/module-model": "^5.0.0",
|
|
43
|
+
"@xyo-network/payload-builder": "^5.0.0",
|
|
44
|
+
"@xyo-network/payload-model": "^5.0.0",
|
|
45
|
+
"@xyo-network/payloadset-plugin": "^5.0.0",
|
|
46
|
+
"@xyo-network/uniswap-crypto-market-payload-plugin": "^5.0.0"
|
|
43
47
|
},
|
|
44
48
|
"devDependencies": {
|
|
45
|
-
"@xylabs/ts-scripts-yarn3": "^7.0.
|
|
46
|
-
"@xylabs/tsconfig": "^7.0.
|
|
47
|
-
"@xylabs/vitest-extended": "^
|
|
49
|
+
"@xylabs/ts-scripts-yarn3": "^7.0.2",
|
|
50
|
+
"@xylabs/tsconfig": "^7.0.2",
|
|
51
|
+
"@xylabs/vitest-extended": "^5.0.0",
|
|
48
52
|
"typescript": "^5.8.3",
|
|
49
53
|
"vitest": "^3.2.4"
|
|
50
54
|
},
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import type { AssetInfo, CryptoMarketAssetPayload } from '@xyo-network/crypto-asset-payload-plugin'
|
|
4
|
+
import { CryptoMarketAssetSchema } from '@xyo-network/crypto-asset-payload-plugin'
|
|
5
|
+
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
6
|
+
import {
|
|
7
|
+
describe, expect,
|
|
8
|
+
it,
|
|
9
|
+
} from 'vitest'
|
|
10
|
+
|
|
11
|
+
import { average } from '../average.ts'
|
|
12
|
+
|
|
13
|
+
const schema = CryptoMarketAssetSchema
|
|
14
|
+
|
|
15
|
+
const getPayloadWithPrice = (price: number): CryptoMarketAssetPayload => {
|
|
16
|
+
const assets: Record<string, AssetInfo> = { xyo: { value: { usd: price.toString() } } }
|
|
17
|
+
const timestamp = Date.now()
|
|
18
|
+
return new PayloadBuilder<CryptoMarketAssetPayload>({ schema }).fields({ assets, timestamp }).build()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('average', () => {
|
|
22
|
+
it('averages numbers', async () => {
|
|
23
|
+
const payloads = await Promise.all([getPayloadWithPrice(1), getPayloadWithPrice(2), getPayloadWithPrice(3)])
|
|
24
|
+
expect(average(...payloads)?.xyo?.value?.usd).toBe('2')
|
|
25
|
+
})
|
|
26
|
+
it('handles single value', () => {
|
|
27
|
+
const payloads = getPayloadWithPrice(1)
|
|
28
|
+
expect(average(payloads)?.xyo?.value?.usd).toBe('1')
|
|
29
|
+
})
|
|
30
|
+
it('handles no values', () => {
|
|
31
|
+
expect(average()?.xyo?.value?.usd).toBeUndefined()
|
|
32
|
+
})
|
|
33
|
+
it('handles undefined values', async () => {
|
|
34
|
+
const payloads = await Promise.all([getPayloadWithPrice(1), undefined, getPayloadWithPrice(3)])
|
|
35
|
+
expect(average(...payloads)?.xyo?.value?.usd).toBe('2')
|
|
36
|
+
})
|
|
37
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import { assertEx } from '@xylabs/assert'
|
|
4
|
+
import {
|
|
5
|
+
describe, expect,
|
|
6
|
+
it,
|
|
7
|
+
} from 'vitest'
|
|
8
|
+
|
|
9
|
+
import { sampleCoinGeckoPayload } from '../../test/index.ts'
|
|
10
|
+
import { divineCoinGeckoPrices } from '../divineCoinGeckoPrices.ts'
|
|
11
|
+
|
|
12
|
+
describe('divineCoinGeckoPrices', () => {
|
|
13
|
+
// eslint-disable-next-line complexity
|
|
14
|
+
it('divines prices from CoinGecko', () => {
|
|
15
|
+
const result = divineCoinGeckoPrices(sampleCoinGeckoPayload)
|
|
16
|
+
expect(result.assets).toBeObject()
|
|
17
|
+
const assets = assertEx(result.assets)
|
|
18
|
+
for (let [token, assetInfo] of Object.entries(assets)) {
|
|
19
|
+
expect(token).toBeString()
|
|
20
|
+
expect(assetInfo).toBeObject()
|
|
21
|
+
const value = assertEx(assetInfo?.value)
|
|
22
|
+
expect(value).toBeObject()
|
|
23
|
+
for (let [symbol, price] of Object.entries(value)) {
|
|
24
|
+
expect(symbol).toBeString()
|
|
25
|
+
expect(price).toBeString()
|
|
26
|
+
const parsed = Number.parseFloat(price)
|
|
27
|
+
expect(parsed).toBeNumber()
|
|
28
|
+
expect(Number.isNaN(parsed)).toBeFalse()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
expect(result?.assets?.xyo?.value?.btc).toBe('6.28282e-7')
|
|
32
|
+
expect(result?.assets?.xyo?.value?.eth).toBe('0.00000885')
|
|
33
|
+
expect(result?.assets?.xyo?.value?.eur).toBe('0.01417995')
|
|
34
|
+
expect(result?.assets?.xyo?.value?.usd).toBe('0.01439307')
|
|
35
|
+
})
|
|
36
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describe, expect,
|
|
5
|
+
it,
|
|
6
|
+
} from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { sampleCoinGeckoPayload, sampleUniswapPayload } from '../../test/index.ts'
|
|
9
|
+
import { divinePrices } from '../divinePrices.ts'
|
|
10
|
+
|
|
11
|
+
describe('divinePrices', () => {
|
|
12
|
+
it('divines prices', () => {
|
|
13
|
+
const result = divinePrices(sampleCoinGeckoPayload, sampleUniswapPayload)
|
|
14
|
+
expect(result).toBeObject()
|
|
15
|
+
expect(result.timestamp).toBeNumber()
|
|
16
|
+
expect(result.assets.xyo?.value.btc).toBe('6.415295e-7')
|
|
17
|
+
expect(result.assets.xyo?.value.eth).toBe('0.000008908865')
|
|
18
|
+
expect(result.assets.xyo?.value.eur).toBe('0.01417995')
|
|
19
|
+
expect(result.assets.xyo?.value.usd).toBe('0.014635635000000001')
|
|
20
|
+
})
|
|
21
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describe, expect,
|
|
5
|
+
it,
|
|
6
|
+
} from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { sampleUniswapPayload } from '../../test/index.ts'
|
|
9
|
+
import { divineUniswapPrices } from '../divineUniswapPrices.ts'
|
|
10
|
+
|
|
11
|
+
describe('divineUniswapPrices', () => {
|
|
12
|
+
it('divines prices from Uniswap', () => {
|
|
13
|
+
const result = divineUniswapPrices(sampleUniswapPayload)
|
|
14
|
+
expect(result).toBeObject()
|
|
15
|
+
expect(result?.assets?.xyo?.value?.btc).toBe('6.54777e-7')
|
|
16
|
+
expect(result?.assets?.xyo?.value?.eth).toBe('0.00000896773')
|
|
17
|
+
expect(result?.assets?.xyo?.value?.eur).toBe(undefined)
|
|
18
|
+
expect(result?.assets?.xyo?.value?.usd).toBe('0.0148782')
|
|
19
|
+
})
|
|
20
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import type { CryptoMarketAssetPayload } from '@xyo-network/crypto-asset-payload-plugin'
|
|
4
|
+
import { CryptoMarketAssetSchema } from '@xyo-network/crypto-asset-payload-plugin'
|
|
5
|
+
import type { Payload } from '@xyo-network/payload-model'
|
|
6
|
+
import {
|
|
7
|
+
describe, expect,
|
|
8
|
+
test,
|
|
9
|
+
} from 'vitest'
|
|
10
|
+
|
|
11
|
+
import { CryptoMarketAssetDiviner } from '../Diviner.ts'
|
|
12
|
+
import { sampleCoinGeckoPayload, sampleUniswapPayload } from '../test/index.ts'
|
|
13
|
+
|
|
14
|
+
const coinGeckoPayload = sampleCoinGeckoPayload
|
|
15
|
+
const uniswapPayload = sampleUniswapPayload
|
|
16
|
+
|
|
17
|
+
describe('Diviner', () => {
|
|
18
|
+
const cases: [input: string, expected: string, data: Payload[]][] = [
|
|
19
|
+
['only CoinGecko Payload', 'observation', [coinGeckoPayload]],
|
|
20
|
+
['only Uniswap Payload', 'observation', [uniswapPayload]],
|
|
21
|
+
['CoinGecko & Uniswap Payload', 'observation', [coinGeckoPayload, uniswapPayload]],
|
|
22
|
+
['no inputs', 'empty observation', []],
|
|
23
|
+
]
|
|
24
|
+
test.each(cases)('with %s returns %s', async (_input: string, _expected: string, data: Payload[]) => {
|
|
25
|
+
const diviner = await CryptoMarketAssetDiviner.create({ account: 'random' })
|
|
26
|
+
const payloads = await diviner.divine(data)
|
|
27
|
+
expect(payloads).toBeArray()
|
|
28
|
+
expect(payloads.length).toBe(1)
|
|
29
|
+
for (let payload of payloads) {
|
|
30
|
+
if (payload?.schema === CryptoMarketAssetSchema) {
|
|
31
|
+
const assetPayload = payload as CryptoMarketAssetPayload
|
|
32
|
+
expect(assetPayload).toBeObject()
|
|
33
|
+
expect(assetPayload?.assets).toBeObject()
|
|
34
|
+
expect(assetPayload?.schema).toBe(CryptoMarketAssetSchema)
|
|
35
|
+
expect(assetPayload?.timestamp).toBeNumber()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import { PayloadSetPluginResolver } from '@xyo-network/payloadset-plugin'
|
|
4
|
+
import {
|
|
5
|
+
describe, expect,
|
|
6
|
+
test,
|
|
7
|
+
} from 'vitest'
|
|
8
|
+
|
|
9
|
+
import { CryptoMarketAssetPlugin } from '../Plugin.ts'
|
|
10
|
+
|
|
11
|
+
describe('CryptoMarketCoinGeckoPlugin', () => {
|
|
12
|
+
test('Add to Resolver', async () => {
|
|
13
|
+
const plugin = CryptoMarketAssetPlugin()
|
|
14
|
+
const resolver = await new PayloadSetPluginResolver().register(plugin)
|
|
15
|
+
expect(resolver.resolve(plugin.set)).toBeDefined()
|
|
16
|
+
})
|
|
17
|
+
})
|
package/typedoc.json
DELETED