Skip to content

Commit

Permalink
Add sources field to divined result
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Aug 1, 2023
1 parent 8498e16 commit c18d0c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AbstractDiviner } from '@xyo-network/abstract-diviner'
import { PayloadHasher } from '@xyo-network/core'
import {
isNftInfoPayload,
NftScoreDivinerConfig,
Expand All @@ -23,6 +24,14 @@ export const isNftScorePayload = (payload: Payload): payload is NftScorePayload
export class NftScoreDiviner<TParams extends NftScoreDivinerParams = NftScoreDivinerParams> extends AbstractDiviner<TParams> {
static override configSchemas = [NftScoreDivinerConfigSchema]

protected override divineHandler = async (payloads?: Payload[]): Promise<Payload[]> =>
(await Promise.all(payloads?.filter(isNftInfoPayload).map(analyzeNft) ?? [])).map(toNftScorePayload)
protected override divineHandler = async (payloads?: Payload[]): Promise<Payload[]> => {
const nfts = payloads?.filter(isNftInfoPayload) ?? []
const results = await Promise.all(
nfts.map<Promise<NftScorePayload>>(async (p) => {
const [score, sourceHash] = await Promise.all([toNftScorePayload(await analyzeNft(p)), PayloadHasher.hashAsync(p)])
return { ...score, sources: [sourceHash] }
}),
)
return results
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ describe('NftScoreDiviner', () => {
})
test('divine', async () => {
const diviner = await NftScoreDiviner.create({ account: await HDWallet.random() })
const result = await diviner.divine(data)
const scores = result.filter(isNftScorePayload)
expect(scores.length).toBeGreaterThan(0)
expect(result.length).toEqual(scores.length)
for (const score of scores) {
const scores = (await diviner.divine(data)).filter(isNftScorePayload)
expect(scores).toBeArrayOfSize(data.length)
for (let i = 0; i < scores.length; i++) {
const score = scores[i]
const wrapped = PayloadWrapper.wrap(score)
expect(await wrapped.getValid()).toBe(true)
const payload = wrapped.payload()
expect(payload?.sources).toBeArrayOfSize(1)
expect(payload?.sources?.[0]).toBeString()
const sourceHash = await PayloadWrapper.wrap(data[i]).hashAsync()
expect(payload?.sources?.[0]).toBe(sourceHash)
}
})
})

0 comments on commit c18d0c5

Please sign in to comment.