git.delta.rocks / unique-network / refs/commits / fe8844c28c10

difftreelog

Merge pull request #97 from usetech-llc/feature/NFTPAR-281_overflow_tests

Greg Zaitsev2021-02-12parents: #800a78f #22e2b6e.patch.diff
in: master
Fungible overflow tests

8 files changed

modifiedtests/package.jsondiffbeforeafterboth
40 "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",40 "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",
41 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",41 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
42 "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",42 "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
43 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"43 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
44 "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"
44 },45 },
45 "author": "",46 "author": "",
46 "license": "SEE LICENSE IN ../LICENSE",47 "license": "SEE LICENSE IN ../LICENSE",
modifiedtests/src/approve.test.tsdiffbeforeafterboth
12 approveExpectFail,12 approveExpectFail,
13 approveExpectSuccess,13 approveExpectSuccess,
14 createCollectionExpectSuccess,14 createCollectionExpectSuccess,
15 createFungibleItemExpectSuccess,
15 createItemExpectSuccess,16 createItemExpectSuccess,
16 destroyCollectionExpectSuccess,17 destroyCollectionExpectSuccess,
18 transferFromExpectSuccess,
19 U128_MAX,
17} from './util/helpers';20} from './util/helpers';
1821
19chai.use(chaiAsPromised);22chai.use(chaiAsPromised);
addedtests/src/overflow.test.tsdiffbeforeafterboth

no changes

