difftreelog
Merge pull request #97 from usetech-llc/feature/NFTPAR-281_overflow_tests
in: master
Fungible overflow tests
8 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -40,7 +40,8 @@
"testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",
"testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
"testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
- "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"
+ "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
+ "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"
},
"author": "",
"license": "SEE LICENSE IN ../LICENSE",
tests/src/approve.test.tsdiffbeforeafterboth--- a/tests/src/approve.test.ts
+++ b/tests/src/approve.test.ts
@@ -12,8 +12,11 @@
approveExpectFail,
approveExpectSuccess,
createCollectionExpectSuccess,
+ createFungibleItemExpectSuccess,
createItemExpectSuccess,
destroyCollectionExpectSuccess,
+ transferFromExpectSuccess,
+ U128_MAX,
} from './util/helpers';
chai.use(chaiAsPromised);
tests/src/overflow.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/overflow.test.ts
@@ -0,0 +1,67 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
+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 () => {
+ 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 () => {
+ 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 () => {
+ 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)).toString()).to.equal('0');
+
+ 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)).toString()).to.equal('1');
+ });
+});
tests/src/removeFromContractWhiteList.test.tsdiffbeforeafterboth--- a/tests/src/removeFromContractWhiteList.test.ts
+++ b/tests/src/removeFromContractWhiteList.test.ts
@@ -1,3 +1,8 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
import privateKey from "./substrate/privateKey";
import usingApi from "./substrate/substrate-api";
import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from "./util/contracthelpers";
tests/src/substrate/substrate-api.tsdiffbeforeafterboth--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -18,9 +18,10 @@
return { provider: wsProvider, types: rtt };
}
-export default async function usingApi(action: (api: ApiPromise) => Promise<void>, settings: ApiOptions | undefined = undefined): Promise<void> {
+export default async function usingApi<T = void>(action: (api: ApiPromise) => Promise<T>, settings: ApiOptions | undefined = undefined): Promise<T> {
settings = settings || defaultApiOptions();
let api: ApiPromise = new ApiPromise(settings);
+ let result: T = null as unknown as T;
// TODO: Remove, this is temporary: Filter unneeded API output
// (Jaco promised it will be removed in the next version)
@@ -32,15 +33,16 @@
try {
await promisifySubstrate(api, async () => {
- if(api) {
+ if (api) {
await api.isReadyOrError;
- await action(api);
+ result = await action(api);
}
})();
} finally {
await api.disconnect();
console.error = consoleErr;
}
+ return result as T;
}
enum TransactionStatus {
tests/src/transferFrom.test.tsdiffbeforeafterboth--- a/tests/src/transferFrom.test.ts
+++ b/tests/src/transferFrom.test.ts
@@ -11,8 +11,10 @@
approveExpectFail,
approveExpectSuccess,
createCollectionExpectSuccess,
+ createFungibleItemExpectSuccess,
createItemExpectSuccess,
destroyCollectionExpectSuccess,
+ getAllowance,
transferFromExpectFail,
transferFromExpectSuccess,
burnItemExpectSuccess,
@@ -48,6 +50,22 @@
newReFungibleTokenId, Bob, Alice, Charlie, 100, 'ReFungible');
});
});
+
+ it('Should reduce allowance if value is big', async () => {
+ await usingApi(async () => {
+ const alice = privateKey('//Alice');
+ const bob = privateKey('//Bob');
+ const charlie = privateKey('//Charlie');
+
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createFungibleItemExpectSuccess(alice, fungibleCollectionId, { Value: 500000n });
+
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob, 500000n);
+ await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, bob, alice, charlie, 500000n, 'Fungible');
+ expect((await getAllowance(fungibleCollectionId, newFungibleTokenId, alice.address, bob.address)).toString()).to.equal('0');
+ });
+ });
});
describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
tests/src/util/helpers.tsdiffbeforeafterboth21chai.use(chaiAsPromised);21chai.use(chaiAsPromised);22const expect = chai.expect;22const expect = chai.expect;2324export const U128_MAX = (1n << 128n) - 1n;232524type GenericResult = {26type GenericResult = {25 success: boolean,27 success: boolean,238 return unused;240 return unused;239}241}242243export 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}240249241export 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}481490482export interface CreateFungibleData extends Struct {491export interface CreateFungibleData {483 readonly value: u128;492 readonly Value: bigint;484}493}485494486export interface CreateReFungibleData extends Struct {}495export interface CreateReFungibleData { }487export interface CreateNftData extends Struct {}496export interface CreateNftData { }488497489export 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};494505495export 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) => {510521511export async function522export async function512approveExpectSuccess(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}527538528export async function539export async function529transferFromExpectSuccess(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);632643633export async function644export async function634approveExpectFail(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}655656export 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}665666export 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 });674675 const events = await submitTransactionAsync(sender, tx);676 const result = getCreateItemResult(events);677678 expect(result.success).to.be.true;679 return result.itemId;680 });681}644682645export 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 = '') {tests/yarn.lockdiffbeforeafterboth--- a/tests/yarn.lock
+++ b/tests/yarn.lock
@@ -4710,7 +4710,7 @@
dependencies:
ci-info "^2.0.0"
-is-core-module@^2.1.0:
+is-core-module@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a"
integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ==
@@ -7110,11 +7110,11 @@
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=
resolve@^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:
- version "1.19.0"
- resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c"
- integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==
+ version "1.20.0"
+ resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
+ integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==
dependencies:
- is-core-module "^2.1.0"
+ is-core-module "^2.2.0"
path-parse "^1.0.6"
responselike@^1.0.2: