git.delta.rocks / unique-network / refs/commits / 21f171f02141

difftreelog

tests: add tests for fungible overflows

Yaroslav Bolyukin2021-02-10parent: #3b359ab.patch.diff
in: master

2 files changed

modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.61.24",8    "@polkadot/ts": "^0.3.59",9    "@polkadot/typegen": "^3.6.4",10    "@polkadot/util-crypto": "^5.4.4",11    "@types/chai": "^4.2.12",12    "@types/chai-as-promised": "^7.1.3",13    "@types/mocha": "^8.0.3",14    "chai": "^4.2.0",15    "mocha": "^8.1.1",16    "ts-node": "^9.0.0",17    "tslint": "^5.20.1",18    "typescript": "^3.9.7"19  },20  "scripts": {21    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",22    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",23    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",24    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",25    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",26    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",27    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",28    "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",29    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",30    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",31    "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",32    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",33    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",34    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",35    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",36    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",37    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",38    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",39    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",40    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",41    "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",42    "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"43  },44  "author": "",45  "license": "SEE LICENSE IN ../LICENSE",46  "homepage": "",47  "dependencies": {48    "@polkadot/api": "^3.6.4",49    "@polkadot/api-contract": "^3.6.4",50    "@polkadot/util": "^3.6.4",51    "bignumber.js": "^9.0.0",52    "chai-as-promised": "^7.1.1"53  },54  "standard": {55    "globals": [56      "it",57      "assert",58      "beforeEach",59      "afterEach",60      "describe",61      "contract",62      "artifacts"63    ]64  }65}
addedtests/src/overflow.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/overflow.test.ts
@@ -0,0 +1,68 @@
+import { IKeyringPair } from "@polkadot/types/types";
+import chai from 'chai';
+import chaiAsPromised from "chai-as-promised";
+import privateKey from "./substrate/privateKey";
+import usingApi from "./substrate/substrate-api";
+import { approveExpectFail, approveExpectSuccess, createCollectionExpectSuccess, createFungibleItemExpectSuccess, getAllowance, getFungibleBalance, transferExpectFail, transferExpectSuccess, transferFromExpectFail, transferFromExpectSuccess, U128_MAX } from "./util/helpers";
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test fungible overflows', () => {
+    let alice: IKeyringPair;
+    let bob: IKeyringPair;
+    let charlie: IKeyringPair;
+
+    before(async () => {
+        await usingApi(async () => {
+            alice = privateKey('//Alice');
+            bob = privateKey('//Bob');
+            charlie = privateKey('//Charlie');
+        });
+    });
+
+    it('fails when overflows on transfer', async () => {
+        await usingApi(async () => {
+            const fungibleCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
+
+            await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: U128_MAX });
+            await transferExpectSuccess(fungibleCollectionId, 0, alice, bob, U128_MAX, 'Fungible');
+
+            await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: 1n });
+            await transferExpectFail(fungibleCollectionId, 0, alice, bob, 1, 'Fungible');
+
+            expect(await getFungibleBalance(fungibleCollectionId, alice.address)).to.equal(1n);
+            expect(await getFungibleBalance(fungibleCollectionId, bob.address)).to.equal(U128_MAX);
+        });
+    });
+
+    it('fails on allowance overflow', async () => {
+        await usingApi(async () => {
+            const fungibleCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
+
+            await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: U128_MAX });
+            await approveExpectSuccess(fungibleCollectionId, 0, alice, bob, U128_MAX);
+            await approveExpectFail(fungibleCollectionId, 0, alice, bob, U128_MAX);
+        });
+    });
+
+    it('fails when overflows on transferFrom', async () => {
+        await usingApi(async () => {
+            const fungibleCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
+
+            await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: U128_MAX });
+            await approveExpectSuccess(fungibleCollectionId, 0, alice, bob, U128_MAX);
+            await transferFromExpectSuccess(fungibleCollectionId, 0, bob, alice, charlie, U128_MAX, 'Fungible');
+
+            expect(await getFungibleBalance(fungibleCollectionId, charlie.address)).to.equal(U128_MAX);
+            expect(await getAllowance(fungibleCollectionId, 0, alice.address, bob.address)).to.equal(0n);
+
+            await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: U128_MAX });
+            await approveExpectSuccess(fungibleCollectionId, 0, alice, bob, 1n);
+            await transferFromExpectFail(fungibleCollectionId, 0, bob, alice, charlie, 1);
+
+            expect(await getFungibleBalance(fungibleCollectionId, charlie.address)).to.equal(U128_MAX);
+            expect(await getAllowance(fungibleCollectionId, 0, alice.address, bob.address)).to.equal(0n);
+        });
+    });
+});