difftreelog
Merge pull request #85 from usetech-llc/feature/NFTPAR-244_setContractSponsoringRateLimit
in: master
Add tests for setContractSponsoringRateLimit
4 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -35,7 +35,8 @@
"testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",
"testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",
"testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",
- "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts"
+ "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
+ "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts"
},
"author": "",
"license": "SEE LICENSE IN ../LICENSE",
tests/src/setContractSponsoringRateLimit.test.tsdiffbeforeafterboth1import { IKeyringPair } from '@polkadot/types/types';2import privateKey from './substrate/privateKey';3import usingApi from './substrate/substrate-api';4import waitNewBlocks from './substrate/wait-new-blocks';5import { deployFlipper, toggleFlipValueExpectFailure, toggleFlipValueExpectSuccess } from './util/contracthelpers';6import {7 enableContractSponsoringExpectSuccess,8 findUnusedAddress,9 setContractSponsoringRateLimitExpectFailure,10 setContractSponsoringRateLimitExpectSuccess,11} from './util/helpers';1213describe('Integration Test setContractSponsoringRateLimit', () => {14 it('ensure sponsored contract can\'t be called twice without pause for free', async () => {15 await usingApi(async (api) => {16 const user = await findUnusedAddress(api);1718 const [flipper, deployer] = await deployFlipper(api);19 await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);20 await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 10);21 await toggleFlipValueExpectSuccess(user, flipper);22 await toggleFlipValueExpectFailure(user, flipper);23 });24 });2526 it('ensure sponsored contract can be called twice with pause for free', async () => {27 await usingApi(async (api) => {28 const user = await findUnusedAddress(api);2930 const [flipper, deployer] = await deployFlipper(api);31 await enableContractSponsoringExpectSuccess(deployer, flipper.address, true);32 await setContractSponsoringRateLimitExpectSuccess(deployer, flipper.address, 1);33 await toggleFlipValueExpectSuccess(user, flipper);34 await waitNewBlocks(api, 1);35 await toggleFlipValueExpectSuccess(user, flipper);36 });37 });38});3940describe('Negative Integration Test setContractSponsoringRateLimit', () => {41 let alice: IKeyringPair;4243 before(async () => {44 alice = privateKey('//Alice');45 });4647 it('fails when called for non-contract address', async () => {48 await usingApi(async (api) => {49 const user = await findUnusedAddress(api);5051 await setContractSponsoringRateLimitExpectFailure(alice, user.address, 1);52 });53 });5455 it('fails when called by non-owning user', async () => {56 await usingApi(async (api) => {57 const [flipper, _] = await deployFlipper(api);5859 await setContractSponsoringRateLimitExpectFailure(alice, flipper.address, 1);60 });61 });62});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'));