-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnftwatchdao.spec.ts
434 lines (423 loc) · 20.8 KB
/
nftwatchdao.spec.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
import { expect } from 'chai';
import { Account, Blockchain, expectToThrow, mintTokens, nameToBigInt, symbolCodeToBigInt } from '@proton/vert';
import { Asset, Name } from '@greymass/eosio';
import {
COLLECTION_CYPHER_GANG,
COLLECTION_PIXELHEROES,
TOKEN_XPR,
eosio_assert,
getTokenAmountActionParam,
} from '../helpers/common.ts';
import { createTestCollection } from '../helpers/atomicassets.helper.ts';
import {
ERROR_ACCOUNT_NOT_EXISTS,
ERROR_COLLECTION_ALREADY_BLACKLISTED,
ERROR_COLLECTION_ALREADY_REPORTED,
ERROR_COLLECTION_NOT_BLACKLISTED,
ERROR_COLLECTION_NOT_EXISTS,
ERROR_COLLECTION_NOT_REPORTED,
ERROR_COLLECTION_NOT_SHIELDED,
ERROR_GUARD_ALREADY_AUTHORIZED,
ERROR_INSUFFICIENT_SHIELDING_BALANCE,
ERROR_INVALID_CID,
ERROR_INVALID_FEE_STRUCTURE,
ERROR_INVALID_MARKETPLACE,
ERROR_INVALID_SHIELDING_PRICE,
ERROR_INVALID_SYMBOL_ONLY_XPR_ALLOWED,
ERROR_MARKETPLACE_ALREADY_REGISTERED,
ERROR_MISSING_SHIELDING_BALANCE,
ERROR_SHIELDING_REQUEST_NOT_EXISTS,
} from './nftwatchdao.constants.ts';
import { Blacklist, BlacklistReport, Globals, Shielding, ShieldingRequest } from './nftwatchdao.tables.ts';
const blockchain = new Blockchain();
// deploy contract to test
const nftwatchdao = blockchain.createContract('nftwatchdao', 'nftwatchdao/target/nftwatchdao.contract');
// deploy contracts required for testing
const eosioToken = blockchain.createContract('eosio.token', 'node_modules/proton-tsc/external/eosio.token/eosio.token');
const atomicassets = blockchain.createContract('atomicassets', 'external/atomicassets/atomicassets');
// globals table
const getGlobals = (): Globals => nftwatchdao.tables.globals().getTableRows()[0];
// create accounts
const [soonmarket, protonpunk, pixelheroes, reporter, requester, stonebreaker, randomacc] = blockchain.createAccounts(
'soonmarket',
'protonpunk',
'pixelheroes',
'reporter',
'requester',
'stonebreaker',
'randomacc',
);
let initSnapshot: number;
// helpers
const initContracts = async (...contracts: Array<Account>): Promise<void> => {
for (const contract of contracts) {
await contract.actions.init().send();
}
};
const getAccountXprBalance = (account: Account) => {
const accountBigInt = nameToBigInt(account.name);
const symcodeBigInt = symbolCodeToBigInt(Asset.SymbolCode.from('XPR'));
return eosioToken.tables.accounts(accountBigInt).getTableRow(symcodeBigInt);
};
before(async () => {
// init contracts
await initContracts(atomicassets);
// add stonebreaker as authorized guard
await nftwatchdao.actions.addguard([stonebreaker.name]).send(`${nftwatchdao.name}@active`);
// mint xpr
await mintTokens(eosioToken, TOKEN_XPR.name, TOKEN_XPR.precision, 100_000_000.0, 10_000_000, [requester]);
// create collections
await createTestCollection(atomicassets, protonpunk);
await createTestCollection(atomicassets, pixelheroes);
initSnapshot = blockchain.store.snapshot();
});
beforeEach(async () => {
blockchain.store.revertTo(initSnapshot);
});
describe('NftWatchDAO', () => {
describe('blacklisting', () => {
it('reject blacklisting', async () => {
await nftwatchdao.actions
.report([COLLECTION_PIXELHEROES, reporter.name, 'creator left the space'])
.send(`${reporter.name}@active`);
let reportEntry: BlacklistReport = nftwatchdao.tables
.blacklistrep()
.getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(reportEntry).not.undefined;
await nftwatchdao.actions
.rejectreport([COLLECTION_PIXELHEROES, stonebreaker.name, 'not a valid reason to blacklist'])
.send(`${stonebreaker.name}@active`);
reportEntry = nftwatchdao.tables.blacklistrep().getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(reportEntry).undefined;
expect(getGlobals().blacklistCount).equal(0);
});
it('confirm & delete blacklisting', async () => {
const REPORT_REASON = 'fake copy scam';
const GUARD_COMMENT = 'confirming a fake copy scam';
await nftwatchdao.actions
.report([COLLECTION_PIXELHEROES, reporter.name, REPORT_REASON])
.send(`${reporter.name}@active`);
let reportEntry = nftwatchdao.tables.blacklistrep().getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(reportEntry).not.undefined;
await nftwatchdao.actions
.confirmrep([COLLECTION_PIXELHEROES, stonebreaker.name, GUARD_COMMENT])
.send(`${stonebreaker.name}@active`);
reportEntry = nftwatchdao.tables.blacklistrep().getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(reportEntry).undefined;
let blacklistEntry: Blacklist = nftwatchdao.tables
.blacklist()
.getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(blacklistEntry).not.undefined;
expect(blacklistEntry.reportReason).equal(REPORT_REASON);
expect(blacklistEntry.reportedBy).equal(reporter.name.toString());
expect(blacklistEntry.guardComment).equal(GUARD_COMMENT);
expect(blacklistEntry.confirmedBy).equal(stonebreaker.name.toString());
expect(getGlobals().blacklistCount).equal(1);
await nftwatchdao.actions
.delblacklist([
COLLECTION_PIXELHEROES,
stonebreaker.name,
'fake copies have been burned. collection is clean.',
])
.send(`${stonebreaker.name}@active`);
blacklistEntry = nftwatchdao.tables.blacklist().getTableRow(nameToBigInt(COLLECTION_PIXELHEROES));
expect(blacklistEntry).undefined;
expect(getGlobals().blacklistCount).equal(0);
});
});
describe('shielding', () => {
it('reject shielding', async () => {
const SHIELDING_PRICE = 12500;
await eosioToken.actions
.transfer([
requester.name,
nftwatchdao.name,
getTokenAmountActionParam(SHIELDING_PRICE, TOKEN_XPR),
'shielding',
])
.send(`${requester.name}@active`);
await nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`);
let requestEntry: ShieldingRequest = nftwatchdao.tables
.shieldingreq()
.getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(requestEntry).not.undefined;
// check fee distribution after request
let guardAcc = getAccountXprBalance(stonebreaker);
let marketAcc = getAccountXprBalance(soonmarket);
let daoAcc = getAccountXprBalance(nftwatchdao);
expect(guardAcc).undefined;
expect(marketAcc.balance).equal(Asset.from((SHIELDING_PRICE * 10) / 100, '4,XPR').toString());
expect(daoAcc.balance).equal(Asset.from((SHIELDING_PRICE * 90) / 100, '4,XPR').toString());
await nftwatchdao.actions
.rejectshield([COLLECTION_CYPHER_GANG, stonebreaker.name, 'Qm...'])
.send(`${stonebreaker.name}@active`);
requestEntry = nftwatchdao.tables.shieldingreq().getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(requestEntry).undefined;
const shieldingEntry = nftwatchdao.tables.shieldings().getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(shieldingEntry).undefined;
expect(getGlobals().shieldCount).equal(0);
// check fee distribution after rejection
guardAcc = getAccountXprBalance(stonebreaker);
marketAcc = getAccountXprBalance(soonmarket);
daoAcc = getAccountXprBalance(nftwatchdao);
expect(guardAcc.balance).equal(Asset.from((SHIELDING_PRICE * 50) / 100, '4,XPR').toString());
expect(marketAcc.balance).equal(Asset.from((SHIELDING_PRICE * 10) / 100, '4,XPR').toString());
expect(daoAcc.balance).equal(Asset.from((SHIELDING_PRICE * 40) / 100, '4,XPR').toString());
});
it('confirm & delete shielding', async () => {
const SHIELDING_PRICE = 12500;
await eosioToken.actions
.transfer([
requester.name,
nftwatchdao.name,
getTokenAmountActionParam(SHIELDING_PRICE, TOKEN_XPR),
'shielding',
])
.send(`${requester.name}@active`);
await nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`);
let requestEntry: ShieldingRequest = nftwatchdao.tables
.shieldingreq()
.getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(requestEntry).not.undefined;
// check fee distribution after request
let guardAcc = getAccountXprBalance(stonebreaker);
let marketAcc = getAccountXprBalance(soonmarket);
let daoAcc = getAccountXprBalance(nftwatchdao);
expect(guardAcc).undefined;
expect(marketAcc.balance).equal(Asset.from((SHIELDING_PRICE * 10) / 100, '4,XPR').toString());
expect(daoAcc.balance).equal(Asset.from((SHIELDING_PRICE * 90) / 100, '4,XPR').toString());
await nftwatchdao.actions
.confshield([COLLECTION_CYPHER_GANG, stonebreaker.name, 'bafy...'])
.send(`${stonebreaker.name}@active`);
requestEntry = nftwatchdao.tables.shieldingreq().getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(requestEntry).undefined;
let shieldingEntry: Shielding = nftwatchdao.tables
.shieldings()
.getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(shieldingEntry).not.undefined;
expect(shieldingEntry.requestedBy).equal(requester.name.toString());
expect(shieldingEntry.confirmedBy).equal(stonebreaker.name.toString());
expect(shieldingEntry.reportCid).equal('bafy...');
expect(getGlobals().shieldCount).equal(1);
// check fee distribution after confirmation
guardAcc = getAccountXprBalance(stonebreaker);
marketAcc = getAccountXprBalance(soonmarket);
daoAcc = getAccountXprBalance(nftwatchdao);
expect(guardAcc.balance).equal(Asset.from((SHIELDING_PRICE * 50) / 100, '4,XPR').toString());
expect(marketAcc.balance).equal(Asset.from((SHIELDING_PRICE * 10) / 100, '4,XPR').toString());
expect(daoAcc.balance).equal(Asset.from((SHIELDING_PRICE * 40) / 100, '4,XPR').toString());
// delete shielding
await nftwatchdao.actions
.delshielding([
COLLECTION_CYPHER_GANG,
stonebreaker.name,
'community is concerned about the project. shielding must be revoked',
])
.send(`${stonebreaker.name}@active`);
shieldingEntry = nftwatchdao.tables.shieldings().getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(shieldingEntry).undefined;
expect(getGlobals().shieldCount).equal(0);
});
it('add shielding directly without request', async () => {
await nftwatchdao.actions
.addshielding([
COLLECTION_CYPHER_GANG,
stonebreaker.name,
'has been shielded already before decentralized process',
'Qm...',
])
.send(`${stonebreaker.name}@active`);
let shieldingEntry: Shielding = nftwatchdao.tables
.shieldings()
.getTableRow(nameToBigInt(COLLECTION_CYPHER_GANG));
expect(shieldingEntry).not.undefined;
expect(shieldingEntry.requestedBy).equal('');
expect(shieldingEntry.confirmedBy).equal(stonebreaker.name.toString());
expect(shieldingEntry.reportCid).equal('Qm...');
expect(getGlobals().shieldCount).equal(1);
});
});
describe('revert paths', () => {
it('set invalid shielding price', async () => {
// wrong asset
await expectToThrow(
nftwatchdao.actions.setshieldprc([Asset.from('5000.0000 LOAN')]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_INVALID_SYMBOL_ONLY_XPR_ALLOWED),
);
// wrong precision
await expectToThrow(
nftwatchdao.actions.setshieldprc([Asset.from('25000.00001 XPR')]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_INVALID_SYMBOL_ONLY_XPR_ALLOWED),
);
});
it('set invalid fee structure', async () => {
// too low
await expectToThrow(
nftwatchdao.actions.setfeestruct([20, 20, 59]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_INVALID_FEE_STRUCTURE),
);
// too high
await expectToThrow(
nftwatchdao.actions.setfeestruct([20, 20, 61]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_INVALID_FEE_STRUCTURE),
);
});
it('add non existing guard & marketplace', async () => {
await expectToThrow(
nftwatchdao.actions.addguard([Name.from('notexists')]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_ACCOUNT_NOT_EXISTS),
);
await expectToThrow(
nftwatchdao.actions.addmarket([Name.from('notexists')]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_ACCOUNT_NOT_EXISTS),
);
});
it('add a guard and marketplace twice', async () => {
// stonebreaker has been added in initial setup
await expectToThrow(
nftwatchdao.actions.addguard([stonebreaker.name]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_GUARD_ALREADY_AUTHORIZED),
);
// soonmarket is added by default
await expectToThrow(
nftwatchdao.actions.addmarket([soonmarket.name]).send(`${nftwatchdao.name}@active`),
eosio_assert(ERROR_MARKETPLACE_ALREADY_REGISTERED),
);
});
it('request shield & report for non existing collection', async () => {
await expectToThrow(
nftwatchdao.actions
.reqshielding(['notexisits', requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`),
eosio_assert(ERROR_COLLECTION_NOT_EXISTS),
);
await expectToThrow(
nftwatchdao.actions.report(['notexisits', reporter.name, '']).send(`${reporter.name}@active`),
eosio_assert(ERROR_COLLECTION_NOT_EXISTS),
);
});
it('request shield & report for already blacklisted collection', async () => {
await nftwatchdao.actions
.addblacklist([COLLECTION_PIXELHEROES, stonebreaker.name, '1337 stuff'])
.send(`${stonebreaker.name}@active`);
await expectToThrow(
nftwatchdao.actions
.reqshielding([COLLECTION_PIXELHEROES, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`),
eosio_assert(ERROR_COLLECTION_ALREADY_BLACKLISTED),
);
await expectToThrow(
nftwatchdao.actions.report([COLLECTION_PIXELHEROES, reporter.name, '']).send(`${reporter.name}@active`),
eosio_assert(ERROR_COLLECTION_ALREADY_BLACKLISTED),
);
});
it('report already reported collection', async () => {
await nftwatchdao.actions
.report([COLLECTION_PIXELHEROES, reporter.name, ''])
.send(`${reporter.name}@active`);
await expectToThrow(
nftwatchdao.actions.report([COLLECTION_PIXELHEROES, reporter.name, '']).send(`${reporter.name}@active`),
eosio_assert(ERROR_COLLECTION_ALREADY_REPORTED),
);
});
it('confirm a not reported collection', async () => {
await expectToThrow(
nftwatchdao.actions
.confirmrep([COLLECTION_PIXELHEROES, stonebreaker.name, ''])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_COLLECTION_NOT_REPORTED),
);
});
it('delete a not blacklisted collection', async () => {
await expectToThrow(
nftwatchdao.actions
.delblacklist([COLLECTION_PIXELHEROES, stonebreaker.name, ''])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_COLLECTION_NOT_BLACKLISTED),
);
});
it('request shield with invalid marketplace', async () => {
await expectToThrow(
nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, 'digitalgalaxy', false, ''])
.send(`${requester.name}@active`),
eosio_assert(ERROR_INVALID_MARKETPLACE),
);
});
it('request shield with missing balance', async () => {
await expectToThrow(
nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`),
eosio_assert(ERROR_MISSING_SHIELDING_BALANCE),
);
});
it('request shield with wrong shielding price', async () => {
await expectToThrow(
eosioToken.actions
.transfer([
requester.name,
nftwatchdao.name,
getTokenAmountActionParam(12400, TOKEN_XPR),
'shielding',
])
.send(`${requester.name}@active`),
eosio_assert(ERROR_INVALID_SHIELDING_PRICE),
);
});
it('request shield with insufficient balance', async () => {
await eosioToken.actions
.transfer([requester.name, nftwatchdao.name, getTokenAmountActionParam(12500, TOKEN_XPR), 'shielding'])
.send(`${requester.name}@active`);
await nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`);
await expectToThrow(
nftwatchdao.actions
.reqshielding([COLLECTION_PIXELHEROES, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`),
eosio_assert(ERROR_INSUFFICIENT_SHIELDING_BALANCE),
);
});
it('confirm non existing shielding request', async () => {
await expectToThrow(
nftwatchdao.actions
.confshield([COLLECTION_CYPHER_GANG, stonebreaker.name, 'Qm...'])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_SHIELDING_REQUEST_NOT_EXISTS),
);
});
it('confirm / reject shielding with invalid reportCid', async () => {
await eosioToken.actions
.transfer([requester.name, nftwatchdao.name, getTokenAmountActionParam(12500, TOKEN_XPR), 'shielding'])
.send(`${requester.name}@active`);
await nftwatchdao.actions
.reqshielding([COLLECTION_CYPHER_GANG, requester.name, soonmarket.name, false, ''])
.send(`${requester.name}@active`);
await expectToThrow(
nftwatchdao.actions
.confshield([COLLECTION_CYPHER_GANG, stonebreaker.name, 'invalid reportCid'])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_INVALID_CID),
);
await expectToThrow(
nftwatchdao.actions
.rejectshield([COLLECTION_CYPHER_GANG, stonebreaker.name, 'invalid reportCid'])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_INVALID_CID),
);
});
it('delete a not shielded collection', async () => {
await expectToThrow(
nftwatchdao.actions
.delshielding([COLLECTION_PIXELHEROES, stonebreaker.name, ''])
.send(`${stonebreaker.name}@active`),
eosio_assert(ERROR_COLLECTION_NOT_SHIELDED),
);
});
});
});