-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Produce item published events (#29)
- Loading branch information
1 parent
37147c0
commit b93bf72
Showing
5 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Events, ItemPublishedEvent } from '@dcl/schemas' | ||
import { AppComponents, IEventGenerator } from '../../types' | ||
|
||
export const PAGE_SIZE = 1000 | ||
|
||
const PUBLISHED_ITEMS_QUERY = ` | ||
query Items($updatedAt: BigInt!, $paginationId: ID) { | ||
items( | ||
where: {updatedAt_gte: $updatedAt, id_gt: $paginationId} | ||
orderBy: id | ||
orderDirection: asc | ||
first: ${PAGE_SIZE} | ||
) { | ||
id | ||
collection{ | ||
id | ||
} | ||
blockchainId | ||
creator | ||
itemType | ||
rarity | ||
urn | ||
createdAt | ||
updatedAt | ||
metadata { | ||
wearable { | ||
name | ||
} | ||
emote { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
type ItemsResponse = { | ||
items: { | ||
id: string | ||
collection: { | ||
id: string | ||
} | ||
blockchainId: string | ||
creator: string | ||
itemType: string | ||
rarity: string | ||
urn: string | ||
createdAt: number | ||
updatedAt: number | ||
metadata: { | ||
wearable: { | ||
name: string | ||
} | ||
emote: { | ||
name: string | ||
} | ||
} | ||
}[] | ||
} | ||
|
||
export async function itemPublishedProducer( | ||
components: Pick<AppComponents, 'l2CollectionsSubGraph'> | ||
): Promise<IEventGenerator> { | ||
const { l2CollectionsSubGraph } = components | ||
|
||
async function run(updatedAt: number) { | ||
const now = Date.now() | ||
const produced: ItemPublishedEvent[] = [] | ||
|
||
let result: ItemsResponse | ||
let paginationId = '' | ||
do { | ||
result = await l2CollectionsSubGraph.query<ItemsResponse>(PUBLISHED_ITEMS_QUERY, { | ||
updatedAt: Math.floor(updatedAt / 1000), | ||
paginationId | ||
}) | ||
|
||
if (result.items.length === 0) { | ||
break | ||
} | ||
|
||
for (const item of result.items) { | ||
const event: ItemPublishedEvent = { | ||
type: Events.Type.BLOCKCHAIN, | ||
subType: Events.SubType.Blockchain.ITEM_PUBLISHED, | ||
key: item.urn, | ||
timestamp: item.createdAt * 1000, | ||
metadata: { | ||
creator: item.creator, | ||
category: item.metadata.wearable ? 'wearable' : 'emote', | ||
rarity: item.rarity, | ||
network: 'polygon', | ||
tokenId: item.id | ||
} | ||
} | ||
produced.push(event) | ||
|
||
paginationId = item.id | ||
} | ||
} while (result.items.length === PAGE_SIZE) | ||
|
||
return { | ||
event: { | ||
type: Events.Type.BLOCKCHAIN, | ||
subType: Events.SubType.Blockchain.ITEM_PUBLISHED | ||
}, | ||
records: produced, | ||
lastRun: now | ||
} | ||
} | ||
|
||
return { | ||
event: { | ||
type: Events.Type.BLOCKCHAIN, | ||
subType: Events.SubType.Blockchain.ITEM_PUBLISHED | ||
}, | ||
run | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { createConfigComponent } from '@well-known-components/env-config-provider' | ||
import { itemPublishedProducer } from '../../../src/adapters/producers/item-published' | ||
|
||
describe('item published producer', () => { | ||
test('should work when some new published item is found', async () => { | ||
const l2CollectionsSubGraph = { | ||
query: jest.fn() | ||
} | ||
l2CollectionsSubGraph.query.mockReturnValue({ | ||
items: [ | ||
{ | ||
id: '0x6105f0f5ef8b28cf81e64551588d13221d4151ad-3', | ||
collection: { | ||
id: '0x6105f0f5ef8b28cf81e64551588d13221d4151ad' | ||
}, | ||
blockchainId: '3', | ||
creator: '0x943d99cefa84b247b679d2b7cce17c7c93e6587b', | ||
itemType: 'wearable_v2', | ||
rarity: 'mythic', | ||
urn: 'urn:decentraland:matic:collections-v2:0x6105f0f5ef8b28cf81e64551588d13221d4151ad:3', | ||
createdAt: '1623259167', | ||
updatedAt: '1623355278', | ||
metadata: { | ||
wearable: { | ||
name: 'MetaZoo Intl. BlackCat Hood' | ||
}, | ||
emote: null | ||
} | ||
} | ||
] | ||
}) | ||
|
||
const producer = await itemPublishedProducer({ l2CollectionsSubGraph }) | ||
let result = await producer.run(Date.now()) | ||
expect(result).toMatchObject({ | ||
event: { | ||
type: 'blockchain', | ||
subType: 'item-published' | ||
}, | ||
records: [ | ||
{ | ||
type: 'blockchain', | ||
subType: 'item-published', | ||
key: 'urn:decentraland:matic:collections-v2:0x6105f0f5ef8b28cf81e64551588d13221d4151ad:3', | ||
timestamp: 1623259167000, | ||
metadata: { | ||
creator: '0x943d99cefa84b247b679d2b7cce17c7c93e6587b', | ||
category: 'wearable', | ||
rarity: 'mythic', | ||
network: 'polygon', | ||
tokenId: '0x6105f0f5ef8b28cf81e64551588d13221d4151ad-3' | ||
} | ||
} | ||
], | ||
lastRun: expect.anything() | ||
}) | ||
}) | ||
|
||
test('should work when no new published items', async () => { | ||
const l2CollectionsSubGraph = { | ||
query: jest.fn() | ||
} | ||
l2CollectionsSubGraph.query.mockReturnValue({ | ||
items: [] | ||
}) | ||
|
||
const producer = await itemPublishedProducer({ l2CollectionsSubGraph }) | ||
let result = await producer.run(Date.now()) | ||
expect(result).toMatchObject({ | ||
event: { | ||
type: 'blockchain', | ||
subType: 'item-published' | ||
}, | ||
records: [], | ||
lastRun: expect.anything() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters