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
39 "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.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",40 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
41 "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.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"42 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
43 "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"
43 },44 },
44 "author": "",45 "author": "",
45 "license": "SEE LICENSE IN ../LICENSE",46 "license": "SEE LICENSE IN ../LICENSE",
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);
+        });
+    });
+});