-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubgraphEvent.test.ts
174 lines (148 loc) Β· 4.19 KB
/
SubgraphEvent.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { NvmAccount, Nevermined, generateId, getChecksumAddress } from '../../src'
import config from '../../test/config'
import { assert } from 'chai'
import { awaitTimeout, mineBlocks, sleep } from '../utils/utils'
describe('SubgraphEvent', () => {
let account: NvmAccount
let nevermined: Nevermined
let executeTransaction: () => Promise<any>
before(async function () {
if (!config.graphHttpUri) {
this.skip()
}
nevermined = await Nevermined.getInstance(config)
;[account] = nevermined.accounts.list()
await nevermined.keeper.dispenser.requestTokens(1, account)
executeTransaction = () => nevermined.keeper.dispenser.requestTokens(1, account)
})
it('should query for the event', async () => {
const response = await nevermined.keeper.token.events.getEventData({
eventName: 'Transfer',
filterSubgraph: {
where: {
to: account.getId(),
},
orderBy: 'blockNumber',
orderDirection: 'desc',
},
result: {
to: true,
value: true,
blockNumber: true,
blockTimestamp: true,
transactionHash: true,
},
})
const event = response[0]
assert.strictEqual(getChecksumAddress(event.to), getChecksumAddress(account.getId()))
})
it('should be able to listen to events', async () => {
let validResolve = false
let subscription
const waitUntilEvent = new Promise((resolve) => {
subscription = nevermined.keeper.token.events.subscribe(
(events) => {
assert.isDefined(events)
assert.isAtLeast(events.length, 1)
if (validResolve) {
resolve(0)
}
},
{
eventName: 'Transfer',
filterSubgraph: {
where: {
to: account.getId(),
},
},
result: {
to: true,
value: true,
},
},
)
})
await Promise.all([executeTransaction()])
// TODO: See if we can remove this
await sleep(2000)
validResolve = true
await Promise.all([executeTransaction()])
await waitUntilEvent
subscription.unsubscribe()
})
it('should listen to event only once', async () => {
const event = nevermined.keeper.token.events
let canBeRejected = false
const waitUntilEvent = new Promise((resolve, reject) => {
event.once(
() => {
if (canBeRejected) {
reject(new Error(''))
}
canBeRejected = true
setTimeout(resolve, 600)
},
{
eventName: 'Transfer',
filterSubgraph: {
where: {
to: account.getId(),
},
},
result: {
to: true,
value: true,
},
},
)
})
await executeTransaction()
// TODO: See if we can remove this
await sleep(2000)
await executeTransaction()
await waitUntilEvent
// TODO: See if we can remove this
await sleep(2000)
})
it('should get the event like a promise', async () => {
const event = nevermined.keeper.token.events
const waitUntilEvent = event.once((events) => events, {
eventName: 'Transfer',
filterSubgraph: {
where: {
to: account.getId(),
},
},
result: {
to: true,
value: true,
},
})
// TODO: See if we can remove this
await sleep(400)
await executeTransaction()
await waitUntilEvent
})
it('once should not return unless there is an event', async () => {
// non-existent provId
const provId = `0x${generateId()}`
const resultPromise = nevermined.keeper.didRegistry.events.once((e) => e, {
eventName: 'ProvenanceAttributeRegistered',
filterSubgraph: { where: { provId: provId } },
result: {
id: true,
provId: true,
_did: true,
_agentId: true,
_activityId: true,
_relatedDid: true,
_agentInvolvedId: true,
_method: true,
_attributes: true,
_blockNumberUpdated: true,
},
})
await mineBlocks(nevermined, account, 1)
await assert.isRejected(Promise.race([resultPromise, awaitTimeout(2000)]), /Timeout/)
})
})