difftreelog
Merge pull request #85 from usetech-llc/feature/NFTPAR-244_setContractSponsoringRateLimit
in: master
Add tests for setContractSponsoringRateLimit
4 files changed
tests/package.jsondiffbeforeafterboth1{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 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",29 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",30 "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",31 "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",32 "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",33 "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",34 "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",35 "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",36 "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",37 "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",38 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts"39 },40 "author": "",41 "license": "SEE LICENSE IN ../LICENSE",42 "homepage": "",43 "dependencies": {44 "@polkadot/api": "^3.6.4",45 "@polkadot/api-contract": "^3.6.4",46 "@polkadot/util": "^3.6.4",47 "bignumber.js": "^9.0.0",48 "chai-as-promised": "^7.1.1"49 },50 "standard": {51 "globals": [52 "it",53 "assert",54 "beforeEach",55 "afterEach",56 "describe",57 "contract",58 "artifacts"59 ]60 }61}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 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",29 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",30 "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",31 "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",32 "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",33 "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",34 "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",35 "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",36 "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",37 "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",38 "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",39 "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"40 },41 "author": "",42 "license": "SEE LICENSE IN ../LICENSE",43 "homepage": "",44 "dependencies": {45 "@polkadot/api": "^3.6.4",46 "@polkadot/api-contract": "^3.6.4",47 "@polkadot/util": "^3.6.4",48 "bignumber.js": "^9.0.0",49 "chai-as-promised": "^7.1.1"50 },51 "standard": {52 "globals": [53 "it",54 "assert",55 "beforeEach",56 "afterEach",57 "describe",58 "contract",59 "artifacts"60 ]61 }62}tests/src/setContractSponsoringRateLimit.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/setContractSponsoringRateLimit.test.ts
@@ -0,0 +1,62 @@
+import { IKeyringPair } from '@polkadot/types/types';
+import privateKey from './substrate/privateKey';
+import usingApi from './substrate/substrate-api';
+import waitNewBlocks from './substrate/wait-new-blocks';
+import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from './util/contracthelpers';
+import {
+ enableContractSponsoringExpectSuccess,
+ findUnusedAddress,
+ setContractSponsoringRateLimitExpectFailure,
+ setContractSponsoringRateLimitExpectSuccess,
+} from './util/helpers';
+
+describe('Integration Test setContractSponsoringRateLimit', () => {
+ it('ensure sponsored contract can\'t be called twice without pause for free', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ const [flipper, deployer] = await deployFlipper(api);
+ await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);
+ await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 10);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ await toggleFlipValueExpectFailure(user, flipper);
+ });
+ });
+
+ it('ensure sponsored contract can be called twice with pause for free', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ const [flipper, deployer] = await deployFlipper(api);
+ await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);
+ await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 1);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ await waitNewBlocks(api, 1);
+ await toggleFlipValueExpectSuccess(user, flipper);
+ });
+ });
+});
+
+describe('Negative Integration Test setContractSponsoringRateLimit', () => {
+ let alice: IKeyringPair;
+
+ before(async () => {
+ alice = privateKey('//Alice');
+ });
+
+ it('fails when called for non-contract address', async () => {
+ await usingApi(async (api) => {
+ const user = await findUnusedAddress(api);
+
+ await setContractSponsoringRateLimitExpectFailure(alice, user.address, 1);
+ });
+ });
+
+ it('fails when called by non-owning user', async () => {
+ await usingApi(async (api) => {
+ const [flipper, _] = await deployFlipper(api);
+
+ await setContractSponsoringRateLimitExpectFailure(alice, flipper.address, 1);
+ });
+ });
+});
tests/src/util/contracthelpers.tsdiffbeforeafterboth--- a/tests/src/util/contracthelpers.ts
+++ b/tests/src/util/contracthelpers.ts
@@ -5,7 +5,7 @@
import chai from "chai";
import chaiAsPromised from 'chai-as-promised';
-import { submitTransactionAsync } from "../substrate/substrate-api";
+import { submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";
import fs from "fs";
import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";
import { IKeyringPair } from "@polkadot/types/types";
@@ -14,7 +14,7 @@
chai.use(chaiAsPromised);
const expect = chai.expect;
import { BigNumber } from 'bignumber.js';
-import { findUnusedAddress } from '../util/helpers';
+import { findUnusedAddress, getGenericResult } from '../util/helpers';
const value = 0;
const gasLimit = '200000000000';
@@ -92,6 +92,19 @@
return (result.result.asOk.data[0] == 0x00) ? false : true;
}
+export async function toggleFlipValueExpectSuccess(sender: IKeyringPair, contract: Contract) {
+ const tx = contract.tx.flip(value, gasLimit);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+}
+
+export async function toggleFlipValueExpectFailure(sender: IKeyringPair, contract: Contract) {
+ const tx = contract.tx.flip(value, gasLimit);
+ await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+}
+
function instantiateTransferContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
return new Promise<any>(async (resolve, reject) => {
const unsub = await blueprint.tx
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -384,6 +384,46 @@
});
}
+export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+ });
+}
+
+export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);
+ const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.false;
+ });
+}
+
+export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+ });
+}
+
+export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);
+ const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.false;
+ });
+}
+
export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {
await usingApi(async (api) => {
const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));