-
Notifications
You must be signed in to change notification settings - Fork 16
/
Dispenser.test.ts
483 lines (443 loc) · 16.4 KB
/
Dispenser.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import {
DatatokenCreateParams,
NftFactory,
NftCreateData,
sleep,
ZERO_ADDRESS,
Dispenser,
Datatoken,
DispenserParams
} from '@oceanprotocol/lib'
import DispenserTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import { assert } from 'chai'
import Web3 from 'web3'
import { homedir } from 'os'
import fs from 'fs'
import { fetch } from 'cross-fetch'
import { TransactionReceipt } from 'web3-core'
import { AbiItem } from 'web3-utils/types'
const sleepMs = 1800
const data = JSON.parse(
fs.readFileSync(
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
)
)
const addresses = data.development
const web3 = new Web3('http://127.0.0.1:8545')
const subgraphUrl =
'http://127.0.0.1:9000/subgraphs/name/oceanprotocol/ocean-subgraph'
describe('Dispenser tests', async () => {
const nftName = 'test-Fixed-Price-NFT'
const nftSymbol = 'TST-FIXED'
const tokenURI = 'https://oceanprotocol.com/nft/fixed'
const cap = '10000'
const feeAmount = '0.2'
const templateIndex = 1
const dispenserAddress = addresses.Dispenser.toLowerCase()
let dtAddress: string
let marketPlaceFeeAddress: string
let dt
let Factory: NftFactory
let factoryAddress: string
let accounts: string[]
let publisher: string
let nftAddress: string
let time: number
let blockNumber: number
let dispenser: Dispenser
let dispenserId: string
let datatoken: Datatoken
let user1: string
let user2: string
before(async () => {
factoryAddress = addresses.ERC721Factory.toLowerCase()
Factory = new NftFactory(factoryAddress, web3)
accounts = await web3.eth.getAccounts()
publisher = accounts[0].toLowerCase()
marketPlaceFeeAddress = accounts[1].toLowerCase()
user1 = accounts[2].toLowerCase()
user2 = accounts[3].toLowerCase()
})
it('should initialize Dispenser and datatoken class', async () => {
dispenser = new Dispenser(
addresses.Dispenser,
web3,
8996,
null,
DispenserTemplate.abi as AbiItem[]
)
assert(dispenser.getDefaultAbi() !== null)
datatoken = new Datatoken(web3, 8996, null, ERC20Template.abi as AbiItem[])
assert(datatoken.getDefaultAbi() !== null)
})
it('Deploying an NFT with ERC20', async () => {
const date = new Date()
time = Math.floor(date.getTime() / 1000)
blockNumber = await web3.eth.getBlockNumber()
const nftParams: NftCreateData = {
name: nftName,
symbol: nftSymbol,
templateIndex: 1,
tokenURI,
transferable: true,
owner: publisher
}
const erc20Params: DatatokenCreateParams = {
templateIndex,
cap,
feeAmount,
paymentCollector: ZERO_ADDRESS,
feeToken: ZERO_ADDRESS,
minter: publisher,
mpFeeAddress: marketPlaceFeeAddress
}
const tx = await Factory.createNftWithDatatoken(
publisher,
nftParams,
erc20Params
)
assert(tx.events.NFTCreated.event === 'NFTCreated')
assert(tx.events.TokenCreated.event === 'TokenCreated')
nftAddress = tx.events.NFTCreated.returnValues.newTokenAddress.toLowerCase()
dtAddress =
tx.events.TokenCreated.returnValues.newTokenAddress.toLowerCase()
// Check NFT values
await sleep(sleepMs)
const nftQuery = {
query: `query {
nft(id:"${nftAddress}"){
id,
symbol,
name,
tokenUri,
owner,
creator,
address,
providerUrl,
assetState,
managerRole,
erc20DeployerRole,
storeUpdateRole,
metadataRole,
template,
transferable,
createdTimestamp,
tx,
block,
orderCount}}`
}
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(nftQuery)
})
const nft = (await initialResponse.json()).data.nft
const nftTx: TransactionReceipt = await web3.eth.getTransactionReceipt(
nft.tx
)
assert(nft.id === nftAddress, 'incorrect value for: id')
assert(nft.symbol === nftSymbol, 'incorrect value for: symbol')
assert(nft.name === nftName, 'incorrect value for: name')
assert(nft.tokenUri === tokenURI, 'incorrect value for: tokenUri')
assert(nft.owner === publisher, 'incorrect value for: owner')
assert(nft.creator === publisher, 'incorrect value for: creator')
assert(nft.managerRole[0] === publisher, 'incorrect value for: managerRole')
assert(
nft.erc20DeployerRole[0] === factoryAddress,
'incorrect value for: erc20DeployerRole'
)
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
assert(nft.template === '', 'incorrect value for: template')
assert(nft.transferable === true, 'incorrect value for: transferable')
assert(nft.createdTimestamp >= time, 'incorrect value: createdTimestamp')
assert(nft.createdTimestamp < time + 5, 'incorrect value: createdTimestamp')
assert(nftTx.from === publisher, 'incorrect value for: tx')
assert(nftTx.to === factoryAddress, 'incorrect value for: tx')
assert(nftTx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(nft.block >= blockNumber, 'incorrect value for: block')
assert(nft.block < blockNumber + 50, 'incorrect value for: block')
assert(nft.orderCount === '0', 'incorrect value for: orderCount')
})
it('Test all DT Fields after deploying', async () => {
// Check Datatoken Values
const dtQuery = {
query: `query {
token(id: "${dtAddress}"){
id,
symbol,
name,
decimals,
address,
cap,
supply,
isDatatoken,
nft {id},
minter,
paymentManager,
paymentCollector,
publishMarketFeeAddress,
publishMarketFeeAmount,
templateId,
holderCount,
orderCount,
orders {id},
fixedRateExchanges {id},
dispensers {id},
createdTimestamp,
tx,
block,
lastPriceValue
}}`
}
const dtResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(dtQuery)
})
dt = (await dtResponse.json()).data.token
const dtTx: TransactionReceipt = await web3.eth.getTransactionReceipt(dt.tx)
assert(dt.id === dtAddress, 'incorrect value for: id')
assert(dt.symbol, 'incorrect value for: symbol')
assert(dt.name, 'incorrect value for: name')
assert(dt.decimals === 18, 'incorrect value for: decimals')
assert(dt.address === dtAddress, 'incorrect value for: address')
assert(dt.cap === cap, 'incorrect value for: cap')
assert(dt.supply === '0', 'incorrect value for: supply')
assert(dt.isDatatoken === true, 'incorrect value for: isDatatoken')
assert(dt.nft.id === nftAddress, 'incorrect value for: nft.id')
assert(dt.minter[0] === publisher, 'incorrect value for: minter')
assert(dt.paymentManager === null, 'incorrect value for: paymentManager')
assert(
dt.paymentCollector === null,
'incorrect value for: paymentCollector'
)
assert(
dt.publishMarketFeeAddress === marketPlaceFeeAddress,
'incorrect value for: publishMarketFeeAddress'
)
assert(
dt.publishMarketFeeAmount === feeAmount,
'incorrect value for: publishMarketFeeAmount'
)
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
assert(dt.orders, 'incorrect value for: orders')
assert(dt.fixedRateExchanges, 'incorrect value for: fixedRateExchanges')
assert(dt.dispensers, 'incorrect value for: dispensers')
assert(dt.createdTimestamp >= time, 'incorrect value for: createdTimestamp')
assert(
dt.createdTimestamp < time + 5,
'incorrect value for: createdTimestamp'
)
assert(dtTx.from === publisher, 'incorrect value for: tx')
assert(dtTx.to === factoryAddress, 'incorrect value for: tx')
assert(dtTx.blockNumber >= blockNumber, 'incorrect value for: tx')
assert(dt.block >= blockNumber, 'incorrect value for: block')
assert(dt.block < blockNumber + 50, 'incorrect value for: block')
assert(dt.lastPriceValue === '0', 'incorrect value for: lastPriceValue')
})
it('Make user1 minter', async () => {
await datatoken.addMinter(dtAddress, publisher, user1)
assert((await datatoken.getPermissions(dtAddress, user1)).minter === true)
await sleep(sleepMs)
const minterQuery = {
query: `query {token(id: "${dtAddress}"){minter{id}}}`
}
const minterResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(minterQuery)
})
const minter = (await minterResponse.json()).data.token.minter
assert(minter[1] === user1, 'incorrect value for: minter')
})
it('Create dispenser', async () => {
const maxTokens = '921'
const maxBalance = '9032'
const dispenserParams: DispenserParams = {
maxTokens,
maxBalance,
withMint: true
}
const tx = (
await datatoken.createDispenser(
dtAddress,
publisher,
dispenserAddress,
dispenserParams
)
).events.NewDispenser
await sleep(sleepMs)
assert(tx, 'Cannot create dispenser')
dispenserId = `${dispenserAddress}-${dtAddress}`
const dispenserQuery = {
query: `query {dispenser(id: "${dispenserId}"){
id
contract
active
owner
token {
id
}
allowedSwapper
isMinter
maxTokens
maxBalance
balance
block
createdTimestamp
tx
dispenses {
id
}
__typename
}}`
}
const graphResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(dispenserQuery)
})
const response = (await graphResponse.json()).data.dispenser
assert(response.id === dispenserId, 'incorrect value for: id')
assert(response.contract === dispenserAddress, 'incorrect value: contract')
assert(response.active === true, 'incorrect value for: active')
assert(response.owner === publisher, 'incorrect value for: owner')
assert(response.token.id === dtAddress, 'incorrect value for: token.id')
assert(response.allowedSwapper === ZERO_ADDRESS, 'incorrect allowedSwapper')
assert(response.isMinter === true, 'incorrect value for: isMinter')
assert(response.maxTokens === web3.utils.fromWei(maxTokens), 'maxTokens')
assert(response.maxBalance === web3.utils.fromWei(maxBalance), 'maxBalance')
assert(response.balance === '0', 'maxBalance')
assert(response.block === tx.blockNumber, 'wrong block')
assert(response.createdTimestamp >= time, 'incorrect: createdTimestamp')
assert(response.createdTimestamp < time + 15, 'incorrect: createdTimestamp')
assert(response.tx === tx.transactionHash, 'incorrect value for: tx')
assert(response.dispenses.length === 0, 'incorrect value for: dispenses')
assert(response.__typename === 'Dispenser', 'incorrect value: __typename')
})
it('Deactivates dispenser', async () => {
const deactiveQuery = {
query: `query {dispenser(id: "${dispenserId}"){active}}`
}
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(deactiveQuery)
})
const initialActive = (await initialResponse.json()).data.dispenser.active
assert(initialActive === true, 'incorrect value for: initialActive')
// Deactivate exchange
await dispenser.deactivate(dtAddress, publisher)
const status = await dispenser.status(dtAddress)
assert(status.active === false, 'Dispenser is still active')
await sleep(sleepMs)
// Check the updated value for active
const updatedResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(deactiveQuery)
})
const updatedActive = (await updatedResponse.json()).data.dispenser.active
assert(updatedActive === false, 'incorrect value for: updatedActive')
})
it('Activates exchange', async () => {
const activeQuery = {
query: `query {dispenser(id: "${dispenserId}"){active}}`
}
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(activeQuery)
})
const initialActive = (await initialResponse.json()).data.dispenser.active
assert(initialActive === false, 'incorrect value for: initialActive')
// Activate exchange
await dispenser.activate(dtAddress, '100', '100', publisher)
await sleep(sleepMs)
// Check the updated value for active
const updatedResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(activeQuery)
})
const updatedActive = (await updatedResponse.json()).data.dispenser.active
assert(updatedActive === true, 'incorrect value for: updatedActive')
})
it('User2 gets datatokens from the dispenser', async () => {
const amount = '3'
const dispenseQuery = {
query: `query {dispenser(id: "${dispenserId}"){
dispenses{
id
dispenser{id}
user {id}
amount
block
createdTimestamp
tx
__typename
}}}`
}
// Check initial values before dispense
const response1 = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(dispenseQuery)
})
const before = (await response1.json()).data.dispenser.dispenses
assert(before.length === 0, 'incorrect value for: dispenses')
const tx = await dispenser.dispense(dtAddress, user2, amount, user2)
await sleep(sleepMs)
// Check values after dispense
const response2 = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(dispenseQuery)
})
const dispense = (await response2.json()).data.dispenser.dispenses[0]
assert(dispense.id === `${tx.transactionHash}-${dispenserId}`, 'wrong id')
assert(dispense.dispenser.id === dispenserId, 'incorrect value for: user')
assert(dispense.user.id === user2, 'incorrect value for: user')
assert(dispense.amount === amount, 'incorrect value for: user')
assert(dispense.block === tx.blockNumber, 'incorrect value for: block')
assert(dispense.createdTimestamp >= time, 'incorrect: createdTimestamp')
assert(dispense.createdTimestamp < time + 15, 'incorrect: createdTimestamp')
assert(dispense.tx === tx.transactionHash, 'incorrect value for: tx')
assert(dispense.__typename === 'DispenserTransaction', 'wrong __typename')
})
it('Owner withdraws all of the datatokens', async () => {
await dispenser.ownerWithdraw(dtAddress, publisher)
await sleep(sleepMs)
// Check balance after owner withdraw
const balanceQuery = {
query: `query {dispenser(id: "${dispenserId}"){balance}}`
}
const response = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(balanceQuery)
})
const balance = (await response.json()).data.dispenser.balance
assert(balance === '0', 'incorrect value for: balance')
})
it('Updates allowed swapper', async () => {
const swapperQuery = {
query: `query {dispenser(id: "${dispenserId}"){allowedSwapper}}`
}
// Check initial allowedSwapper
const swapperResponse1 = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(swapperQuery)
})
const allowedSwapper1 = (await swapperResponse1.json()).data.dispenser
.allowedSwapper
assert(
allowedSwapper1 === ZERO_ADDRESS,
'incorrect value for: allowedSwapper'
)
await dispenser.setAllowedSwapper(dtAddress, publisher, user1)
await sleep(sleepMs)
const swapperResponse2 = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(swapperQuery)
})
const allowedSwapper2 = (await swapperResponse2.json()).data.dispenser
.allowedSwapper
assert(allowedSwapper2 === user1, 'incorrect value for: allowedSwapper 2')
})
})