Skip to content

Commit 21a4414

Browse files
added validaTetokenAmount test
1 parent e7ce838 commit 21a4414

1 file changed

Lines changed: 81 additions & 77 deletions

File tree

test/unit/KnowledgeCollection.test.ts

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -298,83 +298,6 @@ describe('@unit KnowledgeCollection', () => {
298298
expect(total).to.equal(tokenAmount);
299299
});
300300

301-
it('Should create a knowledge collection successfully and distribute tokens to epochs', async () => {
302-
const kcCreator = getDefaultKCCreator(accounts);
303-
const publishingNode = getDefaultPublishingNode(accounts);
304-
const receivingNodes = getDefaultReceivingNodes(accounts);
305-
306-
const contracts = {
307-
Profile,
308-
KnowledgeCollection,
309-
Token,
310-
};
311-
312-
const { identityId: publishingNodeIdentityId } = await createProfile(
313-
contracts.Profile,
314-
publishingNode,
315-
);
316-
const receivingNodesIdentityIds = (
317-
await createProfiles(contracts.Profile, receivingNodes)
318-
).map((p) => p.identityId);
319-
320-
let currentEpoch = await Chronos.getCurrentEpoch();
321-
console.log(`\n🏁 Current epoch ${currentEpoch}`);
322-
let tokenAmount = ethers.parseEther('100');
323-
let numberOfEpochs = 5;
324-
const { collectionId } = await createKnowledgeCollection(
325-
kcCreator,
326-
publishingNode,
327-
publishingNodeIdentityId,
328-
receivingNodes,
329-
receivingNodesIdentityIds,
330-
contracts,
331-
merkleRoot,
332-
'test-operation-id',
333-
10, // knowledgeAssetsAmount
334-
1000, // byteSize
335-
numberOfEpochs, // epochs
336-
tokenAmount, // tokenAmount
337-
false, // isImmutable
338-
ethers.ZeroAddress, // paymaster
339-
);
340-
341-
expect(collectionId).to.equal(1);
342-
343-
// Verify knowledge collection was created
344-
const metadata =
345-
await KnowledgeCollectionStorage.getKnowledgeCollectionMetadata(
346-
collectionId,
347-
);
348-
349-
expect(metadata[0][0].length).to.equal(receivingNodesIdentityIds.length); // merkle roots
350-
expect(metadata[1].length).to.equal(0); // burned
351-
expect(metadata[2]).to.equal(10); // minted
352-
expect(metadata[3]).to.equal(1000); // byteSize
353-
expect(metadata[4]).to.equal(currentEpoch); // startEpoch
354-
expect(metadata[5]).to.equal(currentEpoch + BigInt(numberOfEpochs)); // endEpoch
355-
expect(metadata[6]).to.equal(tokenAmount); // tokenAmount
356-
expect(metadata[7]).to.equal(false); // isImmutable
357-
358-
expect(await EpochStorage.getEpochPool(1, currentEpoch)).to.be.equal(
359-
tokenAmount / BigInt(numberOfEpochs),
360-
);
361-
expect(await EpochStorage.getEpochPool(1, currentEpoch + 1n)).to.be.equal(
362-
tokenAmount / BigInt(numberOfEpochs),
363-
);
364-
expect(await EpochStorage.getEpochPool(1, currentEpoch + 2n)).to.be.equal(
365-
tokenAmount / BigInt(numberOfEpochs),
366-
);
367-
expect(await EpochStorage.getEpochPool(1, currentEpoch + 3n)).to.be.equal(
368-
tokenAmount / BigInt(numberOfEpochs),
369-
);
370-
expect(await EpochStorage.getEpochPool(1, currentEpoch + 4n)).to.be.equal(
371-
tokenAmount / BigInt(numberOfEpochs),
372-
);
373-
expect(await EpochStorage.getEpochPool(1, currentEpoch + 5n)).to.be.equal(
374-
0,
375-
);
376-
});
377-
378301
it('Should revert if insufficient signatures provided', async () => {
379302
const kcCreator = getDefaultKCCreator(accounts);
380303
const publishingNode = getDefaultPublishingNode(accounts);
@@ -524,4 +447,85 @@ describe('@unit KnowledgeCollection', () => {
524447
const sum = pools.reduce((a, v) => a + v, 0n);
525448
expect(sum).to.equal(tokenAmount); // total check
526449
});
450+
451+
it('Should revert when tokenAmount is lower than _validateTokenAmount expects', async () => {
452+
/* ---------- environment setup ---------- */
453+
// Force stake-weighted average ASK to 20 tokens / epoch
454+
const ask = ethers.parseEther('20'); // 20 TRAC/epoch
455+
const stake = ethers.parseEther('1'); // 1 TRAC stake
456+
await AskStorage.connect(accounts[0]).setWeightedActiveAskSum(ask * stake); // 20 * 10^36
457+
await AskStorage.connect(accounts[0]).setTotalActiveStake(stake); // 1 * 10^18
458+
459+
const kcCreator = getDefaultKCCreator(accounts); // same sender as other tests
460+
const publishingNode = getDefaultPublishingNode(accounts);
461+
const receivingNodes = getDefaultReceivingNodes(accounts);
462+
463+
// Profiles
464+
const { identityId: publisherId } = await createProfile(
465+
Profile,
466+
publishingNode,
467+
);
468+
const receiverIds = (await createProfiles(Profile, receivingNodes)).map(
469+
(p) => p.identityId,
470+
);
471+
472+
// Signatures (publisher + receivers)
473+
const sig = await getKCSignaturesData(
474+
publishingNode,
475+
publisherId,
476+
receivingNodes,
477+
);
478+
479+
// Common params
480+
const byteSize = 1024; // 1 KB
481+
const epochs = 5; // 5 epochs
482+
const needTokens = ethers.parseEther('100'); // 20 * 5
483+
const fewTokens = ethers.parseEther('99'); // deliberately low
484+
485+
// Allow kcCreator to spend TRAC
486+
await Token.connect(kcCreator).increaseAllowance(
487+
KnowledgeCollection.getAddress(),
488+
needTokens,
489+
);
490+
491+
/* ---------- expect revert: tokenAmount too small ---------- */
492+
await expect(
493+
KnowledgeCollection.connect(kcCreator).createKnowledgeCollection(
494+
'validate-fail',
495+
sig.merkleRoot,
496+
1, // knowledgeAssetsAmount
497+
byteSize,
498+
epochs,
499+
fewTokens, // < expected
500+
false, // isImmutable
501+
ethers.ZeroAddress,
502+
publisherId,
503+
sig.publisherR,
504+
sig.publisherVS,
505+
receiverIds,
506+
sig.receiverRs,
507+
sig.receiverVSs,
508+
),
509+
).to.be.revertedWithCustomError(KnowledgeCollection, 'InvalidTokenAmount');
510+
511+
/* ---------- same call with correct tokenAmount should succeed ---------- */
512+
const { collectionId } = await createKnowledgeCollection(
513+
kcCreator,
514+
publishingNode,
515+
publisherId,
516+
receivingNodes,
517+
receiverIds,
518+
{ KnowledgeCollection, Token },
519+
sig.merkleRoot,
520+
'validate-pass',
521+
1, // knowledgeAssetsAmount
522+
byteSize,
523+
epochs,
524+
needTokens, // correct amount
525+
false,
526+
ethers.ZeroAddress,
527+
);
528+
529+
expect(collectionId).to.equal(1);
530+
});
527531
});

0 commit comments

Comments
 (0)