modifiedtests/src/removeFromContractWhiteList.test.tsdiffbeforeafterboth
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
1import privateKey from "./substrate/privateKey";6import privateKey from "./substrate/privateKey";
2import usingApi from "./substrate/substrate-api";7import usingApi from "./substrate/substrate-api";
modifiedtests/src/substrate/substrate-api.tsdiffbeforeafterboth
18 return { provider: wsProvider, types: rtt };18 return { provider: wsProvider, types: rtt };
19}19}
2020
21export default async function usingApi(action: (api: ApiPromise) => Promise<void>, settings: ApiOptions | undefined = undefined): Promise<void> {21export default async function usingApi<T = void>(action: (api: ApiPromise) => Promise<T>, settings: ApiOptions | undefined = undefined): Promise<T> {
22 settings = settings || defaultApiOptions();22 settings = settings || defaultApiOptions();
23 let api: ApiPromise = new ApiPromise(settings);23 let api: ApiPromise = new ApiPromise(settings);
24 let result: T = null as unknown as T;
2425
25 // TODO: Remove, this is temporary: Filter unneeded API output 26 // TODO: Remove, this is temporary: Filter unneeded API output
26 // (Jaco promised it will be removed in the next version)27 // (Jaco promised it will be removed in the next version)
34 await promisifySubstrate(api, async () => {35 await promisifySubstrate(api, async () => {
35 if(api) {36 if (api) {
36 await api.isReadyOrError;37 await api.isReadyOrError;
37 await action(api);38 result = await action(api);
38 }39 }
39 })();40 })();
40 } finally {41 } finally {
41 await api.disconnect();42 await api.disconnect();
42 console.error = consoleErr;43 console.error = consoleErr;
43 }44 }
45 return result as T;
44}46}
4547
46enum TransactionStatus {48enum TransactionStatus {
modifiedtests/src/transferFrom.test.tsdiffbeforeafterboth
11 approveExpectFail,11 approveExpectFail,
12 approveExpectSuccess,12 approveExpectSuccess,
13 createCollectionExpectSuccess,13 createCollectionExpectSuccess,
14 createFungibleItemExpectSuccess,
14 createItemExpectSuccess,15 createItemExpectSuccess,
15 destroyCollectionExpectSuccess,16 destroyCollectionExpectSuccess,
17 getAllowance,
16 transferFromExpectFail,18 transferFromExpectFail,
17 transferFromExpectSuccess,19 transferFromExpectSuccess,
18 burnItemExpectSuccess,20 burnItemExpectSuccess,
49 });51 });
50 });52 });
53
54 it('Should reduce allowance if value is big', async () => {
55 await usingApi(async () => {
56 const alice = privateKey('//Alice');
57 const bob = privateKey('//Bob');
58 const charlie = privateKey('//Charlie');
59
60 // fungible
61 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
62 const newFungibleTokenId = await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: 500000n });
63
64 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob, 500000n);
65 await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 500000n, 'Fungible');
66 expect((await getAllowance(fungibleCollectionId, newFungibleTokenId, alice.address, bob.address)).toString()).to.equal('0');
67 });
68 });
51});69});
5270
53describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {71describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
21chai.use(chaiAsPromised);21chai.use(chaiAsPromised);
22const expect = chai.expect;22const expect = chai.expect;
23
24export const U128_MAX = (1n << 128n) - 1n;
2325
24type GenericResult = {26type GenericResult = {
25 success: boolean,27 success: boolean,
238 return unused;240 return unused;
239}241}
242
243export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {
244 return await usingApi(async (api) => {
245 const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;
246 return BigInt(bn.toString());
247 });
248}
240249
241export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {250export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {
242 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));251 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));
479 });488 });
480}489}
481490
482export interface CreateFungibleData extends Struct {491export interface CreateFungibleData {
483 readonly value: u128;492 readonly Value: bigint;
484}493}
485494
486export interface CreateReFungibleData extends Struct {}495export interface CreateReFungibleData { }
487export interface CreateNftData extends Struct {}496export interface CreateNftData { }
488497
489export interface CreateItemData extends Enum {498export type CreateItemData = {
490 NFT: CreateNftData;499 NFT: CreateNftData;
500} | {
491 Fungible: CreateFungibleData;501 Fungible: CreateFungibleData;
502} | {
492 ReFungible: CreateReFungibleData;503 ReFungible: CreateReFungibleData;
493}504};
494505
495export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {506export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {
496 await usingApi(async (api) => {507 await usingApi(async (api) => {
510521
511export async function522export async function
512approveExpectSuccess(collectionId: number,523approveExpectSuccess(collectionId: number,
513 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) { //alice,bob524 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {
514 await usingApi(async (api: ApiPromise) => {525 await usingApi(async (api: ApiPromise) => {
515 const allowanceBefore =526 const allowanceBefore =
516 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;527 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
521 expect(result.success).to.be.true;532 expect(result.success).to.be.true;
522 const allowanceAfter =533 const allowanceAfter =
523 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;534 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
524 expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);535 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());
525 });536 });
526}537}
527538
528export async function539export async function
529transferFromExpectSuccess(collectionId: number,540transferFromExpectSuccess(collectionId: number,
530 tokenId: number,541 tokenId: number,
531 accountApproved: IKeyringPair, //bob542 accountApproved: IKeyringPair,
532 accountFrom: IKeyringPair, //alice543 accountFrom: IKeyringPair,
533 accountTo: IKeyringPair, //charlie544 accountTo: IKeyringPair,
534 value: number = 1,545 value: number | bigint = 1,
535 type: string = 'NFT') {546 type: string = 'NFT') {
536 await usingApi(async (api: ApiPromise) => {547 await usingApi(async (api: ApiPromise) => {
537 let balanceBefore = new BN(0);548 let balanceBefore = new BN(0);
550 }561 }
551 if (type === 'Fungible') {562 if (type === 'Fungible') {
552 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;563 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
553 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);564 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());
554 }565 }
555 if (type === 'ReFungible') {566 if (type === 'ReFungible') {
556 const nftItemData =567 const nftItemData =
567 accountApproved: IKeyringPair,578 accountApproved: IKeyringPair,
568 accountFrom: IKeyringPair,579 accountFrom: IKeyringPair,
569 accountTo: IKeyringPair,580 accountTo: IKeyringPair,
570 value: number = 1) {581 value: number | bigint = 1) {
571 await usingApi(async (api: ApiPromise) => {582 await usingApi(async (api: ApiPromise) => {
572 const transferFromTx = await api.tx.nft.transferFrom(583 const transferFromTx = await api.tx.nft.transferFrom(
573 accountFrom.address, accountTo.address, collectionId, tokenId, value);584 accountFrom.address, accountTo.address, collectionId, tokenId, value);
583 tokenId: number,594 tokenId: number,
584 sender: IKeyringPair,595 sender: IKeyringPair,
585 recipient: IKeyringPair,596 recipient: IKeyringPair,
586 value: number = 1,597 value: number | bigint = 1,
587 type: string = 'NFT') {598 type: string = 'NFT') {
588 await usingApi(async (api: ApiPromise) => {599 await usingApi(async (api: ApiPromise) => {
589 let balanceBefore = new BN(0);600 let balanceBefore = new BN(0);
601 }612 }
602 if (type === 'Fungible') {613 if (type === 'Fungible') {
603 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;614 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;
604 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);615 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());
605 }616 }
606 if (type === 'ReFungible') {617 if (type === 'ReFungible') {
607 const nftItemData =618 const nftItemData =
617 tokenId: number,628 tokenId: number,
618 sender: IKeyringPair,629 sender: IKeyringPair,
619 recipient: IKeyringPair,630 recipient: IKeyringPair,
620 value: number = 1,631 value: number | bigint = 1,
621 type: string = 'NFT') {632 type: string = 'NFT') {
622 await usingApi(async (api: ApiPromise) => {633 await usingApi(async (api: ApiPromise) => {
623 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);634 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);
632643
633export async function644export async function
634approveExpectFail(collectionId: number,645approveExpectFail(collectionId: number,
635 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {646 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {
636 await usingApi(async (api: ApiPromise) => {647 await usingApi(async (api: ApiPromise) => {
637 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);648 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);
638 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;649 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;
642 });653 });
643}654}
655
656export async function getFungibleBalance(
657 collectionId: number,
658 owner: string,
659) {
660 return await usingApi(async (api) => {
661 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};
662 return BigInt(response.Value);
663 });
664}
665
666export async function createFungibleItemExpectSuccess(
667 sender: IKeyringPair,
668 collectionId: number,
669 data: CreateFungibleData,
670 owner: string = sender.address,
671) {
672 return await usingApi(async (api) => {
673 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });
674
675 const events = await submitTransactionAsync(sender, tx);
676 const result = getCreateItemResult(events);
677
678 expect(result.success).to.be.true;
679 return result.itemId;
680 });
681}
644682
645export async function createItemExpectSuccess(683export async function createItemExpectSuccess(
646 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {684 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
modifiedtests/yarn.lockdiffbeforeafterboth
4710 dependencies:4710 dependencies:
4711 ci-info "^2.0.0"4711 ci-info "^2.0.0"
47124712
4713is-core-module@^2.1.0:4713is-core-module@^2.2.0:
4714 version "2.2.0"4714 version "2.2.0"
4715 resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a"4715 resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a"
4716 integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ==4716 integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ==
7110 integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=7110 integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=
71117111
7112resolve@^1.1.6, resolve@^1.10.0, resolve@^1.10.1, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.18.1, resolve@^1.19.0, resolve@^1.3.2:7112resolve@^1.1.6, resolve@^1.10.0, resolve@^1.10.1, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.18.1, resolve@^1.19.0, resolve@^1.3.2:
7113 version "1.19.0"7113 version "1.20.0"
7114 resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c"7114 resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
7115 integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==7115 integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==
7116 dependencies:7116 dependencies:
7117 is-core-module "^2.1.0"7117 is-core-module "^2.2.0"
7118 path-parse "^1.0.6"7118 path-parse "^1.0.6"
71197119
7120responselike@^1.0.2:7120responselike@^1.0.2: