-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContractEvent.test.ts
143 lines (117 loc) Β· 3.88 KB
/
ContractEvent.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
import chai, { assert } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import config from '../../test/config'
import { mineBlocks, awaitTimeout } from '../utils/utils'
import { NvmAccount } from '../../src/models/NvmAccount'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { ContractEvent } from '../../src/events/ContractEvent'
import { getChecksumAddress } from '../../src/nevermined/utils/BlockchainViemUtils'
import { generateId } from '../../src/common/helpers'
chai.use(chaiAsPromised)
describe('ContractEvent', () => {
let account: NvmAccount
let account2: NvmAccount
let account3: NvmAccount
let account4: NvmAccount
let account5: NvmAccount
let account6: NvmAccount
let nevermined: Nevermined
before(async () => {
nevermined = await Nevermined.getInstance({ ...config, graphHttpUri: undefined })
;[account, account2, account3, account4, account5, account6] = nevermined.accounts.list()
await nevermined.accounts.requestTokens(account, 1n)
})
it('should get a ContractEvent instance', async () => {
assert.instanceOf(nevermined.keeper.token.events, ContractEvent)
})
it('should query for the event', async () => {
const response = await nevermined.keeper.token.events.getEventData({
filterJsonRpc: {
to: account.getId(),
},
result: {
to: true,
value: true,
},
eventName: 'Transfer',
})
assert.strictEqual(
getChecksumAddress(response.pop().args.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',
filterJsonRpc: { to: account.getId() },
fromBlock: 0n,
toBlock: 'latest',
},
)
})
// await Promise.all([executeTransaction()])
await nevermined.accounts.requestTokens(account2, 1n)
validResolve = true
await nevermined.accounts.requestTokens(account3, 1n)
// await Promise.all([executeTransaction()])
await waitUntilEvent
subscription.unsubscribe()
})
it('should listen to event only once', async () => {
const to = account.getId()
const event = nevermined.keeper.token.events
let canBeRejected = false
const waitUntilEvent = new Promise((resolve, reject) => {
event.once(
() => {
if (canBeRejected) {
reject(new Error(''))
}
setTimeout(resolve, 600)
},
{
eventName: 'Transfer',
filterJsonRpc: {
to,
},
},
)
})
await nevermined.accounts.requestTokens(account4, 1n)
canBeRejected = true
await nevermined.accounts.requestTokens(account5, 1n)
await waitUntilEvent
})
it('should get the event like a promise', async () => {
const to = account.getId()
const event = nevermined.keeper.token.events
const waitUntilEvent = event.once((events) => events, {
eventName: 'Transfer',
filterJsonRpc: { to },
})
await nevermined.accounts.requestTokens(account6, 1n)
// 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',
filterJsonRpc: { provId },
})
await mineBlocks(nevermined, account, 1)
await assert.isRejected(Promise.race([resultPromise, awaitTimeout(2000)]), /Timeout/)
})
})